veloren/common/src/states/roll.rs

62 lines
2.0 KiB
Rust
Raw Normal View History

2020-02-24 18:17:16 +00:00
use crate::{
2020-03-07 18:15:02 +00:00
comp::{CharacterState, StateUpdate},
2020-03-14 21:17:27 +00:00
sys::character_behavior::{CharacterBehavior, JoinData},
2020-02-24 18:17:16 +00:00
};
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;
2020-03-14 21:17:27 +00:00
#[derive(Copy, Clone, Debug, PartialEq, Serialize, Deserialize, Eq, Hash)]
pub struct Data {
/// How long the state has until exiting
pub remaining_duration: Duration,
}
2020-01-07 15:49:08 +00:00
2020-03-14 21:17:27 +00:00
impl CharacterBehavior for Data {
fn behavior(&self, data: &JoinData) -> StateUpdate {
let mut update = StateUpdate {
character: *data.character,
pos: *data.pos,
vel: *data.vel,
ori: *data.ori,
energy: *data.energy,
local_events: VecDeque::new(),
server_events: VecDeque::new(),
};
2020-03-07 18:15:02 +00:00
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)
2020-03-07 18:15:02 +00:00
+ 1.5 * data.inputs.move_dir.try_normalized().unwrap_or_default())
2019-12-26 18:01:19 +00:00
.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 =
2020-03-07 18:15:02 +00:00
vek::ops::Slerp::slerp(update.ori.0, update.vel.0.into(), 9.0 * data.dt.0);
}
2020-03-14 21:17:27 +00:00
if self.remaining_duration == Duration::default() {
2020-01-22 12:48:55 +00:00
// Roll duration has expired
update.vel.0 *= 0.3;
2020-03-07 18:15:02 +00:00
update.character = CharacterState::Idle {};
2020-01-22 12:48:55 +00:00
} else {
// Otherwise, tick down remaining_duration
2020-03-14 21:17:27 +00:00
update.character = CharacterState::Roll(Data {
remaining_duration: self
.remaining_duration
2020-03-07 18:15:02 +00:00
.checked_sub(Duration::from_secs_f32(data.dt.0))
2020-01-22 12:48:55 +00:00
.unwrap_or_default(),
2020-03-14 21:17:27 +00:00
});
2020-01-22 12:48:55 +00:00
}
2020-03-07 18:15:02 +00:00
2020-03-14 21:17:27 +00:00
update
}
2019-12-26 14:43:59 +00:00
}