2020-02-11 15:42:17 +00:00
|
|
|
use crate::{
|
|
|
|
comp::{Attacking, CharacterState, EcsStateData, ItemKind::Tool, StateUpdate, ToolData},
|
2020-02-24 19:57:33 +00:00
|
|
|
states::StateHandler,
|
2020-02-11 15:42:17 +00:00
|
|
|
};
|
|
|
|
use std::{collections::VecDeque, time::Duration};
|
2019-12-26 14:43:59 +00:00
|
|
|
|
|
|
|
#[derive(Clone, Copy, Default, Debug, PartialEq, Serialize, Deserialize, Eq, Hash)]
|
2020-01-08 16:56:36 +00:00
|
|
|
pub struct State {
|
2019-12-26 14:43:59 +00:00
|
|
|
/// How long the state has until exitting
|
2019-12-30 13:56:42 +00:00
|
|
|
pub remaining_duration: Duration,
|
2020-02-24 13:32:12 +00:00
|
|
|
/// Whether the attack can deal more damage
|
|
|
|
pub exhausted: bool,
|
2019-12-26 14:43:59 +00:00
|
|
|
}
|
|
|
|
|
2020-01-08 16:56:36 +00:00
|
|
|
impl StateHandler for State {
|
2020-01-05 23:17:22 +00:00
|
|
|
fn new(ecs_data: &EcsStateData) -> Self {
|
|
|
|
let tool_data =
|
2020-01-05 23:21:37 +00:00
|
|
|
if let Some(Tool(data)) = ecs_data.stats.equipment.main.as_ref().map(|i| i.kind) {
|
2020-01-05 23:17:22 +00:00
|
|
|
data
|
|
|
|
} else {
|
2020-01-05 23:21:37 +00:00
|
|
|
ToolData::default()
|
2020-01-05 23:17:22 +00:00
|
|
|
};
|
|
|
|
Self {
|
|
|
|
remaining_duration: tool_data.attack_duration(),
|
2020-02-24 13:32:12 +00:00
|
|
|
exhausted: false,
|
2020-01-05 23:17:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-28 16:10:39 +00:00
|
|
|
fn handle(&self, ecs_data: &EcsStateData) -> StateUpdate {
|
|
|
|
let mut update = StateUpdate {
|
2019-12-26 14:43:59 +00:00
|
|
|
pos: *ecs_data.pos,
|
|
|
|
vel: *ecs_data.vel,
|
|
|
|
ori: *ecs_data.ori,
|
2020-02-24 18:17:16 +00:00
|
|
|
energy: *ecs_data.energy,
|
2019-12-26 14:43:59 +00:00
|
|
|
character: *ecs_data.character,
|
2020-02-03 10:54:50 +00:00
|
|
|
local_events: VecDeque::new(),
|
|
|
|
server_events: VecDeque::new(),
|
2019-12-26 14:43:59 +00:00
|
|
|
};
|
2019-12-26 18:01:19 +00:00
|
|
|
|
2020-02-24 13:32:12 +00:00
|
|
|
let tool_kind = ecs_data.stats.equipment.main.as_ref().map(|i| i.kind);
|
|
|
|
|
|
|
|
let can_apply_damage = !self.exhausted
|
|
|
|
&& if let Some(Tool(data)) = tool_kind {
|
|
|
|
(self.remaining_duration < data.attack_recover_duration())
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
};
|
|
|
|
|
|
|
|
let mut exhausted = self.exhausted;
|
|
|
|
|
|
|
|
if can_apply_damage {
|
|
|
|
if let Some(Tool(data)) = tool_kind {
|
|
|
|
ecs_data
|
|
|
|
.updater
|
|
|
|
.insert(*ecs_data.entity, Attacking { weapon: data });
|
|
|
|
exhausted = true;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
ecs_data.updater.remove::<Attacking>(*ecs_data.entity);
|
|
|
|
}
|
|
|
|
|
|
|
|
let remaining_duration = self
|
|
|
|
.remaining_duration
|
|
|
|
.checked_sub(Duration::from_secs_f32(ecs_data.dt.0))
|
|
|
|
.unwrap_or_default();
|
|
|
|
|
|
|
|
// Tick down
|
|
|
|
update.character = CharacterState::BasicAttack(Some(State {
|
|
|
|
remaining_duration,
|
|
|
|
exhausted,
|
|
|
|
}));
|
|
|
|
|
|
|
|
// Check if attack duration has expired
|
|
|
|
if remaining_duration == Duration::default() {
|
|
|
|
update.character = CharacterState::Wielded(None);
|
|
|
|
ecs_data.updater.remove::<Attacking>(*ecs_data.entity);
|
|
|
|
}
|
2019-12-26 18:01:19 +00:00
|
|
|
|
2020-01-12 23:14:08 +00:00
|
|
|
update
|
2019-12-26 14:43:59 +00:00
|
|
|
}
|
|
|
|
}
|