veloren/common/src/states/basic_attack.rs

85 lines
2.8 KiB
Rust
Raw Normal View History

2020-02-11 15:42:17 +00:00
use crate::{
comp::{Attacking, CharacterState, EnergySource, ItemKind::Tool, StateUpdate},
2020-03-07 18:15:02 +00:00
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,
2020-03-14 15:40:29 +00:00
buildup_duration,
recover_duration,
2020-03-07 18:15:02 +00:00
} = data.character
{
let tool_kind = data.stats.equipment.main.as_ref().map(|i| i.kind);
2020-03-14 15:40:29 +00:00
handle_move(data, &mut update);
2020-02-24 13:32:12 +00:00
2020-03-14 15:40:29 +00:00
if buildup_duration != &Duration::default() {
// Start to swing
update.character = CharacterState::BasicAttack {
buildup_duration: buildup_duration
.checked_sub(Duration::from_secs_f32(data.dt.0))
.unwrap_or_default(),
recover_duration: *recover_duration,
exhausted: false,
};
} else if !*exhausted {
// Swing hits
if let Some(Tool(tool)) = tool_kind {
data.updater.insert(data.entity, Attacking {
weapon: Some(tool),
applied: false,
hit_count: 0,
});
}
2020-02-24 13:32:12 +00:00
update.character = CharacterState::BasicAttack {
2020-03-14 15:40:29 +00:00
buildup_duration: *buildup_duration,
recover_duration: *recover_duration,
exhausted: true,
2020-03-07 18:15:02 +00:00
};
2020-03-14 15:40:29 +00:00
} else if recover_duration != &Duration::default() {
// Recover from swing
update.character = CharacterState::BasicAttack {
buildup_duration: *buildup_duration,
recover_duration: recover_duration
.checked_sub(Duration::from_secs_f32(data.dt.0))
.unwrap_or_default(),
exhausted: true,
}
} else {
// Done
if let Some(Tool(tool)) = tool_kind {
update.character = CharacterState::Wielding { tool };
data.updater.remove::<Attacking>(data.entity);
2020-03-14 15:40:29 +00:00
} else {
update.character = CharacterState::Idle;
}
2020-03-14 15:40:29 +00:00
}
2020-03-14 15:40:29 +00:00
// More handling
if let Some(attack) = data.attacking {
if attack.applied && attack.hit_count > 0 {
data.updater.remove::<Attacking>(data.entity);
update.energy.change_by(100, EnergySource::HitEnemy);
}
}
2020-03-14 15:40:29 +00:00
update
2020-03-07 18:15:02 +00:00
} else {
update.character = CharacterState::Idle {};
2020-01-12 23:14:08 +00:00
update
2019-12-26 14:43:59 +00:00
}
}