GlobalGameJame/Assets/Scripts/WeaponManager.cs

94 lines
2.9 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class WeaponManager : MonoBehaviour
{
public enum WeaponType { None, Shoot, Meele, Throw };
public WeaponType weaponType = WeaponType.None;
public float weaponRange = 1;
public GameObject spawnShoot;
public GameObject particleShoot;
public float damage = 0;
public float fireRate = 0.5f;
private float fireTime;
public int ammoCost = 2;
public Animator animator;
public PlayerManager player;
public bool hit = false;
public AudioSource audioSource = new AudioSource();
public List<AudioClip> shootSound = new List<AudioClip>();
// Start is called before the first frame update
void Start()
{
fireTime = fireRate;
}
// Update is called once per frame
void Update()
{
WeaponAttack();
}
public void WeaponAttack()
{
if (Input.GetMouseButtonDown(0) && fireTime <= Time.time) {
if (weaponType == WeaponType.Shoot)
{
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]);
}
player.ammo -= ammoCost;
Destroy(shoot, 0.3f);
fireTime = fireRate + Time.time;
}
}
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;
}
}
else if (weaponType == WeaponType.Meele)
{
hit = true;
animator.SetTrigger("Hit");
fireTime = fireRate + Time.time;
}
}
if (fireTime <= Time.time)
{
if (animator != null) {
animator.ResetTrigger("Hit");
}
hit = false;
}
}
public void Hit_Enemy_Start ()
{
hit = true;
}
public void Hit_Enemy_End ()
{
hit = false;
}
}