pub struct ChargeAction { /* private fields */ }Expand description
A fixed-size charge state machine; stepping does not allocate or queue input.
Implementations§
Source§impl ChargeAction
impl ChargeAction
pub fn new( minimum: f32, maximum: f32, recovery: f32, ) -> Result<ChargeAction, LoopError>
pub fn power(&self) -> f32
pub fn charging(&self) -> bool
Sourcepub fn cooldown_seconds(&self) -> f32
pub fn cooldown_seconds(&self) -> f32
Remaining recovery in simulation seconds. Reading does not advance time.
See Self::step_edges for an example.
Sourcepub fn cancel(&mut self)
pub fn cancel(&mut self)
Discards charge progress without a release, preserving recovery. A subsequent held sample may start charging again when recovery ends.
Sourcepub fn reset(&mut self)
pub fn reset(&mut self)
Discards charge and recovery, preserving constructor configuration.
Also clear pending host input on restart; this helper owns no input map.
use ferravex_play::gameplay::charge::ChargeAction;
let mut charge = ChargeAction::new(0.25, 1.0, 0.5)?;
charge.step(1.0, true)?;
charge.step(0.0, false)?;
charge.cancel(); // Does not bypass recovery.
assert_eq!(charge.cooldown_seconds(), 0.5);
charge.reset();
assert_eq!(charge.cooldown_seconds(), 0.0);
assert!(!charge.charging());
assert_eq!(charge.power(), 0.0);Sourcepub fn step(&mut self, dt: f32, held: bool) -> Result<Option<f32>, LoopError>
pub fn step(&mut self, dt: f32, held: bool) -> Result<Option<f32>, LoopError>
A release emits at most one normalized power value. Continued holds cap at full charge. Hosts commit resource costs only for returned releases.
Sourcepub fn step_edges(
&mut self,
dt: f32,
held: bool,
pressed: bool,
released: bool,
) -> Result<ChargeStep, LoopError>
pub fn step_edges( &mut self, dt: f32, held: bool, pressed: bool, released: bool, ) -> Result<ChargeStep, LoopError>
Steps one aggregate snapshot, retaining held, pressed and released
independently. All eight flag combinations are accepted. Invalid dt
(nonfinite or outside 0.0..=3600.0) returns LoopError::Invalid
before any mutation.
Deterministic processing order and duration attribution:
- Age preexisting recovery by
dt. Only its nonnegative remaindermax(dt - old_cooldown, 0)is available for held charging. - If already charging and
released || !held, resolve that old charge using only previously accumulated duration, with no currentdt. Meetingminimumwithin the legacy1e-7second tolerance emits its capped power and starts a fresh, unaged recovery. Otherwise reportChargeRelease::CanceledTooShortwithout starting recovery. - If finally held and recovery is zero, start/continue charging with the
available duration. Thus release/repress (
held && pressed && released) may return an old release plus a new charge. Positive fresh recovery blocks that new charge for this entire snapshot, even for largedt. - If finally unheld,
pressedand no old charge was resolved, a ready action resolves a zero-duration tap. This includes both edges withheld == false, and also pressed-only with final unheld. A positive minimum exceeding the tolerance cancels it; minimum zero emits zero power. No second tap is resolved after closing an old charge.
A held level can start a charge without pressed (including when recovery
expires exactly at the end of dt, with zero progress). pressed alone
never resets an existing held charge. released with no existing charge
is ignored; a final held level can still start one. Zero dt still
processes edges and minimum/recovery rules. Preexisting recovery expiring
within dt admits a final hold with only the remainder, or a zero-duration
tap with no elapsed charge credit. Fresh recovery is never aged here.
With pressed == released == false, emitted values and final state match
Self::step exactly. Extra transitions aggregated into the same flags
cannot be counted or timed. The caller must consume edges only once on an
actual tick; replaying the same edge snapshot is a new request.
Self::cancel drops progress but keeps recovery; Self::reset clears
both. Neither clears pending host input.
use ferravex_play::gameplay::charge::{ChargeAction, ChargeRelease};
let mut charge = ChargeAction::new(0.25, 1.0, 0.0)?;
charge.step_edges(0.75, true, true, false)?;
let report = charge.step_edges(0.25, true, true, true)?;
assert_eq!(report.release, ChargeRelease::Emitted(0.75));
assert!(report.charging); // A new charge, not the old released charge.
assert_eq!(report.power, 0.25);
assert_eq!(report.cooldown_seconds, charge.cooldown_seconds());
let report = charge.step_edges(0.0, false, false, true)?;
assert_eq!(report.release, ChargeRelease::Emitted(0.25));
assert!(!report.charging);
assert_eq!(report.power, 0.0);Trait Implementations§
Source§impl Clone for ChargeAction
impl Clone for ChargeAction
Source§fn clone(&self) -> ChargeAction
fn clone(&self) -> ChargeAction
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more