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-03-28 01:31:22 +00:00
|
|
|
util::Dir,
|
2020-02-24 18:17:16 +00:00
|
|
|
};
|
2020-03-21 21:16:26 +00:00
|
|
|
use std::time::Duration;
|
2019-12-26 18:01:19 +00:00
|
|
|
use vek::Vec3;
|
2019-12-28 16:10:39 +00:00
|
|
|
|
2020-04-01 13:23:26 +00:00
|
|
|
const ROLL_SPEED: f32 = 25.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-03-26 13:46:08 +00:00
|
|
|
/// Had weapon
|
|
|
|
pub was_wielded: bool,
|
2020-03-14 21:17:27 +00:00
|
|
|
}
|
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 {
|
2020-03-21 21:16:26 +00:00
|
|
|
let mut update = StateUpdate::from(data);
|
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;
|
|
|
|
|
2020-02-24 19:57:33 +00:00
|
|
|
// Smooth orientation
|
2020-03-28 01:31:22 +00:00
|
|
|
update.ori.0 = Dir::slerp_to_vec3(update.ori.0, update.vel.0, 9.0 * data.dt.0);
|
2020-02-24 19:57:33 +00:00
|
|
|
|
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
|
2020-03-10 17:54:59 +00:00
|
|
|
update.vel.0 *= 0.3;
|
2020-03-26 13:46:08 +00:00
|
|
|
if self.was_wielded {
|
|
|
|
update.character = CharacterState::Wielding;
|
|
|
|
} else {
|
|
|
|
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-26 13:46:08 +00:00
|
|
|
was_wielded: self.was_wielded,
|
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
|
|
|
}
|