Created
November 14, 2016 05:22
-
-
Save jweimann/2e8b22330679b292b2229ada9e150d11 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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