Class WeaponState
Mutable state of a weapon instance, tracking magazine, fire mode, and trigger state. Enforces basic weapon invariants (e.g. no firing with empty magazine).
Inherited Members
Namespace: Serenity.GameWeapon.Domain.Entities
Assembly: Serenity.GameWeapon.Domain.dll
Syntax
public class WeaponState
Constructors
WeaponState(WeaponId, MagazineState, FireMode)
Creates a new WeaponState with an infinite ammo reserve.
Declaration
public WeaponState(WeaponId id, MagazineState magazine, FireMode fireMode)
Parameters
| Type | Name | Description |
|---|---|---|
| WeaponId | id | The weapon identity. |
| MagazineState | magazine | The initial magazine state. |
| FireMode | fireMode | The initial fire mode. |
WeaponState(WeaponId, MagazineState, FireMode, AmmoReserveState)
Creates a new WeaponState with the specified parameters.
Declaration
public WeaponState(WeaponId id, MagazineState magazine, FireMode fireMode, AmmoReserveState reserve)
Parameters
| Type | Name | Description |
|---|---|---|
| WeaponId | id | The weapon identity. |
| MagazineState | magazine | The initial magazine state. |
| FireMode | fireMode | The initial fire mode. |
| AmmoReserveState | reserve | The initial ammo reserve state. |
Properties
Cadence
Gets the current fire cadence, which gates TryFire(float).
Declaration
public FireCadence Cadence { get; }
Property Value
| Type | Description |
|---|---|
| FireCadence |
FireMode
Gets or sets the current fire mode.
Declaration
public FireMode FireMode { get; set; }
Property Value
| Type | Description |
|---|---|
| FireMode |
Id
Gets the identity of this weapon.
Declaration
public WeaponId Id { get; }
Property Value
| Type | Description |
|---|---|
| WeaponId |
IsReloading
Gets whether the weapon is currently reloading.
Declaration
public bool IsReloading { get; }
Property Value
| Type | Description |
|---|---|
| bool |
IsTriggerPressed
Gets whether the trigger is currently pressed.
Declaration
public bool IsTriggerPressed { get; }
Property Value
| Type | Description |
|---|---|
| bool |
Magazine
Gets the current magazine state.
Declaration
public MagazineState Magazine { get; }
Property Value
| Type | Description |
|---|---|
| MagazineState |
Reserve
Gets the current ammo reserve state.
Declaration
public AmmoReserveState Reserve { get; }
Property Value
| Type | Description |
|---|---|
| AmmoReserveState |
Methods
ReloadCompleted()
Completes an in-progress reload, filling the magazine to full.
Declaration
public void ReloadCompleted()
ReloadCompleted(int)
Completes an in-progress reload, withdrawing the loaded rounds from Reserve and adding them to the magazine.
Declaration
public void ReloadCompleted(int ammoLoaded)
Parameters
| Type | Name | Description |
|---|---|---|
| int | ammoLoaded | The number of rounds requested from the reserve, capped at the magazine's max capacity. A negative value requests a full refill instead. With an infinite reserve, a negative value fills the magazine to full directly, matching the pre-reserve behavior exactly. |
ReloadRequested()
Requests a reload. Returns true if reload was initiated.
Declaration
public bool ReloadRequested()
Returns
| Type | Description |
|---|---|
| bool | True if reload started (not already reloading and not full); otherwise false. |
SetReserve(int, int)
Reconfigures the ammo reserve, replacing the current one.
Declaration
public void SetReserve(int maxCapacity, int currentAmmo)
Parameters
| Type | Name | Description |
|---|---|---|
| int | maxCapacity | The maximum reserve capacity. A negative value means infinite. |
| int | currentAmmo | The initial reserve ammo. Ignored when the reserve is infinite. |
ToSnapshot()
Creates an immutable snapshot of the current weapon state.
Declaration
public WeaponSnapshot ToSnapshot()
Returns
| Type | Description |
|---|---|
| WeaponSnapshot | A read-only snapshot of the weapon state. |
TriggerPressed()
Presses the trigger. Returns true if firing is allowed.
Declaration
public bool TriggerPressed()
Returns
| Type | Description |
|---|---|
| bool | True if the weapon can fire (not empty, not reloading); otherwise false. |
TriggerReleased()
Releases the trigger.
Declaration
public void TriggerReleased()
TryFire(float)
Attempts to fire at the given time, gated by reload state, ammo, and Cadence.
Declaration
public bool TryFire(float nowSeconds)
Parameters
| Type | Name | Description |
|---|---|---|
| float | nowSeconds | The current time, in seconds, supplied by the caller. |
Returns
| Type | Description |
|---|---|
| bool | True if a round was consumed and the shot time was recorded; false when reloading, the
magazine is empty, or Cadence blocks firing at |