Struct ChargeAction

Source
pub struct ChargeAction { /* private fields */ }
Expand description

A fixed-size charge state machine; stepping does not allocate or queue input.

Implementations§

Source§

impl ChargeAction

Source

pub fn new( minimum: f32, maximum: f32, recovery: f32, ) -> Result<ChargeAction, LoopError>

Source

pub fn power(&self) -> f32

Source

pub fn charging(&self) -> bool

Source

pub fn cooldown_seconds(&self) -> f32

Remaining recovery in simulation seconds. Reading does not advance time. See Self::step_edges for an example.

Source

pub fn cancel(&mut self)

Discards charge progress without a release, preserving recovery. A subsequent held sample may start charging again when recovery ends.

Source

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);
Source

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.

Source

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:

  1. Age preexisting recovery by dt. Only its nonnegative remainder max(dt - old_cooldown, 0) is available for held charging.
  2. If already charging and released || !held, resolve that old charge using only previously accumulated duration, with no current dt. Meeting minimum within the legacy 1e-7 second tolerance emits its capped power and starts a fresh, unaged recovery. Otherwise report ChargeRelease::CanceledTooShort without starting recovery.
  3. 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 large dt.
  4. If finally unheld, pressed and no old charge was resolved, a ready action resolves a zero-duration tap. This includes both edges with held == 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

Source§

fn clone(&self) -> ChargeAction

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for ChargeAction

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

impl Copy for ChargeAction

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.