2020-01-21 22:54:32 +00:00
|
|
|
use super::utils::*;
|
2020-02-24 18:17:16 +00:00
|
|
|
use crate::{
|
|
|
|
comp::{CharacterState, EcsStateData, ItemKind::Tool, StateUpdate, ToolData},
|
|
|
|
states::StateHandler,
|
|
|
|
};
|
|
|
|
use std::{collections::VecDeque, time::Duration};
|
2019-12-26 14:43:59 +00:00
|
|
|
|
2020-01-05 18:19:09 +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 before a new action can be performed
|
|
|
|
/// after equipping
|
|
|
|
pub equip_delay: Duration,
|
|
|
|
}
|
|
|
|
|
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 {
|
2020-02-24 21:20:50 +00:00
|
|
|
let equip_delay =
|
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-02-24 21:20:50 +00:00
|
|
|
data.equip_time()
|
2020-01-05 23:17:22 +00:00
|
|
|
} else {
|
2020-02-24 21:20:50 +00:00
|
|
|
Duration::default()
|
2020-01-05 23:17:22 +00:00
|
|
|
};
|
2020-01-21 22:54:32 +00:00
|
|
|
|
2020-02-24 21:20:50 +00:00
|
|
|
Self { equip_delay }
|
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
|
|
|
character: *ecs_data.character,
|
|
|
|
pos: *ecs_data.pos,
|
|
|
|
vel: *ecs_data.vel,
|
|
|
|
ori: *ecs_data.ori,
|
2020-02-24 18:17:16 +00:00
|
|
|
energy: *ecs_data.energy,
|
2020-02-03 10:54:50 +00:00
|
|
|
local_events: VecDeque::new(),
|
|
|
|
server_events: VecDeque::new(),
|
2019-12-26 14:43:59 +00:00
|
|
|
};
|
|
|
|
|
2020-01-21 22:54:32 +00:00
|
|
|
handle_move_dir(&ecs_data, &mut update);
|
2019-12-26 14:43:59 +00:00
|
|
|
|
2020-01-21 22:54:32 +00:00
|
|
|
if self.equip_delay == Duration::default() {
|
|
|
|
// Wield delay has expired
|
|
|
|
update.character = CharacterState::Wielded(None);
|
2019-12-28 16:10:39 +00:00
|
|
|
} else {
|
2020-01-21 22:54:32 +00:00
|
|
|
// Wield delay hasn't expired yet
|
2019-12-26 18:01:19 +00:00
|
|
|
// Update wield delay
|
2020-01-21 22:54:32 +00:00
|
|
|
update.character = CharacterState::Wielding(Some(State {
|
2019-12-26 14:43:59 +00:00
|
|
|
equip_delay: self
|
|
|
|
.equip_delay
|
|
|
|
.checked_sub(Duration::from_secs_f32(ecs_data.dt.0))
|
|
|
|
.unwrap_or_default(),
|
2020-01-05 18:19:09 +00:00
|
|
|
}));
|
2019-12-26 18:01:19 +00:00
|
|
|
}
|
2019-12-26 14:43:59 +00:00
|
|
|
|
2020-01-12 23:14:08 +00:00
|
|
|
update
|
2019-12-26 14:43:59 +00:00
|
|
|
}
|
|
|
|
}
|