WeaponSwapScript.cs 3.59 KB
Newer Older
RyanLindsay's avatar
RyanLindsay committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
using System;
using UnityEngine;

/*  
 * This WeaponSwapScript makes sure that, if the player wants to swap weapons,
 * they can swap weapons!  
 * 
 * WARNING:  If we want to add more weapons, we have to modify this script to 
 * accomodate for swapping weapons with the number buttons. Currently, it can
 * swap through 3 weapons.
 */
public class WeaponSwapScript : MonoBehaviour
{

    public int selectedWeapon = 0;

    //  Start makes sure a weapon is selected at the start of a level
    void Start()
    {
        SelectWeapon();
    }

    // Update is called once per frame.
    // Update checks to see if either:
    //   -the scroll wheel is changing what weapon is active
    //   -the number buttons (not the num pad) are changing what weapon is active
    void Update()
    {
        //This is used at the bottom of Update to check and make sure that the 
        //current weapon is the same as the newly selected weapon.
        //If they're different, then the current weapon will update.
        int previousSelectedWeapon = selectedWeapon;

        //When the scroll wheel goes up.
        if (Input.GetAxis("Mouse ScrollWheel") > 0f)
        {
            //If the selected weapon is already at the top of list, 
            //then it'll wrap around to the bottom of the list. Else, keep 
            //going up the list of weapons.
            if (selectedWeapon >= transform.childCount - 1)
                selectedWeapon = 0;
            else
                selectedWeapon++;
        }

        //When the scroll wheel goes down.
        if (Input.GetAxis("Mouse ScrollWheel") < 0)
        {
            //If the selected weaopn is already at the bottom of the list, 
            //then it'll wrap around to the top of the list. Else, keep
            //going down the list of weapons.
            if (selectedWeapon <= 0)
                selectedWeapon = transform.childCount - 1;
            else
                selectedWeapon--;
        }


        //These will alllow the nubmer buttons to change what weapon you're using.
        //WARNING: If we plan to add more weapons and want them to be able to swap with them
        //in game, then we must add to this!!!!
        if (Input.GetKeyDown(KeyCode.Alpha1))
        {
            selectedWeapon = 0;
        }

        if (Input.GetKeyDown(KeyCode.Alpha2) && transform.childCount >= 2)
        {
            selectedWeapon = 1;
        }

        if (Input.GetKeyDown(KeyCode.Alpha3) && transform.childCount >= 3)
        {
            selectedWeapon = 2;
        }



        //This calls SelectWeapon to change weapons
        if (previousSelectedWeapon != selectedWeapon)
        {
            SelectWeapon();
            AddAmmo(selectedWeapon, 22);
        }
    }

    //This is what actually changes the weapons.  It loops through all of the weapons
    //and either toggles the weapon active or not active
    void SelectWeapon()
    {
        int x = 0; //the 'iterator' of the foreach loop
        foreach (Transform weapon in transform)
        {
            if (x == selectedWeapon)//if this is the weapon the player wants, then it becomes true.
                weapon.gameObject.SetActive(true);
            else //if this weapon is NOT what the player wants, then it becomes false.
                weapon.gameObject.SetActive(false);
            x++;
        }
    }

    public void AddAmmo(int ammoType, int ammoToGivePlayer)
    {
        int x = 0;
        foreach (Transform weapon in transform)
        {
            if(x == ammoType)
            {
                weapon.GetComponent<GunScript>().ammo+=ammoToGivePlayer;
            }
            x++;
        }
    }
}