b7330ac0cc
Vanilla 26.3 has no gamepad input, so this is Horizon's own, on Bedrock's default layout (docs/controllers.md). gilrs reads the pads; the pure part (bindings, radial deadzones, the look curve, button edges, the horizon-controller.txt settings file) is in gamepad.rs with unit tests. Buttons act as the vanilla key mapping they're bound to, through the same path a rebound keyboard key takes (the keyboard's post-rebinding handling moves into Game::mapped_key). The left stick is an analog move vector, the right stick turns the view per frame, LB/RB step the hotbar, B and Start leave screens and A respawns. horizon-ctl pad injects a virtual pad. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
137 lines
4.5 KiB
Rust
137 lines
4.5 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,
|
|
/// A controller stick's move vector (`[left, forward]`, Horizon's own),
|
|
/// used instead of the keys' direction while it is pushed.
|
|
pub analog: Option<[f32; 2]>,
|
|
}
|
|
impl Keys {
|
|
pub fn direction(self) -> [f32; 2] {
|
|
match self.analog {
|
|
Some(v) => v,
|
|
None => input::keyboard_direction(self.forward, self.backward, self.left, self.right),
|
|
}
|
|
}
|
|
/// Add a stick's move vector: the direction flags code reading keys
|
|
/// (sprint, vehicles, the server's input packet) see are set past a
|
|
/// third of the travel.
|
|
pub fn with_stick(mut self, stick: Option<[f32; 2]>) -> Self {
|
|
if let Some([left, forward]) = stick {
|
|
self.forward |= forward > 0.3;
|
|
self.backward |= forward < -0.3;
|
|
self.left |= left > 0.3;
|
|
self.right |= left < -0.3;
|
|
self.analog = Some([left, forward]);
|
|
}
|
|
self
|
|
}
|
|
}
|
|
#[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(())
|
|
}
|
|
}
|