veloren/common/src/states/roll.rs

79 lines
2.5 KiB
Rust
Raw Normal View History

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 18:01:19 +00:00
use vek::Vec3;
2019-12-28 16:10:39 +00:00
2020-01-07 15:49:08 +00:00
const ROLL_SPEED: f32 = 17.0;
2019-12-26 14:43:59 +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 the state has until exitting
remaining_duration: 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 {
let tool_data =
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-01-05 23:17:22 +00:00
data
} else {
2020-01-05 23:21:37 +00:00
ToolData::default()
2020-01-05 23:17:22 +00:00
};
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
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 18:01:19 +00:00
};
// Update velocity
update.vel.0 = Vec3::new(0.0, 0.0, update.vel.0.z)
+ (update.vel.0 * Vec3::new(1.0, 1.0, 0.0)
+ 1.5
* ecs_data
.inputs
.move_dir
.try_normalized()
.unwrap_or_default())
.try_normalized()
.unwrap_or_default()
* ROLL_SPEED;
// Smooth orientation
if update.vel.0.magnitude_squared() > 0.0001
&& (update.ori.0.normalized() - Vec3::from(update.vel.0).normalized())
.magnitude_squared()
> 0.001
{
update.ori.0 =
vek::ops::Slerp::slerp(update.ori.0, update.vel.0.into(), 9.0 * ecs_data.dt.0);
}
2020-01-22 12:48:55 +00:00
if self.remaining_duration == Duration::default() {
// Roll duration has expired
update.character = CharacterState::Idle(None);
} else {
// Otherwise, tick down remaining_duration
update.character = CharacterState::Roll(Some(State {
remaining_duration: self
.remaining_duration
.checked_sub(Duration::from_secs_f32(ecs_data.dt.0))
.unwrap_or_default(),
}));
}
2019-12-26 18:01:19 +00:00
2020-01-12 23:14:08 +00:00
update
2019-12-26 14:43:59 +00:00
}
}