veloren/common/src/comp/states/wield.rs

70 lines
2.3 KiB
Rust
Raw Normal View History

2020-01-01 17:16:29 +00:00
use crate::comp::{
2020-01-05 23:17:22 +00:00
AbilityAction, AbilityActionKind::*, ActionState::*, EcsStateData, IdleState, ItemKind::Tool,
StateHandler, StateUpdate, ToolData,
2020-01-01 17:16:29 +00:00
};
2020-01-05 18:19:09 +00:00
use crate::util::state_utils::*;
2019-12-26 14:43:59 +00:00
use std::time::Duration;
2020-01-05 18:19:09 +00:00
#[derive(Clone, Copy, Default, Debug, PartialEq, Serialize, Deserialize, Eq, Hash)]
2019-12-28 16:10:39 +00:00
pub struct WieldState {
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-05 18:19:09 +00:00
impl StateHandler for WieldState {
2020-01-05 23:17:22 +00:00
fn new(ecs_data: &EcsStateData) -> Self {
let tool_data =
if let Some(Tool(data)) = ecs_data.stats.equipment.main.as_ref().map(|i| &i.kind) {
data
} else {
&ToolData::default()
};
Self {
equip_delay: tool_data.equip_time(),
}
}
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,
};
2019-12-26 18:01:19 +00:00
// Only act once equip_delay has expired
if self.equip_delay == Duration::default() {
// Toggle Weapons
2019-12-29 16:36:59 +00:00
if ecs_data.inputs.toggle_wield.is_just_pressed()
2019-12-26 18:01:19 +00:00
&& ecs_data.character.action_state.is_equip_finished()
{
2020-01-05 18:19:09 +00:00
update.character.action_state = Idle(Some(IdleState));
2019-12-26 18:01:19 +00:00
return update;
}
2019-12-26 14:43:59 +00:00
2019-12-26 18:01:19 +00:00
// Try weapon actions
if ecs_data.inputs.primary.is_pressed() {
2020-01-05 18:19:09 +00:00
// ecs_data
// .updater
// .insert(*ecs_data.entity, AbilityAction(Primary));
2019-12-26 18:01:19 +00:00
} else if ecs_data.inputs.secondary.is_pressed() {
2020-01-05 18:19:09 +00:00
// ecs_data
// .updater
// .insert(*ecs_data.entity, AbilityAction(Secondary));
2019-12-26 18:01:19 +00:00
}
2019-12-28 16:10:39 +00:00
} else {
// Equip delay hasn't expired yet
2019-12-26 18:01:19 +00:00
// Update wield delay
2020-01-05 18:19:09 +00:00
update.character.action_state = Wield(Some(WieldState {
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
return update;
}
}