Struct Magazine

Source
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

Source

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.

Source

pub fn capacity(&self) -> u16

Maximum loaded rounds. See the module example.

Source

pub fn loaded(&self) -> u16

Source

pub fn reserve(&self) -> u32

Source

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.

Source

pub fn reloading(&self) -> bool

Source

pub fn remaining(&self) -> f32

Source

pub fn can_fire(&self) -> bool

Whether a call to Self::fire would accept a shot in the current state. See the module example.

Source

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.

Source

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.

Source

pub fn fire(&mut self) -> bool

Source

pub fn reload(&mut self) -> bool

Source

pub fn cancel_reload(&mut self)

Source

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.

Source

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

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.

Source

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

Trait Implementations§

Source§

impl Clone for Magazine

Source§

fn clone(&self) -> Magazine

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 Magazine

Source§

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

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

impl Copy for Magazine

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.