Created
December 16, 2014 14:30
-
-
Save DaveVoyles/2d0d9eb99b35ebe3ae6b to your computer and use it in GitHub Desktop.
Switching to the next weapon in a weapon inventory
This file contains hidden or 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
/// <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