use super::utils::*;
use crate::{
comp::{
buff::{BuffChange, BuffKind},
character_state::OutputEvents,
controller::InputKind,
inventory::{
item::{ConsumableKind, ItemKind},
slot::{InvSlotId, Slot},
},
CharacterState, InventoryManip, StateUpdate,
},
event::{BuffEvent, InventoryManipEvent},
states::behavior::{CharacterBehavior, JoinData},
};
use serde::{Deserialize, Serialize};
use std::time::Duration;
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct StaticData {
pub buildup_duration: Duration,
pub use_duration: Duration,
pub recover_duration: Duration,
pub inv_slot: InvSlotId,
pub item_hash: u64,
pub item_kind: ItemUseKind,
pub was_wielded: bool,
pub was_sneak: bool,
}
#[derive(Clone, Debug, PartialEq, Eq, 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);
match self.static_data.item_kind {
ItemUseKind::Consumable(
ConsumableKind::Drink | ConsumableKind::Charm | ConsumableKind::Recipe,
) => {
handle_orientation(data, &mut update, 1.0, None);
handle_move(data, &mut update, 1.0);
},
ItemUseKind::Consumable(ConsumableKind::Food | ConsumableKind::ComplexFood) => {
handle_orientation(data, &mut update, 0.0, None);
handle_move(data, &mut update, 0.0);
},
}
let use_point = match self.static_data.item_kind {
ItemUseKind::Consumable(
ConsumableKind::Drink | ConsumableKind::Food | ConsumableKind::Recipe,
) => UsePoint::BuildupUse,
ItemUseKind::Consumable(ConsumableKind::ComplexFood | ConsumableKind::Charm) => {
UsePoint::UseRecover
},
};
match self.stage_section {
StageSection::Buildup => {
if self.timer < self.static_data.buildup_duration {
update.character = CharacterState::UseItem(Data {
static_data: self.static_data.clone(),
timer: tick_attack_or_default(data, self.timer, None),
..*self
});
} else {
update.character = CharacterState::UseItem(Data {
static_data: self.static_data.clone(),
timer: Duration::default(),
stage_section: StageSection::Action,
});
if let UsePoint::BuildupUse = use_point {
use_item(data, output_events, self);
}
}
},
StageSection::Action => {
if self.timer < self.static_data.use_duration {
update.character = CharacterState::UseItem(Data {
static_data: self.static_data.clone(),
timer: tick_attack_or_default(data, self.timer, None),
..*self
});
} else {
update.character = CharacterState::UseItem(Data {
static_data: self.static_data.clone(),
timer: Duration::default(),
stage_section: StageSection::Recover,
});
if let UsePoint::UseRecover = use_point {
use_item(data, output_events, self);
}
}
},
StageSection::Recover => {
if self.timer < self.static_data.recover_duration {
update.character = CharacterState::UseItem(Data {
static_data: self.static_data.clone(),
timer: tick_attack_or_default(
data,
self.timer,
Some(data.stats.recovery_speed_modifier),
),
..*self
});
} else {
end_ability(data, &mut update);
}
},
_ => {
end_ability(data, &mut update);
},
}
if input_is_pressed(data, InputKind::Roll) {
handle_input(data, output_events, &mut update, InputKind::Roll);
}
if matches!(update.character, CharacterState::Roll(_)) {
output_events.emit_server(BuffEvent {
entity: data.entity,
buff_change: BuffChange::RemoveByKind(BuffKind::Potion),
});
output_events.emit_server(BuffEvent {
entity: data.entity,
buff_change: BuffChange::RemoveByKind(BuffKind::Saturation),
});
}
update
}
}
#[derive(Copy, Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub enum ItemUseKind {
Consumable(ConsumableKind),
}
impl From<&ItemKind> for Option<ItemUseKind> {
fn from(item_kind: &ItemKind) -> Self {
match item_kind {
ItemKind::Consumable { kind, .. } => Some(ItemUseKind::Consumable(*kind)),
_ => None,
}
}
}
impl ItemUseKind {
pub fn durations(&self) -> (Duration, Duration, Duration) {
match self {
Self::Consumable(ConsumableKind::Drink) => (
Duration::from_secs_f32(0.1),
Duration::from_secs_f32(1.1),
Duration::from_secs_f32(0.1),
),
Self::Consumable(ConsumableKind::Food) => (
Duration::from_secs_f32(1.0),
Duration::from_secs_f32(4.0),
Duration::from_secs_f32(0.5),
),
Self::Consumable(ConsumableKind::ComplexFood) => (
Duration::from_secs_f32(1.0),
Duration::from_secs_f32(4.5),
Duration::from_secs_f32(0.5),
),
Self::Consumable(ConsumableKind::Charm) => (
Duration::from_secs_f32(0.1),
Duration::from_secs_f32(0.8),
Duration::from_secs_f32(0.1),
),
Self::Consumable(ConsumableKind::Recipe) => (
Duration::from_secs_f32(0.0),
Duration::from_secs_f32(0.0),
Duration::from_secs_f32(0.0),
),
}
}
}
enum UsePoint {
BuildupUse,
UseRecover,
}
fn use_item(data: &JoinData, output_events: &mut OutputEvents, state: &Data) {
let item_is_same = data
.inventory
.and_then(|inv| inv.get(state.static_data.inv_slot))
.map_or(false, |item| {
item.item_hash() == state.static_data.item_hash
});
if item_is_same {
let inv_manip = InventoryManip::Use(Slot::Inventory(state.static_data.inv_slot));
output_events.emit_server(InventoryManipEvent(data.entity, inv_manip));
}
}