Commit a0c9489d authored by RyanLindsay's avatar RyanLindsay

Works with the AmmoPickup

parent 379ca7c1
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++;
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment