veloren/common/src/states/basic_attack.rs

73 lines
2.1 KiB
Rust
Raw Normal View History

2020-02-11 15:42:17 +00:00
use crate::{
2020-03-07 18:15:02 +00:00
comp::{Attacking, CharacterState, ItemKind::Tool, StateUpdate},
states::utils::*,
2020-03-07 19:02:54 +00:00
sys::character_behavior::JoinData,
2020-02-11 15:42:17 +00:00
};
use std::{collections::VecDeque, time::Duration};
2019-12-26 14:43:59 +00:00
2020-03-07 18:15:02 +00:00
pub fn behavior(data: &JoinData) -> StateUpdate {
let mut update = StateUpdate {
pos: *data.pos,
vel: *data.vel,
ori: *data.ori,
energy: *data.energy,
character: *data.character,
local_events: VecDeque::new(),
server_events: VecDeque::new(),
};
2020-01-05 23:17:22 +00:00
2020-03-07 18:15:02 +00:00
if let CharacterState::BasicAttack {
exhausted,
remaining_duration,
} = data.character
{
handle_move(data, &mut update);
2020-02-24 13:32:12 +00:00
2020-03-07 18:15:02 +00:00
let tool_kind = data.stats.equipment.main.as_ref().map(|i| i.kind);
let can_apply_damage = !*exhausted
&& if let Some(Tool(tool)) = tool_kind {
*remaining_duration < tool.attack_recover_duration()
2020-02-24 13:32:12 +00:00
} else {
true
2020-02-24 13:32:12 +00:00
};
2020-03-07 18:15:02 +00:00
let mut new_exhausted = *exhausted;
2020-02-24 13:32:12 +00:00
if can_apply_damage {
2020-03-07 18:15:02 +00:00
if let Some(Tool(tool)) = tool_kind {
data.updater
.insert(data.entity, Attacking { weapon: Some(tool) });
} else {
2020-03-07 18:15:02 +00:00
data.updater.insert(data.entity, Attacking { weapon: None });
2020-02-24 13:32:12 +00:00
}
2020-03-07 18:15:02 +00:00
new_exhausted = true;
2020-02-24 13:32:12 +00:00
} else {
2020-03-07 18:15:02 +00:00
data.updater.remove::<Attacking>(data.entity);
2020-02-24 13:32:12 +00:00
}
2020-03-07 18:15:02 +00:00
let new_remaining_duration = remaining_duration
.checked_sub(Duration::from_secs_f32(data.dt.0))
2020-02-24 13:32:12 +00:00
.unwrap_or_default();
// Tick down
2020-03-07 18:15:02 +00:00
update.character = CharacterState::BasicAttack {
remaining_duration: new_remaining_duration,
exhausted: new_exhausted,
};
2020-02-24 13:32:12 +00:00
// Check if attack duration has expired
2020-03-07 18:15:02 +00:00
if new_remaining_duration == Duration::default() {
update.character = if let Some(Tool(tool)) = tool_kind {
CharacterState::Wielding { tool }
} else {
CharacterState::Idle {}
};
data.updater.remove::<Attacking>(data.entity);
2020-02-24 13:32:12 +00:00
}
2019-12-26 18:01:19 +00:00
2020-03-07 18:15:02 +00:00
update
} else {
2020-01-12 23:14:08 +00:00
update
2019-12-26 14:43:59 +00:00
}
}