using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
using UnityEngine;
public class PlayerShoot : MonoBehaviour
{
public float damage = 12;
public float range = 75;
public float fireRate;
public float allAmmo;
public int maxAmmo;
private int currentAmmo;
public float reloadTime;
private bool isReloading = false;
private bool canReload = true;
public Camera fpsCam;
private float nextTimeToFire = 0f;
public bool Automatic;
public Animator animator;
public ParticleSystem Bullet;
private bool canShoot;
public Text gunText;
public float ArAmmo;
public float ShotgunAmmo;
public float PistolAmmo;
public bool isPistol;
public bool isAr;
public bool isShotgun;
public void Start()
{
currentAmmo = maxAmmo;
canShoot = true;
}
void Update()
{
gunText.text = currentAmmo.ToString() + " / " + allAmmo.ToString();
if (isReloading && canShoot)
return;
if(allAmmo <= 0) {
canReload = false;
allAmmo = 0;
}
if (Input.GetKey(KeyCode.LeftControl))
{
canShoot = false;
}
else
{
canShoot = true;
}
if (allAmmo <= maxAmmo && currentAmmo <= 0)
{
canShoot = false;
}
if (Input.GetKeyDown(KeyCode.R) && canReload)
{
StartCoroutine(Reload());
return;
}
if(currentAmmo <= 0 && canReload )
{
StartCoroutine(Reload());
return;
}
if (Input.GetButtonDown(“Fire1”) && Time.time >= nextTimeToFire && !Automatic && canShoot)
{
nextTimeToFire = Time.time + 1f / fireRate;
Shoot();
}
if (Input.GetButton(“Fire1”) && Time.time >= nextTimeToFire && Automatic && canShoot)
{
nextTimeToFire = Time.time + 1f / fireRate;
Shoot();
}
}
IEnumerator Reload()
{
allAmmo -= maxAmmo - currentAmmo;
isReloading = true;
animator.SetBool(“Reloading”, true);
yield return new WaitForSeconds(reloadTime);
currentAmmo = maxAmmo;
isReloading = false;
animator.SetBool(“Reloading”, false);
}
public void Shoot()
{
currentAmmo–;
Bullet.Play();
RaycastHit hit;
if( Physics.Raycast(fpsCam.transform.position, fpsCam.transform.forward, out hit, range)){
EnemyDeath enemy = hit.transform.GetComponent();
if(enemy != null)
{
enemy.TakeDamage(damage);
}
}
}
public void PistolAmmoPickUp(float Pamount)
{
if (isPistol)
{
allAmmo += Pamount;
}
}
public void ShotgunAmmoPickUp(float Samount)
{
if (isShotgun)
{
allAmmo += Samount;
}
}
public void ArAmmoPickUp(float Aamount)
{
if (isAr)
{
allAmmo += Aamount;
}
}
}