veloren/common/src/states/roll.rs

131 lines
4.9 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},
states::utils::*,
2020-03-14 21:17:27 +00:00
sys::character_behavior::{CharacterBehavior, JoinData},
util::Dir,
2020-02-24 18:17:16 +00:00
};
use serde::{Deserialize, Serialize};
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;
/// Separated out to condense update portions of character state
#[derive(Copy, Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct StaticData {
/// How long until state should roll
pub buildup_duration: Duration,
/// How long state is rolling for
pub movement_duration: Duration,
/// How long it takes to recover from roll
pub recover_duration: Duration,
}
#[derive(Copy, Clone, Debug, PartialEq, Serialize, Deserialize)]
2020-03-14 21:17:27 +00:00
pub struct Data {
/// Struct containing data that does not change over the course of the
/// character state
pub static_data: StaticData,
/// Timer for each stage
pub timer: Duration,
/// What section the character stage is in
pub stage_section: StageSection,
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 {
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-04-01 13:42:56 +00:00
+ 0.25 * 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
2020-04-25 01:23:49 +00:00
update.ori.0 = Dir::slerp_to_vec3(update.ori.0, update.vel.0.xy().into(), 9.0 * data.dt.0);
match self.stage_section {
StageSection::Buildup => {
if self.timer < self.static_data.buildup_duration {
// Build up
update.character = CharacterState::Roll(Data {
static_data: self.static_data,
timer: self
.timer
.checked_add(Duration::from_secs_f32(data.dt.0))
.unwrap_or_default(),
stage_section: self.stage_section,
was_wielded: self.was_wielded,
});
} else {
// Transitions to movement section of stage
update.character = CharacterState::Roll(Data {
static_data: self.static_data,
timer: Duration::default(),
stage_section: StageSection::Movement,
was_wielded: self.was_wielded,
});
}
},
StageSection::Movement => {
if self.timer < self.static_data.movement_duration {
// Movement
update.character = CharacterState::Roll(Data {
static_data: self.static_data,
timer: self
.timer
.checked_add(Duration::from_secs_f32(data.dt.0))
.unwrap_or_default(),
stage_section: self.stage_section,
was_wielded: self.was_wielded,
});
} else {
// Transitions to recover section of stage
update.character = CharacterState::Roll(Data {
static_data: self.static_data,
timer: Duration::default(),
stage_section: StageSection::Recover,
was_wielded: self.was_wielded,
});
}
},
StageSection::Recover => {
if self.timer < self.static_data.recover_duration {
// Build up
update.character = CharacterState::Roll(Data {
static_data: self.static_data,
timer: self
.timer
.checked_add(Duration::from_secs_f32(data.dt.0))
.unwrap_or_default(),
stage_section: self.stage_section,
was_wielded: self.was_wielded,
});
} else {
// Done
if self.was_wielded {
update.character = CharacterState::Wielding;
} else {
update.character = CharacterState::Idle;
}
}
},
_ => {
// If it somehow ends up in an incorrect stage section
if self.was_wielded {
update.character = CharacterState::Wielding;
} else {
update.character = CharacterState::Idle;
}
},
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
}