Skip to content

Instantly share code, notes, and snippets.

@jweimann
Created November 14, 2016 05:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jweimann/2e8b22330679b292b2229ada9e150d11 to your computer and use it in GitHub Desktop.
Save jweimann/2e8b22330679b292b2229ada9e150d11 to your computer and use it in GitHub Desktop.
using System;
public class WeaponAmmo
{
private int _ammoNotInClip;
private int _ammoInClip;
private int _maxAmmoPerClip;
private bool HasAmmoReady { get { return _ammoInClip > 0; } }
public WeaponAmmo(WeaponSettings weaponSettings)
{
_ammoNotInClip = weaponSettings.StartingAmmo;
_maxAmmoPerClip = weaponSettings.AmmoPerClip;
Reload();
}
public void Reload()
{
int spaceRemaining = _maxAmmoPerClip - _ammoInClip;
int desiredAmmoToMove = Math.Min(_maxAmmoPerClip, spaceRemaining);
int amountToMove = Math.Min(desiredAmmoToMove, _ammoNotInClip);
if (amountToMove > 0)
{
_ammoNotInClip -= amountToMove;
_ammoInClip += amountToMove;
}
}
public bool TryTakeBullet()
{
if (HasAmmoReady == false)
return false;
_ammoInClip--;
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment