Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using UnityEngine.UI;
- using UnityEngine;
- public class BaseWeapon : MonoBehaviour
- {
- //BURNSRED CODE/////////////////////////////////////////////////////////////////////////////
- public SO_BaseWeapon baseWeapon;
- private enum GunType { Semi, Burst, Auto, Shotgun };
- private GunType gunType;
- private string weaponName;
- private string weaponDescription;
- private int damage;
- private float accuracy;
- private float range;
- private int maxAmmo;
- private int currentAmmo;
- private float reloadTime;
- private float roundsPerMinute;
- private Rigidbody shell;
- private Image weaponSprite;
- private AudioClip fireClip;
- private AudioClip reloadClip;
- private AudioClip tracerClip;
- private AudioClip EmptyClip;
- ///////////////////////////////////////////////////////////////////////////////
- //OLD CODE
- /*
- public enum GunType { Semi, Burst, Auto, Shotgun };
- public GunType gunType;
- public bool isPistol;
- public bool isRifle;
- public int damage;
- public float range;
- public int maxAmmo;
- public int currentAmmo;
- public float reloadTime;
- private float secondsBetweenShots;
- private float nextShootTime;
- private bool isReloading = false;
- public float roundsPerMinute;
- public Transform weaponBarrel;
- public Transform shellEject;
- public Rigidbody shell;
- private AudioSource gunAudio;
- public AudioClip fireClip;
- public AudioClip reloadClip;
- public AudioClip tracerClip;
- public AudioClip EmptyClip;
- private LineRenderer tracer;
- public Text currentAmmoText;
- public Text maxAmmoText;
- public Slider ammoSlider;
- public Image weaponImage;
- public Sprite weaponSprite;
- public GameObject AmmoCounter;
- public GameObject reloadNotification;
- */
- //LEGACY CODE
- //feat Harbo.
- public bool isPistol;
- public bool isRifle;
- private float secondsBetweenShots;
- private float nextShootTime;
- private bool isReloading = false;
- public Transform weaponBarrel;
- public Transform shellEject;
- private AudioSource gunAudio;
- private LineRenderer tracer;
- public Text currentAmmoText;
- public Text maxAmmoText;
- public Slider ammoSlider;
- public Image weaponImage;
- public Sprite weaponSprite;
- public GameObject AmmoCounter;
- public GameObject reloadNotification;
- ////////////////////////////////////////////////////////////////////
- // Use this for initialization
- void Start()
- {
- //RED CODE//////////////////////////////////////////////////////////////////////////
- /*
- ░░█▀░░░░░░░░░░░▀▀███████░░░░
- ░░█▌░░░░░░░░░░░░░░░▀██████░░░
- ░█▌░░░░░░░░░░░░░░░░███████▌░░
- ░█░░░░░░░░░░░░░░░░░████████░░
- ▐▌░░░░░░░░░░░░░░░░░▀██████▌░░
- ░▌▄███▌░░░░▀████▄░░░░▀████▌░░
- ▐▀▀▄█▄░▌░░░▄██▄▄▄▀░░░░████▄▄░
- ▐░▀░░═▐░░░░░░══░░▀░░░░▐▀░▄▀▌▌
- ▐░░░░░▌░░░░░░░░░░░░░░░▀░▀░░▌▌
- ▐░░░▄▀░░░▀░▌░░░░░░░░░░░░▌█░▌▌
- ░▌░░▀▀▄▄▀▀▄▌▌░░░░░░░░░░▐░▀▐▐░
- ░▌░░▌░▄▄▄▄░░░▌░░░░░░░░▐░░▀▐░░
- ░█░▐▄██████▄░▐░░░░░░░░█▀▄▄▀░░
- ░▐░▌▌░░░░░░▀▀▄▐░░░░░░█▌░░░░░░
- ░░█░░▄▀▀▀▀▄░▄═╝▄░░░▄▀░▌░░░░░░
- ░░░▌▐░░░░░░▌░▀▀░░▄▀░░▐░░░░░░░
- ░░░▀▄░░░░░░░░░▄▀▀░░░░█░░░░░░░
- ░░░▄█▄▄▄▄▄▄▄▀▀░░░░░░░▌▌░░░░░░
- ░░▄▀▌▀▌░░░░░░░░░░░░░▄▀▀▄░░░░░
- ▄▀░░▌░▀▄░░░░░░░░░░▄▀░░▌░▀▄░░*/
- gunType = baseWeapon.gunType;
- weaponName = baseWeapon.weaponName;
- weaponDescription = baseWeapon.weaponDescription;
- damage = baseWeapon.damage;
- accuracy = baseWeapon.accuracy;
- range = baseWeapon.range;
- maxAmmo = baseWeapon.maxAmmo;
- currentAmmo = baseWeapon.currentAmmo;
- reloadTime = baseWeapon.reloadTime;
- roundsPerMinute = baseWeapon.roundsPerMinute;
- shell = baseWeapon.shell;
- weaponSprite = baseWeapon.weaponSprite;
- fireClip = baseWeapon.fireClip;
- reloadClip = baseWeapon.reloadClip;
- tracerClip = baseWeapon.tracerClip;
- EmptyClip = baseWeapon.EmptyClip;
- ////////////////////////////////////////////////////////////////////////////////////
- // Setup UI elements
- currentAmmoText.text = currentAmmo.ToString();
- maxAmmoText.text = maxAmmo.ToString();
- ammoSlider.maxValue = maxAmmo;
- ammoSlider.value = currentAmmo;
- weaponImage.sprite = weaponSprite;
- secondsBetweenShots = 60 / roundsPerMinute;
- gunAudio = GetComponent<AudioSource>();
- AmmoCounter.SetActive(true);
- reloadNotification.SetActive(false);
- if (GetComponent<LineRenderer>())
- {
- tracer = GetComponent<LineRenderer>();
- }
- }
- // Update is called once per frame
- void Update()
- {
- /* if(currentAmmo <= 0)
- {
- StartCoroutine(reload());
- return;
- } */
- // Update UI elements
- currentAmmoText.text = currentAmmo.ToString();
- maxAmmoText.text = maxAmmo.ToString();
- currentAmmoText.text = currentAmmo.ToString();
- maxAmmoText.text = maxAmmo.ToString();
- ammoSlider.maxValue = maxAmmo;
- ammoSlider.value = currentAmmo;
- weaponImage.sprite = weaponSprite;
- if (currentAmmo <= 0)
- {
- // Set Ammo counter to hidden and show reload notification
- AmmoCounter.SetActive(false);
- reloadNotification.SetActive(true);
- }
- else
- {
- AmmoCounter.SetActive(true);
- reloadNotification.SetActive(false);
- // reverse above
- }
- }
- public void Shoot()
- {
- if (CanShoot())
- {
- if (isReloading == false)
- {
- Ray ray = new Ray(weaponBarrel.position, weaponBarrel.forward);
- RaycastHit hit;
- range = 20;
- if (Physics.Raycast(ray, out hit, range))
- {
- Debug.Log(hit.transform.name);
- range = hit.distance;
- if (hit.collider.tag == "Enemy")
- {
- // Try and find an EnemyHealth script on the gameobject hit.
- EnemyHealth enemyHealth = hit.collider.GetComponent<EnemyHealth>();
- // If the EnemyHealth component exist...
- if (enemyHealth != null)
- {
- // ... the enemy should take damage.
- enemyHealth.TakeDamage(damage, hit.point);
- }
- }
- }
- nextShootTime = Time.time + secondsBetweenShots;
- currentAmmo--;
- Debug.Log("Rounds Remaining : " + currentAmmo + " Out of" + maxAmmo);
- gunAudio.clip = fireClip;
- gunAudio.Play();
- if (tracer)
- {
- StartCoroutine("RenderTracer", ray.direction * range);
- }
- Rigidbody newShell = Instantiate(shell, shellEject.position, Quaternion.identity) as Rigidbody;
- newShell.AddForce(shellEject.forward * Random.Range(150f, 200f) + weaponBarrel.forward * Random.Range(-10f, 10f));
- Debug.DrawRay(ray.origin, ray.direction * range, Color.red);
- }
- }
- }
- public void FullAutoShoot()
- {
- if (gunType == GunType.Auto)
- {
- Shoot();
- }
- }
- public void BurstFire()
- {
- if (gunType == GunType.Burst)
- {
- }
- }
- private bool CanShoot()
- {
- bool CanShoot = true;
- if (Time.time < nextShootTime) { CanShoot = false; }
- if (currentAmmo == 0)
- {
- CanShoot = false;
- }
- return CanShoot;
- }
- IEnumerator reload()
- {
- isReloading = true;
- gunAudio.clip = reloadClip;
- gunAudio.Play();
- yield return new WaitForSeconds(reloadTime);
- currentAmmo = maxAmmo;
- isReloading = false;
- }
- public void Reload()
- {
- StartCoroutine(reload());
- }
- IEnumerator RenderTracer(Vector3 hitPoint)
- {
- tracer.enabled = true;
- tracer.SetPosition(0, weaponBarrel.position);
- tracer.SetPosition(1, weaponBarrel.position + hitPoint);
- yield return null;
- tracer.enabled = false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement