ferravex_play/gameplay/
platforms.rs

1//! Reusable platform mechanisms; hosts provide collision-confirmed contact edges.
2use super::{LoopError, seconds};
3#[derive(Clone, Copy, Debug)]
4pub struct LaunchPad {
5    speed: f32,
6    delay: f64,
7    remaining: f64,
8    contact: bool,
9}
10impl LaunchPad {
11    pub fn new(speed: f32, delay: f32) -> Result<Self, LoopError> {
12        let delay = seconds(delay)?;
13        if !speed.is_finite() || !(0.01..=1e5).contains(&speed) {
14            return Err(LoopError::Invalid);
15        }
16        Ok(Self {
17            speed,
18            delay,
19            remaining: 0.,
20            contact: false,
21        })
22    }
23    /// Returns an authored upward speed on a fresh accepted landing. Continuous
24    /// contact cannot retrigger, even after cooldown. Hosts apply the velocity.
25    pub fn step(&mut self, dt: f32, contact: bool) -> Result<Option<f32>, LoopError> {
26        self.remaining = (self.remaining - seconds(dt)?).max(0.);
27        let fire = contact && !self.contact && self.remaining == 0.;
28        self.contact = contact;
29        if fire {
30            self.remaining = self.delay;
31            Ok(Some(self.speed))
32        } else {
33            Ok(None)
34        }
35    }
36}
37#[cfg(test)]
38mod tests {
39    use super::*;
40    #[test]
41    fn launch_requires_a_new_landing_and_respects_cooldown() {
42        let mut p = LaunchPad::new(8., 0.5).unwrap();
43        assert_eq!(p.step(0., true).unwrap(), Some(8.));
44        assert_eq!(p.step(1., true).unwrap(), None);
45        p.step(0., false).unwrap();
46        assert_eq!(p.step(0., true).unwrap(), Some(8.));
47        p.step(0., false).unwrap();
48        assert_eq!(p.step(0.1, true).unwrap(), None);
49        assert!(p.step(f32::NAN, false).is_err());
50    }
51}
52
53#[derive(Clone, Copy, Debug, PartialEq, Eq)]
54pub enum PlatformPhase {
55    Solid,
56    Warning,
57    Absent,
58}
59/// A timed platform that disappears after contact, then regrows. Hosts exclude
60/// absent surfaces from collision and use phase/progress to telegraph the change.
61#[derive(Clone, Copy, Debug)]
62pub struct CrumblePlatform {
63    warning: f64,
64    regrow: f64,
65    remaining: f64,
66    phase: PlatformPhase,
67}
68impl CrumblePlatform {
69    pub fn new(warning: f32, regrow: f32) -> Result<Self, LoopError> {
70        let warning = seconds(warning)?;
71        let regrow = seconds(regrow)?;
72        if warning == 0. || regrow == 0. {
73            return Err(LoopError::Invalid);
74        }
75        Ok(Self {
76            warning,
77            regrow,
78            remaining: 0.,
79            phase: PlatformPhase::Solid,
80        })
81    }
82    pub fn phase(&self) -> PlatformPhase {
83        self.phase
84    }
85    pub fn solid(&self) -> bool {
86        self.phase != PlatformPhase::Absent
87    }
88    pub fn remaining(&self) -> f32 {
89        self.remaining as f32
90    }
91    /// At most two transitions per step; regrowth never rearms on stale contact
92    /// in the same update. Invalid time leaves the platform unchanged.
93    pub fn step(&mut self, dt: f32, contact: bool) -> Result<(), LoopError> {
94        let mut dt = seconds(dt)?;
95        if self.phase == PlatformPhase::Solid && contact {
96            self.phase = PlatformPhase::Warning;
97            self.remaining = self.warning;
98        }
99        if self.phase == PlatformPhase::Warning {
100            let used = dt.min(self.remaining);
101            self.remaining -= used;
102            dt -= used;
103            if self.remaining <= 1e-7 {
104                self.phase = PlatformPhase::Absent;
105                self.remaining = self.regrow;
106            }
107        }
108        if self.phase == PlatformPhase::Absent {
109            self.remaining = (self.remaining - dt).max(0.);
110            if self.remaining <= 1e-7 {
111                self.phase = PlatformPhase::Solid;
112                self.remaining = 0.;
113            }
114        }
115        Ok(())
116    }
117    pub fn reset(&mut self) {
118        self.phase = PlatformPhase::Solid;
119        self.remaining = 0.;
120    }
121}
122#[cfg(test)]
123mod crumble_tests {
124    use super::*;
125    #[test]
126    fn contact_telegraphs_collapse_and_absence_regrows_without_frame_dependence() {
127        let mut a = CrumblePlatform::new(0.5, 2.).unwrap();
128        let mut b = a;
129        a.step(1., true).unwrap();
130        b.step(0., true).unwrap();
131        for _ in 0..4 {
132            b.step(0.25, false).unwrap();
133        }
134        assert_eq!(a.phase(), PlatformPhase::Absent);
135        assert_eq!(a.phase(), b.phase());
136        assert_eq!(a.remaining(), b.remaining());
137        a.step(1.5, false).unwrap();
138        assert!(a.solid());
139        assert_eq!(a.phase(), PlatformPhase::Solid);
140        a.step(0.1, true).unwrap();
141        assert!(a.step(f32::NAN, false).is_err());
142        assert_eq!(a.phase(), PlatformPhase::Warning);
143        a.reset();
144        assert_eq!(a.phase(), PlatformPhase::Solid);
145    }
146}