118 lines
3.7 KiB
Rust
118 lines
3.7 KiB
Rust
//! LocalPlayer sprint decisions around KeyboardInput.tick. Conditions are the
|
|
//! current player queries; setting sprint dispatches to the live attribute state.
|
|
use crate::input;
|
|
|
|
#[derive(Clone, Copy, Debug, Default)]
|
|
pub struct Keys {
|
|
pub forward: bool,
|
|
pub backward: bool,
|
|
pub left: bool,
|
|
pub right: bool,
|
|
pub jump: bool,
|
|
pub shift: bool,
|
|
pub sprint: bool,
|
|
}
|
|
impl Keys {
|
|
pub fn direction(self) -> [f32; 2] {
|
|
input::keyboard_direction(self.forward, self.backward, self.left, self.right)
|
|
}
|
|
}
|
|
#[derive(Clone, Copy, Debug, Default)]
|
|
pub struct SprintState {
|
|
pub trigger_time: i32,
|
|
pub previous_forward: bool,
|
|
pub previous_shift: bool,
|
|
}
|
|
#[derive(Clone, Copy, Debug, Default)]
|
|
pub struct Conditions {
|
|
pub sprinting: bool,
|
|
pub mobility_restricted: bool,
|
|
pub passenger: bool,
|
|
pub vehicle_can_sprint: bool,
|
|
pub vehicle_local: bool,
|
|
pub enough_food: bool,
|
|
pub may_fly: bool,
|
|
pub flying: bool,
|
|
pub shallow_water: bool,
|
|
pub slow_item: bool,
|
|
pub fall_flying: bool,
|
|
pub under_water: bool,
|
|
pub moving_slowly: bool,
|
|
pub swimming: bool,
|
|
pub in_water: bool,
|
|
pub on_ground: bool,
|
|
pub horizontal_collision: bool,
|
|
pub minor_horizontal_collision: bool,
|
|
}
|
|
impl Conditions {
|
|
fn possible(self, allow_shallow_water: bool) -> bool {
|
|
!self.mobility_restricted
|
|
&& if self.passenger {
|
|
self.vehicle_can_sprint && self.vehicle_local
|
|
} else {
|
|
self.enough_food || self.may_fly
|
|
}
|
|
&& (allow_shallow_water || !self.shallow_water)
|
|
}
|
|
fn can_start(self, forward: bool) -> bool {
|
|
!self.sprinting
|
|
&& forward
|
|
&& self.possible(self.flying)
|
|
&& !self.slow_item
|
|
&& (!self.fall_flying || self.under_water)
|
|
&& (!self.moving_slowly || self.under_water)
|
|
}
|
|
fn should_stop(self, forward: bool, shift: bool) -> bool {
|
|
if self.swimming {
|
|
!self.possible(true) || !self.in_water || (!forward && !self.on_ground && !shift)
|
|
} else {
|
|
!self.possible(self.flying)
|
|
|| !forward
|
|
|| (self.horizontal_collision && !self.minor_horizontal_collision)
|
|
}
|
|
}
|
|
}
|
|
impl SprintState {
|
|
/// Runs the sprint phase. The default vanilla sprint window is seven ticks;
|
|
/// it is supplied so the options binding can also select 0..10. The setter
|
|
/// is called for every native request, including repeated true requests.
|
|
pub fn tick<E>(
|
|
&mut self,
|
|
keys: Keys,
|
|
conditions: Conditions,
|
|
window: i32,
|
|
mut set_sprinting: impl FnMut(bool) -> Result<(), E>,
|
|
) -> Result<(), E> {
|
|
if self.trigger_time > 0 {
|
|
self.trigger_time -= 1;
|
|
}
|
|
let old_forward = self.previous_forward;
|
|
let old_shift = self.previous_shift;
|
|
let forward = keys.direction()[1] > 1.0e-5;
|
|
self.previous_forward = forward;
|
|
self.previous_shift = keys.shift;
|
|
if old_shift || (conditions.slow_item && !conditions.passenger) || keys.backward {
|
|
self.trigger_time = 0;
|
|
}
|
|
let mut sprinting = conditions.sprinting;
|
|
if conditions.can_start(forward) {
|
|
if !old_forward {
|
|
if self.trigger_time > 0 {
|
|
set_sprinting(true)?;
|
|
sprinting = true;
|
|
} else {
|
|
self.trigger_time = window;
|
|
}
|
|
}
|
|
if keys.sprint {
|
|
set_sprinting(true)?;
|
|
sprinting = true;
|
|
}
|
|
}
|
|
if sprinting && conditions.should_stop(forward, keys.shift) {
|
|
set_sprinting(false)?;
|
|
}
|
|
Ok(())
|
|
}
|
|
}
|