Skip to content

Instantly share code, notes, and snippets.

@DaveVoyles
Created December 16, 2014 14:30
Show Gist options
  • Save DaveVoyles/2d0d9eb99b35ebe3ae6b to your computer and use it in GitHub Desktop.
Save DaveVoyles/2d0d9eb99b35ebe3ae6b to your computer and use it in GitHub Desktop.
Switching to the next weapon in a weapon inventory
/// <summary>
/// Switch to the next weapon in our inventory
/// </summary>
private void NextWeapon()
{
// Store the value of the next weapon
currentWeaponIndex ++;
// Reached the end of the array, start from the beginning
if (currentWeaponIndex >= weaponInventory.Length)
{
currentWeaponIndex = 0;
BulletPreset = (BulletPresetType)currentWeaponIndex;
print("Returning to beginning of array");
}
// Loop through the array and grab the next item that is in my inventory
for (int i = currentWeaponIndex; i < weaponInventory.Length; i++)
{
// If we have the weapon in our inventory....
if (weaponInventory[i])
{
BulletPreset = (BulletPresetType)i;
print("Bullet preset type in array is: " + BulletPreset + "CurrentWeaponIndex: " + currentWeaponIndex);
// Exit the loop, we found a weapon
break;
}
// If we can't find a weapon the first time, keep looping through inventory until we can
while (!weaponInventory[i])
{
currentWeaponIndex++;
print("No more weapons. CurrentWeaponIndex: " + currentWeaponIndex);
// Reached the end of the array, start from the beginning
if (currentWeaponIndex >= weaponInventory.Length)
{
currentWeaponIndex = 0;
BulletPreset = (BulletPresetType)currentWeaponIndex;
print("Returning to beginning of array");
}
// Exit while loop, keep iterating through array until a weapon is found
break;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment