GlobalGameJame/Assets/Scripts/WeaponManager.cs

94 lines
2.9 KiB
C#
Raw Normal View History

2020-01-31 23:53:03 +00:00
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class WeaponManager : MonoBehaviour
{
public enum WeaponType { None, Shoot, Meele, Throw };
2020-01-31 23:53:03 +00:00
public WeaponType weaponType = WeaponType.None;
public float weaponRange = 1;
2020-02-01 05:04:53 +00:00
public GameObject spawnShoot;
public GameObject particleShoot;
2020-01-31 23:53:03 +00:00
public float damage = 0;
2020-02-01 05:04:53 +00:00
public float fireRate = 0.5f;
private float fireTime;
2020-02-02 11:23:38 +00:00
public int ammoCost = 2;
2020-02-01 17:17:29 +00:00
public Animator animator;
2020-02-01 18:49:53 +00:00
public PlayerManager player;
2020-02-01 17:17:29 +00:00
public bool hit = false;
2020-01-31 23:53:03 +00:00
2020-02-01 19:58:47 +00:00
public AudioSource audioSource = new AudioSource();
public List<AudioClip> shootSound = new List<AudioClip>();
2020-02-01 19:58:47 +00:00
2020-01-31 23:53:03 +00:00
// Start is called before the first frame update
void Start()
{
2020-02-01 05:04:53 +00:00
fireTime = fireRate;
2020-01-31 23:53:03 +00:00
}
// Update is called once per frame
void Update()
{
2020-02-01 05:04:53 +00:00
WeaponAttack();
2020-01-31 23:53:03 +00:00
}
2020-02-01 05:04:53 +00:00
public void WeaponAttack()
2020-01-31 23:53:03 +00:00
{
2020-02-01 05:04:53 +00:00
if (Input.GetMouseButtonDown(0) && fireTime <= Time.time) {
if (weaponType == WeaponType.Shoot)
2020-01-31 23:53:03 +00:00
{
2020-02-02 11:23:38 +00:00
if (particleShoot != null && player.ammo > (ammoCost - 1))
2020-02-01 05:04:53 +00:00
{
GameObject shoot = Instantiate(particleShoot, spawnShoot.transform.position, spawnShoot.transform.rotation);
if (shootSound.Count > 0)
{
int rand = Random.Range(0, shootSound.Count);
audioSource.PlayOneShot(shootSound[rand]);
}
2020-02-02 11:23:38 +00:00
player.ammo -= ammoCost;
2020-02-01 05:04:53 +00:00
Destroy(shoot, 0.3f);
2020-02-01 18:49:53 +00:00
fireTime = fireRate + Time.time;
2020-02-01 05:04:53 +00:00
}
2020-01-31 23:53:03 +00:00
}
else if (weaponType == WeaponType.Throw)
{
if (particleShoot != null && player.ammo > (ammoCost - 1))
{
GameObject shoot = Instantiate(particleShoot, spawnShoot.transform.position, spawnShoot.transform.rotation);
if (shootSound.Count > 0)
{
int rand = Random.Range(0, shootSound.Count);
audioSource.PlayOneShot(shootSound[rand]);
}
shoot.GetComponent<AmmoManager>().weaponManager = this;
player.ammo -= ammoCost;
fireTime = fireRate + Time.time;
}
}
2020-02-01 05:04:53 +00:00
else if (weaponType == WeaponType.Meele)
{
2020-02-01 21:54:57 +00:00
hit = true;
2020-02-01 17:17:29 +00:00
animator.SetTrigger("Hit");
2020-02-01 18:49:53 +00:00
fireTime = fireRate + Time.time;
2020-02-01 05:04:53 +00:00
}
2020-01-31 23:53:03 +00:00
}
2020-02-01 21:54:57 +00:00
if (fireTime <= Time.time)
{
if (animator != null) {
animator.ResetTrigger("Hit");
}
2020-02-01 21:54:57 +00:00
hit = false;
}
2020-02-01 17:17:29 +00:00
}
public void Hit_Enemy_Start ()
{
hit = true;
}
public void Hit_Enemy_End ()
{
hit = false;
}
2020-01-31 23:53:03 +00:00
}