Advertisement
Guest User

Untitled

a guest
Mar 20th, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.52 KB | None | 0 0
  1. using System.Collections;
  2. using UnityEngine.UI;
  3. using UnityEngine;
  4.  
  5. public class BaseWeapon : MonoBehaviour
  6. {
  7.  
  8. //BURNSRED CODE/////////////////////////////////////////////////////////////////////////////
  9. public SO_BaseWeapon baseWeapon;
  10.  
  11. private enum GunType { Semi, Burst, Auto, Shotgun };
  12. private GunType gunType;
  13.  
  14. private string weaponName;
  15. private string weaponDescription;
  16.  
  17. private int damage;
  18. private float accuracy;
  19. private float range;
  20.  
  21. private int maxAmmo;
  22. private int currentAmmo;
  23. private float reloadTime;
  24.  
  25. private float roundsPerMinute;
  26.  
  27. private Rigidbody shell;
  28.  
  29. private Image weaponSprite;
  30.  
  31. private AudioClip fireClip;
  32. private AudioClip reloadClip;
  33. private AudioClip tracerClip;
  34. private AudioClip EmptyClip;
  35. ///////////////////////////////////////////////////////////////////////////////
  36.  
  37.  
  38.  
  39. //OLD CODE
  40. /*
  41. public enum GunType { Semi, Burst, Auto, Shotgun };
  42. public GunType gunType;
  43.  
  44. public bool isPistol;
  45. public bool isRifle;
  46.  
  47. public int damage;
  48. public float range;
  49.  
  50. public int maxAmmo;
  51. public int currentAmmo;
  52. public float reloadTime;
  53.  
  54. private float secondsBetweenShots;
  55. private float nextShootTime;
  56. private bool isReloading = false;
  57.  
  58. public float roundsPerMinute;
  59.  
  60. public Transform weaponBarrel;
  61. public Transform shellEject;
  62.  
  63. public Rigidbody shell;
  64.  
  65. private AudioSource gunAudio;
  66. public AudioClip fireClip;
  67. public AudioClip reloadClip;
  68. public AudioClip tracerClip;
  69. public AudioClip EmptyClip;
  70.  
  71. private LineRenderer tracer;
  72.  
  73.  
  74. public Text currentAmmoText;
  75. public Text maxAmmoText;
  76. public Slider ammoSlider;
  77. public Image weaponImage;
  78. public Sprite weaponSprite;
  79.  
  80. public GameObject AmmoCounter;
  81. public GameObject reloadNotification;
  82. */
  83.  
  84. //LEGACY CODE
  85. //feat Harbo.
  86. public bool isPistol;
  87. public bool isRifle;
  88.  
  89. private float secondsBetweenShots;
  90. private float nextShootTime;
  91. private bool isReloading = false;
  92.  
  93. public Transform weaponBarrel;
  94. public Transform shellEject;
  95.  
  96. private AudioSource gunAudio;
  97.  
  98. private LineRenderer tracer;
  99.  
  100.  
  101. public Text currentAmmoText;
  102. public Text maxAmmoText;
  103. public Slider ammoSlider;
  104. public Image weaponImage;
  105. public Sprite weaponSprite;
  106.  
  107. public GameObject AmmoCounter;
  108. public GameObject reloadNotification;
  109. ////////////////////////////////////////////////////////////////////
  110.  
  111.  
  112. // Use this for initialization
  113. void Start()
  114. {
  115.  
  116. //RED CODE//////////////////////////////////////////////////////////////////////////
  117. /*
  118. ░░█▀░░░░░░░░░░░▀▀███████░░░░
  119. ░░█▌░░░░░░░░░░░░░░░▀██████░░░
  120. ░█▌░░░░░░░░░░░░░░░░███████▌░░
  121. ░█░░░░░░░░░░░░░░░░░████████░░
  122. ▐▌░░░░░░░░░░░░░░░░░▀██████▌░░
  123. ░▌▄███▌░░░░▀████▄░░░░▀████▌░░
  124. ▐▀▀▄█▄░▌░░░▄██▄▄▄▀░░░░████▄▄░
  125. ▐░▀░░═▐░░░░░░══░░▀░░░░▐▀░▄▀▌▌
  126. ▐░░░░░▌░░░░░░░░░░░░░░░▀░▀░░▌▌
  127. ▐░░░▄▀░░░▀░▌░░░░░░░░░░░░▌█░▌▌
  128. ░▌░░▀▀▄▄▀▀▄▌▌░░░░░░░░░░▐░▀▐▐░
  129. ░▌░░▌░▄▄▄▄░░░▌░░░░░░░░▐░░▀▐░░
  130. ░█░▐▄██████▄░▐░░░░░░░░█▀▄▄▀░░
  131. ░▐░▌▌░░░░░░▀▀▄▐░░░░░░█▌░░░░░░
  132. ░░█░░▄▀▀▀▀▄░▄═╝▄░░░▄▀░▌░░░░░░
  133. ░░░▌▐░░░░░░▌░▀▀░░▄▀░░▐░░░░░░░
  134. ░░░▀▄░░░░░░░░░▄▀▀░░░░█░░░░░░░
  135. ░░░▄█▄▄▄▄▄▄▄▀▀░░░░░░░▌▌░░░░░░
  136. ░░▄▀▌▀▌░░░░░░░░░░░░░▄▀▀▄░░░░░
  137. ▄▀░░▌░▀▄░░░░░░░░░░▄▀░░▌░▀▄░░*/
  138. gunType = baseWeapon.gunType;
  139. weaponName = baseWeapon.weaponName;
  140. weaponDescription = baseWeapon.weaponDescription;
  141.  
  142. damage = baseWeapon.damage;
  143. accuracy = baseWeapon.accuracy;
  144. range = baseWeapon.range;
  145.  
  146. maxAmmo = baseWeapon.maxAmmo;
  147. currentAmmo = baseWeapon.currentAmmo;
  148. reloadTime = baseWeapon.reloadTime;
  149.  
  150. roundsPerMinute = baseWeapon.roundsPerMinute;
  151.  
  152. shell = baseWeapon.shell;
  153.  
  154. weaponSprite = baseWeapon.weaponSprite;
  155.  
  156. fireClip = baseWeapon.fireClip;
  157. reloadClip = baseWeapon.reloadClip;
  158. tracerClip = baseWeapon.tracerClip;
  159. EmptyClip = baseWeapon.EmptyClip;
  160. ////////////////////////////////////////////////////////////////////////////////////
  161.  
  162. // Setup UI elements
  163. currentAmmoText.text = currentAmmo.ToString();
  164. maxAmmoText.text = maxAmmo.ToString();
  165.  
  166. ammoSlider.maxValue = maxAmmo;
  167. ammoSlider.value = currentAmmo;
  168.  
  169. weaponImage.sprite = weaponSprite;
  170.  
  171. secondsBetweenShots = 60 / roundsPerMinute;
  172. gunAudio = GetComponent<AudioSource>();
  173.  
  174. AmmoCounter.SetActive(true);
  175. reloadNotification.SetActive(false);
  176.  
  177. if (GetComponent<LineRenderer>())
  178. {
  179. tracer = GetComponent<LineRenderer>();
  180. }
  181.  
  182. }
  183.  
  184. // Update is called once per frame
  185. void Update()
  186. {
  187. /* if(currentAmmo <= 0)
  188. {
  189. StartCoroutine(reload());
  190. return;
  191. } */
  192.  
  193. // Update UI elements
  194. currentAmmoText.text = currentAmmo.ToString();
  195. maxAmmoText.text = maxAmmo.ToString();
  196.  
  197. currentAmmoText.text = currentAmmo.ToString();
  198. maxAmmoText.text = maxAmmo.ToString();
  199.  
  200. ammoSlider.maxValue = maxAmmo;
  201. ammoSlider.value = currentAmmo;
  202.  
  203. weaponImage.sprite = weaponSprite;
  204.  
  205. if (currentAmmo <= 0)
  206. {
  207. // Set Ammo counter to hidden and show reload notification
  208. AmmoCounter.SetActive(false);
  209. reloadNotification.SetActive(true);
  210. }
  211. else
  212. {
  213. AmmoCounter.SetActive(true);
  214. reloadNotification.SetActive(false);
  215. // reverse above
  216. }
  217.  
  218.  
  219. }
  220.  
  221. public void Shoot()
  222. {
  223. if (CanShoot())
  224. {
  225. if (isReloading == false)
  226. {
  227. Ray ray = new Ray(weaponBarrel.position, weaponBarrel.forward);
  228. RaycastHit hit;
  229.  
  230. range = 20;
  231.  
  232. if (Physics.Raycast(ray, out hit, range))
  233. {
  234. Debug.Log(hit.transform.name);
  235.  
  236. range = hit.distance;
  237.  
  238. if (hit.collider.tag == "Enemy")
  239. {
  240. // Try and find an EnemyHealth script on the gameobject hit.
  241. EnemyHealth enemyHealth = hit.collider.GetComponent<EnemyHealth>();
  242.  
  243. // If the EnemyHealth component exist...
  244. if (enemyHealth != null)
  245. {
  246. // ... the enemy should take damage.
  247. enemyHealth.TakeDamage(damage, hit.point);
  248. }
  249. }
  250.  
  251. }
  252.  
  253. nextShootTime = Time.time + secondsBetweenShots;
  254.  
  255. currentAmmo--;
  256.  
  257. Debug.Log("Rounds Remaining : " + currentAmmo + " Out of" + maxAmmo);
  258.  
  259. gunAudio.clip = fireClip;
  260. gunAudio.Play();
  261.  
  262. if (tracer)
  263. {
  264. StartCoroutine("RenderTracer", ray.direction * range);
  265. }
  266.  
  267. Rigidbody newShell = Instantiate(shell, shellEject.position, Quaternion.identity) as Rigidbody;
  268. newShell.AddForce(shellEject.forward * Random.Range(150f, 200f) + weaponBarrel.forward * Random.Range(-10f, 10f));
  269.  
  270. Debug.DrawRay(ray.origin, ray.direction * range, Color.red);
  271. }
  272. }
  273.  
  274. }
  275.  
  276. public void FullAutoShoot()
  277. {
  278. if (gunType == GunType.Auto)
  279. {
  280. Shoot();
  281. }
  282. }
  283.  
  284. public void BurstFire()
  285. {
  286. if (gunType == GunType.Burst)
  287. {
  288. }
  289. }
  290.  
  291. private bool CanShoot()
  292. {
  293. bool CanShoot = true;
  294.  
  295. if (Time.time < nextShootTime) { CanShoot = false; }
  296. if (currentAmmo == 0)
  297. {
  298. CanShoot = false;
  299. }
  300.  
  301. return CanShoot;
  302. }
  303.  
  304. IEnumerator reload()
  305. {
  306. isReloading = true;
  307.  
  308. gunAudio.clip = reloadClip;
  309. gunAudio.Play();
  310.  
  311. yield return new WaitForSeconds(reloadTime);
  312. currentAmmo = maxAmmo;
  313. isReloading = false;
  314. }
  315.  
  316. public void Reload()
  317. {
  318. StartCoroutine(reload());
  319. }
  320.  
  321. IEnumerator RenderTracer(Vector3 hitPoint)
  322. {
  323. tracer.enabled = true;
  324. tracer.SetPosition(0, weaponBarrel.position);
  325. tracer.SetPosition(1, weaponBarrel.position + hitPoint);
  326.  
  327. yield return null;
  328. tracer.enabled = false;
  329. }
  330. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement