2020-02-24 18:17:16 +00:00
|
|
|
use crate::{
|
2020-03-07 18:15:02 +00:00
|
|
|
comp::{CharacterState, StateUpdate},
|
|
|
|
sys::character_state::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-07 18:15:02 +00:00
|
|
|
pub fn behavior(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(),
|
|
|
|
};
|
|
|
|
|
|
|
|
if let CharacterState::Roll { remaining_duration } = data.character {
|
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;
|
|
|
|
|
2020-02-24 19:57:33 +00:00
|
|
|
// 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-02-24 19:57:33 +00:00
|
|
|
}
|
|
|
|
|
2020-03-07 18:15:02 +00:00
|
|
|
if *remaining_duration == Duration::default() {
|
2020-01-22 12:48:55 +00:00
|
|
|
// Roll duration has expired
|
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-07 18:15:02 +00:00
|
|
|
update.character = CharacterState::Roll {
|
|
|
|
remaining_duration: remaining_duration
|
|
|
|
.checked_sub(Duration::from_secs_f32(data.dt.0))
|
2020-01-22 12:48:55 +00:00
|
|
|
.unwrap_or_default(),
|
2020-03-07 18:15:02 +00:00
|
|
|
};
|
2020-01-22 12:48:55 +00:00
|
|
|
}
|
2019-12-26 14:43:59 +00:00
|
|
|
}
|
2020-03-07 18:15:02 +00:00
|
|
|
|
|
|
|
update
|
2019-12-26 14:43:59 +00:00
|
|
|
}
|