aboutsummaryrefslogtreecommitdiff
path: root/crates/fparkan-runtime/src
diff options
context:
space:
mode:
Diffstat (limited to 'crates/fparkan-runtime/src')
-rw-r--r--crates/fparkan-runtime/src/lib.rs5
-rw-r--r--crates/fparkan-runtime/src/player_gait.rs78
2 files changed, 83 insertions, 0 deletions
diff --git a/crates/fparkan-runtime/src/lib.rs b/crates/fparkan-runtime/src/lib.rs
index 0dc5e4a5..11bac68d 100644
--- a/crates/fparkan-runtime/src/lib.rs
+++ b/crates/fparkan-runtime/src/lib.rs
@@ -94,6 +94,7 @@ pub use laboratory::LaboratoryReport;
mod mission_events;
mod ore_transport;
mod patrol_points;
+mod player_gait;
mod player_motion;
mod unit_speed;
pub use patrol_points::PatrolPointReport;
@@ -154,6 +155,7 @@ pub use mission_events::{
capture_building, capture_building_with_notice, capture_neutral_building,
capture_neutral_warbot, MissionEventStop, MissionEventTick, MissionNotice,
};
+pub use player_gait::player_walk_camera_lift;
pub use player_motion::{
plan_player_motion, plan_player_motion_with_collision, PlayerMotion, PlayerMotionStep,
};
@@ -1455,6 +1457,7 @@ struct LoadedMissionState {
construction: construction::Construction,
unit_patrol: Option<unit_patrol::UnitPatrol>,
player_pitch_bits: u32,
+ player_gait: player_gait::PlayerGait,
boarding: Option<boarding::Boarding>,
local_hero: Option<ObjectHandle>,
script_host_mode: MissionMode,
@@ -2811,6 +2814,7 @@ node_provider_distribution_suspended: false,
construction,
unit_patrol,
player_pitch_bits: 0,
+ player_gait: Default::default(),
boarding: None,
local_hero,
script_host_mode: request.mode,
@@ -2909,6 +2913,7 @@ pub fn step_headless(
}
if let Some(mission) = &mut engine.loaded {
capture_barrier::advance(mission, &engine.world, queue_time_ms);
+ player_gait::advance(mission, &engine.world, queue_time_ms);
control_resources::tick(mission, &engine.world, queue_time_ms);
}
let (autodemo_combat_events, autodemo_combat_unavailable) =
diff --git a/crates/fparkan-runtime/src/player_gait.rs b/crates/fparkan-runtime/src/player_gait.rs
new file mode 100644
index 00000000..3b293d9a
--- /dev/null
+++ b/crates/fparkan-runtime/src/player_gait.rs
@@ -0,0 +1,78 @@
+//! First-person motion of the walking hero's camera.
+
+use crate::{Engine, LoadedMissionState, StartingCockpit};
+use fparkan_world::{transform_state, World};
+
+const WALK_CYCLE_MS: f32 = 448.0;
+const CAMERA_LIFT: f32 = 0.15;
+
+#[derive(Default)]
+pub(super) struct PlayerGait {
+ previous: Option<(u32, [u32; 2])>,
+ phase: f32,
+ lift: f32,
+}
+
+impl PlayerGait {
+ fn advance(&mut self, clock_ms: u32, xy: [u32; 2]) {
+ let Some((previous_ms, previous_xy)) = self.previous.replace((clock_ms, xy)) else {
+ return;
+ };
+ let elapsed_ms = clock_ms.wrapping_sub(previous_ms).min(500) as f32;
+ if previous_xy == xy {
+ let decay = (1.0 - elapsed_ms / (WALK_CYCLE_MS / 4.0)).clamp(0.0, 1.0);
+ self.lift *= decay;
+ if self.lift == 0.0 {
+ self.phase = 0.0;
+ }
+ return;
+ }
+ // shortcut: a sine matches the observed main component of the walk;
+ // the native camera track and its response to movement speed are open.
+ self.phase = (self.phase + elapsed_ms / WALK_CYCLE_MS).fract();
+ self.lift = CAMERA_LIFT * (self.phase * std::f32::consts::TAU).sin();
+ }
+}
+
+pub(super) fn advance(mission: &mut LoadedMissionState, world: &World, now: u32) {
+ let pose = mission
+ .local_hero
+ .filter(|_| mission.boarding.is_none())
+ .and_then(|hero| transform_state(world, hero).ok());
+ if let Some(pose) = pose {
+ let [x, y, _] = pose.position;
+ mission.player_gait.advance(now, [x, y]);
+ } else {
+ mission.player_gait = PlayerGait::default();
+ }
+}
+
+/// Vertical movement of the local hero's camera at the latest simulation tick.
+pub fn player_walk_camera_lift(engine: &Engine, cockpit: StartingCockpit) -> f32 {
+ engine
+ .loaded
+ .as_ref()
+ .filter(|mission| mission.local_hero == Some(cockpit.handle) && mission.boarding.is_none())
+ .map_or(0.0, |mission| mission.player_gait.lift)
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn camera_oscillates_only_after_walk_displaces_the_hero() {
+ let mut gait = PlayerGait::default();
+ gait.advance(0, [0, 0]);
+ gait.advance(112, [0, 0]);
+ assert_eq!(gait.lift, 0.0);
+ gait.advance(224, [1, 0]);
+ assert_eq!(gait.lift, CAMERA_LIFT);
+ gait.advance(448, [3, 0]);
+ assert_eq!(gait.lift, -CAMERA_LIFT);
+ gait.advance(504, [3, 0]);
+ assert_eq!(gait.lift, -CAMERA_LIFT * 0.5);
+ gait.advance(616, [3, 0]);
+ assert_eq!(gait.lift, 0.0);
+ }
+}