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

53 lines
1.7 KiB
Rust
Raw Normal View History

2019-12-26 18:01:19 +00:00
use crate::comp::{
2020-01-05 23:17:22 +00:00
ActionState::Attack, AttackKind::BasicAttack, EcsStateData, ItemKind::Tool, MoveState,
StateHandler, StateUpdate, ToolData,
2019-12-26 18:01:19 +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;
#[derive(Clone, Copy, Default, Debug, PartialEq, Serialize, Deserialize, Eq, Hash)]
2019-12-28 16:10:39 +00:00
pub struct BasicAttackState {
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,
2019-12-26 14:43:59 +00:00
}
2020-01-05 18:19:09 +00:00
impl StateHandler for BasicAttackState {
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 {
remaining_duration: tool_data.attack_duration(),
}
}
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,
character: *ecs_data.character,
};
2019-12-26 18:01:19 +00:00
// Check if attack duration has expired
if self.remaining_duration == Duration::default() {
// If so, go back to wielding or idling
2019-12-28 16:10:39 +00:00
update.character.action_state = attempt_wield(ecs_data.stats);
2019-12-26 18:01:19 +00:00
return update;
}
// Otherwise, tick down remaining_duration, and keep rolling
2020-01-05 18:19:09 +00:00
update.character.action_state = Attack(BasicAttack(Some(BasicAttackState {
2019-12-26 18:01:19 +00:00
remaining_duration: self
.remaining_duration
.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
return update;
2019-12-26 14:43:59 +00:00
}
}