Created
November 20, 2017 18:22
-
-
Save LeviVisser/46f8ab1efa3e1de7aaea70955065ae3f to your computer and use it in GitHub Desktop.
Weapon base class
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.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
public class Weapon : WeaponSystem { | |
/** | |
* Fobject stands for "Firing object" | |
* This can be a projectile like a laser and missile | |
* Or an other generic "non projectile" object like an EMP | |
* */ | |
public List<GameObject> weaponObjects; | |
public GameObject FObject; | |
public EWeaponType weaponType; | |
private bool isFiring; // Necessary??? | |
private bool canFire; | |
private float regenerationTime; | |
private float reloadTime; | |
private float cooldownTime; | |
private bool weaponsActive; | |
[Range(.1f, 10f)] | |
public float Delay; | |
void Start() | |
{ | |
isFiring = false; | |
canFire = true; | |
} | |
// Update is called once per frame | |
void Update () { | |
if (!weaponsActive) return; | |
if (canFire && Input.GetKey(KeyBinds.FireWeapon)) | |
{ | |
Fire(FObject); | |
} | |
} | |
/// <summary> | |
/// A timed function that disables and enables the weapon after a set Delay | |
/// </summary> | |
/// <returns></returns> | |
private IEnumerator CooldownWeapons() { | |
canFire = false; | |
yield return new WaitForSeconds(Delay); | |
canFire = true; | |
} | |
/// <summary> | |
/// Fires the projectile in the direction the weapon is facing | |
/// </summary> | |
/// <param name="projectile">The projectile that needs to be fired</param> | |
private void Fire(GameObject projectile) | |
{ | |
isFiring = true; | |
StartCoroutine(CooldownWeapons()); | |
foreach (GameObject obj in weaponObjects) { | |
Instantiate(projectile, new Vector3(obj.transform.position.x, obj.transform.position.y, obj.transform.position.z), transform.rotation); | |
} | |
isFiring = false; | |
} | |
/// <summary> | |
/// Activates all the weapon's owned gameobjects; | |
/// </summary> | |
public void Activate() { | |
weaponsActive = true; | |
} | |
/// <summary> | |
/// Deactivates all the weapon's owned game objects | |
/// </summary> | |
public void Deactivate() { | |
weaponsActive = false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment