pub struct Magazine { /* private fields */ }Expand description
Bounded ammunition storage and a caller-driven reload timer.
Reloads and capacity changes conserve Self::total_ammo. Only an accepted
shot or explicit reserve addition/removal changes that total after creation.
Implementations§
Source§impl Magazine
impl Magazine
Sourcepub fn new(
capacity: u16,
reserve: u32,
reload_seconds: f32,
) -> Result<Magazine, LoopError>
pub fn new( capacity: u16, reserve: u32, reload_seconds: f32, ) -> Result<Magazine, LoopError>
Creates a full magazine with additional reserve ammunition.
Capacity must be in 1..=4096, reserve at most 1_000_000, and reload
duration finite and in 0.0 < reload_seconds <= 3600.0. Invalid inputs
return LoopError::Invalid.
Sourcepub fn capacity(&self) -> u16
pub fn capacity(&self) -> u16
Maximum loaded rounds. See the module example.
pub fn loaded(&self) -> u16
pub fn reserve(&self) -> u32
Sourcepub fn total_ammo(&self) -> u32
pub fn total_ammo(&self) -> u32
Loaded plus reserve rounds, including during a reload. This is bounded
by 1_004_096. See the module example.
pub fn reloading(&self) -> bool
pub fn remaining(&self) -> f32
Sourcepub fn can_fire(&self) -> bool
pub fn can_fire(&self) -> bool
Whether a call to Self::fire would accept a shot in the current state.
See the module example.
Sourcepub fn can_reload(&self) -> bool
pub fn can_reload(&self) -> bool
Whether a call to Self::reload would start a new reload in the current
state. See the module example.
Sourcepub fn reload_status(&self) -> Option<ReloadStatus>
pub fn reload_status(&self) -> Option<ReloadStatus>
Inspects an active reload without consuming time or transferring ammo.
Returns None before starting, after cancellation, and after completion.
The projected round count uses the current capacity and reserve, without
reserving ammo. See the module example.
pub fn fire(&mut self) -> bool
pub fn reload(&mut self) -> bool
pub fn cancel_reload(&mut self)
Sourcepub fn advance(&mut self, dt: f32) -> Result<bool, LoopError>
pub fn advance(&mut self, dt: f32) -> Result<bool, LoopError>
Returns true exactly once when ammunition transfers at reload completion.
Supply simulation seconds on actual ticks. Invalid deltas (non-finite,
negative, or greater than 3600.0) leave the entire state unchanged.
Sourcepub fn set_capacity(&mut self, capacity: u16) -> Result<u16, LoopError>
pub fn set_capacity(&mut self, capacity: u16) -> Result<u16, LoopError>
Changes capacity while conserving all loaded and reserve ammunition.
Shrinking returns excess loaded rounds to reserve and reports that count. Growing creates empty space without loading or creating rounds. A reload keeps its timing unless the magazine becomes full, which cancels it without a completion event. Setting the same capacity is a no-op.
Returns LoopError::Invalid outside 1..=4096, or
LoopError::Capacity if returned rounds would exceed the reserve bound
of 1_000_000. Either error leaves ammo, capacity and reload untouched.
use ferravex_play::gameplay::{LoopError, magazine::Magazine};
let mut magazine = Magazine::new(6, 10, 1.0)?;
let total = magazine.total_ammo();
assert_eq!(magazine.set_capacity(3)?, 3);
assert_eq!((magazine.loaded(), magazine.reserve()), (3, 13));
assert_eq!(magazine.total_ammo(), total);
assert_eq!(magazine.set_capacity(8)?, 0);
assert_eq!(magazine.loaded(), 3); // Growing does not refill.
assert_eq!(magazine.total_ammo(), total);
assert_eq!(magazine.set_capacity(0), Err(LoopError::Invalid));
assert_eq!(magazine.capacity(), 8);Sourcepub fn add_reserve(&mut self, amount: u32) -> Result<(), LoopError>
pub fn add_reserve(&mut self, amount: u32) -> Result<(), LoopError>
Adds externally supplied rounds to reserve without loading them or
changing reload timing. Increases Self::total_ammo by exactly amount.
Zero is a no-op. Returns LoopError::Capacity without changes on
overflow or if the reserve would exceed 1_000_000 rounds. See
Self::remove_reserve for a transfer example.
Sourcepub fn remove_reserve(&mut self, amount: u32) -> Result<(), LoopError>
pub fn remove_reserve(&mut self, amount: u32) -> Result<(), LoopError>
Removes exactly amount reserve rounds for the caller to transfer or
spend. Decreases Self::total_ammo by that amount; loaded ammo is intact.
Zero is a no-op. Returns LoopError::Unavailable without changes when
reserve is insufficient. Removing the last reserve round cancels any
reload without completion; otherwise reload timing is preserved.
use ferravex_play::gameplay::{LoopError, magazine::Magazine};
let mut magazine = Magazine::new(3, 5, 1.0)?;
assert!(magazine.fire());
assert!(magazine.reload());
assert_eq!(magazine.remove_reserve(6), Err(LoopError::Unavailable));
assert!(magazine.reloading());
magazine.remove_reserve(5)?; // Caller now owns these five rounds.
assert_eq!(magazine.total_ammo(), 2);
assert!(!magazine.advance(1.0)?); // Cancellation is not completion.
magazine.add_reserve(5)?; // Return the rounds without restarting reload.
assert_eq!(magazine.total_ammo(), 7);
assert!(!magazine.reloading());