use super::utils::*;
use crate::{
comp::{
character_state::OutputEvents, controller::InputKind, item::ItemDefinitionIdOwned,
slot::InvSlotId, CharacterState, InventoryManip, StateUpdate,
},
event::{InventoryManipEvent, LocalEvent, ToggleSpriteLightEvent},
outcome::Outcome,
states::behavior::{CharacterBehavior, JoinData},
terrain::SpriteKind,
util::Dir,
};
use serde::{Deserialize, Serialize};
use std::time::Duration;
use vek::Vec3;
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct StaticData {
pub buildup_duration: Duration,
pub use_duration: Duration,
pub recover_duration: Duration,
pub sprite_pos: Vec3<i32>,
pub sprite_kind: SpriteInteractKind,
pub was_wielded: bool,
pub was_sneak: bool,
pub required_item: Option<(ItemDefinitionIdOwned, InvSlotId, bool)>,
}
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct Data {
pub static_data: StaticData,
pub timer: Duration,
pub stage_section: StageSection,
}
impl CharacterBehavior for Data {
fn behavior(&self, data: &JoinData, output_events: &mut OutputEvents) -> StateUpdate {
let mut update = StateUpdate::from(data);
let ori_dir = Dir::from_unnormalized(Vec3::from(
(self.static_data.sprite_pos.map(|x| x as f32 + 0.5) - data.pos.0).xy(),
));
handle_orientation(data, &mut update, 1.0, ori_dir);
handle_move(data, &mut update, 0.0);
match self.stage_section {
StageSection::Buildup => {
if self.timer < self.static_data.buildup_duration {
if let CharacterState::SpriteInteract(c) = &mut update.character {
c.timer = tick_attack_or_default(data, self.timer, None);
}
} else {
if let CharacterState::SpriteInteract(c) = &mut update.character {
c.timer = Duration::default();
c.stage_section = StageSection::Action;
}
}
},
StageSection::Action => {
if self.timer < self.static_data.use_duration {
if let CharacterState::SpriteInteract(c) = &mut update.character {
c.timer = tick_attack_or_default(data, self.timer, None);
}
} else {
if let CharacterState::SpriteInteract(c) = &mut update.character {
c.timer = Duration::default();
c.stage_section = StageSection::Recover;
}
}
},
StageSection::Recover => {
if self.timer < self.static_data.recover_duration {
if let CharacterState::SpriteInteract(c) = &mut update.character {
c.timer = tick_attack_or_default(
data,
self.timer,
Some(data.stats.recovery_speed_modifier),
);
}
} else {
let (has_required_item, inv_slot) = self
.static_data
.required_item
.as_ref()
.map_or((true, None), |&(ref item_def_id, slot, consume)| {
let has_item = data
.inventory
.and_then(|inv| inv.get(slot))
.map_or(false, |item| item.item_definition_id() == *item_def_id);
(has_item, has_item.then_some((slot, consume)))
});
if has_required_item {
let inv_manip = InventoryManip::Collect {
sprite_pos: self.static_data.sprite_pos,
required_item: inv_slot,
};
match self.static_data.sprite_kind {
SpriteInteractKind::ToggleLight(enable) => {
output_events.emit_server(ToggleSpriteLightEvent {
entity: data.entity,
pos: self.static_data.sprite_pos,
enable,
})
},
_ => output_events
.emit_server(InventoryManipEvent(data.entity, inv_manip)),
}
if matches!(self.static_data.sprite_kind, SpriteInteractKind::Unlock) {
output_events.emit_local(LocalEvent::CreateOutcome(
Outcome::SpriteUnlocked {
pos: self.static_data.sprite_pos,
},
));
}
}
end_ability(data, &mut update);
}
},
_ => {
end_ability(data, &mut update);
},
}
handle_wield(data, &mut update);
if input_is_pressed(data, InputKind::Roll) {
handle_input(data, output_events, &mut update, InputKind::Roll);
}
update
}
}
#[derive(Copy, Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub enum SpriteInteractKind {
Chest,
Harvestable,
Collectible,
Unlock,
Fallback,
ToggleLight(bool),
}
impl From<SpriteKind> for Option<SpriteInteractKind> {
fn from(sprite_kind: SpriteKind) -> Self {
match sprite_kind {
SpriteKind::Apple
| SpriteKind::Mushroom
| SpriteKind::RedFlower
| SpriteKind::Sunflower
| SpriteKind::Coconut
| SpriteKind::Beehive
| SpriteKind::Cotton
| SpriteKind::Moonbell
| SpriteKind::Pyrebloom
| SpriteKind::WildFlax
| SpriteKind::RoundCactus
| SpriteKind::ShortFlatCactus
| SpriteKind::MedFlatCactus
| SpriteKind::Wood
| SpriteKind::Bamboo
| SpriteKind::Hardwood
| SpriteKind::Ironwood
| SpriteKind::Frostwood
| SpriteKind::Eldwood => Some(SpriteInteractKind::Harvestable),
SpriteKind::Stones
| SpriteKind::Twigs
| SpriteKind::VialEmpty
| SpriteKind::Bowl
| SpriteKind::PotionMinor
| SpriteKind::Seashells
| SpriteKind::Bomb => Some(SpriteInteractKind::Collectible),
SpriteKind::Keyhole
| SpriteKind::BoneKeyhole
| SpriteKind::HaniwaKeyhole
| SpriteKind::SahaginKeyhole
| SpriteKind::VampireKeyhole
| SpriteKind::GlassKeyhole
| SpriteKind::KeyholeBars
| SpriteKind::TerracottaKeyhole
| SpriteKind::MyrmidonKeyhole
| SpriteKind::MinotaurKeyhole => Some(SpriteInteractKind::Unlock),
_ if sprite_kind.is_container() && sprite_kind.is_collectible() => {
Some(SpriteInteractKind::Chest)
},
_ if sprite_kind.is_collectible() => Some(SpriteInteractKind::Fallback),
_ => None,
}
}
}
impl SpriteInteractKind {
pub fn durations(&self) -> (Duration, Duration, Duration) {
match self {
Self::Chest => (
Duration::from_secs_f32(0.5),
Duration::from_secs_f32(2.0),
Duration::from_secs_f32(0.5),
),
Self::Collectible => (
Duration::from_secs_f32(0.1),
Duration::from_secs_f32(0.2),
Duration::from_secs_f32(0.1),
),
Self::Harvestable => (
Duration::from_secs_f32(0.3),
Duration::from_secs_f32(0.3),
Duration::from_secs_f32(0.2),
),
Self::Fallback => (
Duration::from_secs_f32(5.0),
Duration::from_secs_f32(5.0),
Duration::from_secs_f32(5.0),
),
Self::Unlock => (
Duration::from_secs_f32(0.8),
Duration::from_secs_f32(1.0),
Duration::from_secs_f32(0.3),
),
Self::ToggleLight(_) => (
Duration::from_secs_f32(0.1),
Duration::from_secs_f32(0.2),
Duration::from_secs_f32(0.1),
),
}
}
}