Firearm.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using QFSW.MOP2;
  5. using Sirenix.OdinInspector;
  6. using KairoEngine.Core;
  7. using KairoEngine.Inventory;
  8. using KairoEngine.SFX;
  9. namespace KairoEngine.CharacterSystem
  10. {
  11. public class Firearm : MonoBehaviour
  12. {
  13. [ShowInInspector]
  14. public ItemFirearmRef itemFirearmRef;
  15. public Transform barrel;
  16. public Transform aim;
  17. public Transform grip;
  18. public float camShakeTime = 0.2f;
  19. public float camShakeIntensity = 0.3f;
  20. private Transform direction;
  21. [HideInInspector]
  22. public CharacterController character;
  23. [ReadOnly] public bool isAutomatic = false;
  24. [ReadOnly] public float firingSpeed = 1f;
  25. [ReadOnly] public float soundIntensity = 10f;
  26. [ReadOnly] public GameObject bulletPrefab;
  27. [ReadOnly] public bool isReady = true;
  28. private float counter = 0f;
  29. private ItemBaseFirearm weapon ;
  30. private int burstCurrentBullet = 0;
  31. private string bulletPrefabPool;
  32. private bool playerCharacter = false;
  33. private void Start()
  34. {
  35. character = gameObject.GetComponentInParent<CharacterController>();
  36. if(itemFirearmRef == null) return;
  37. ItemBase item = itemFirearmRef.item;
  38. weapon = (ItemBaseFirearm)item;
  39. if(weapon != null)
  40. {
  41. isAutomatic = weapon.isAutomatic;
  42. firingSpeed = weapon.firingSpeed;
  43. bulletPrefab = weapon.ammoType.bulletPrefab;
  44. bulletPrefabPool = weapon.ammoType.bulletPrefabPool;
  45. }
  46. CharacterAnimator characterAnimator = gameObject.GetComponentInParent<CharacterAnimator>();
  47. if(characterAnimator!= null) direction = characterAnimator.transform;
  48. if(character.unique_name == "player-character_") playerCharacter = true;
  49. else playerCharacter = false;
  50. }
  51. private void Update()
  52. {
  53. if(character == null || transform.parent == null) return;
  54. CoolDown();
  55. Burst();
  56. }
  57. private void CoolDown()
  58. {
  59. if (!isReady)
  60. {
  61. counter += Time.deltaTime;
  62. if (counter >= firingSpeed)
  63. {
  64. isReady = true;
  65. counter = 0f;
  66. }
  67. }
  68. }
  69. public void Fire()
  70. {
  71. if (isReady)
  72. {
  73. isReady = false;
  74. burstCurrentBullet += 1;
  75. if(itemFirearmRef.ammo < 1)
  76. {
  77. // No ammo
  78. SoundController.EmmitSound(weapon.emptySound, transform.position);
  79. }
  80. else
  81. {
  82. // Remove bullet from magazine
  83. itemFirearmRef.ammo -= 1;
  84. // Instantiate Bullets
  85. for (int i = 0; i < weapon.bulletsPerShot; i++)
  86. {
  87. GameObject obj = null;
  88. if(bulletPrefabPool != "") obj = MasterObjectPooler.Instance.GetPool(bulletPrefabPool).GetObject();
  89. else obj = Instantiate(bulletPrefab);
  90. Bullet bullet = obj.GetComponent<Bullet>();
  91. bullet.Initialize(barrel, itemFirearmRef, character, bulletPrefabPool);
  92. }
  93. // Create sound
  94. SoundController.EmmitSound(weapon.shootSound, transform.position);
  95. // Shake Camera
  96. if(playerCharacter) GenericEvents.Trigger("CameraShake", camShakeIntensity, camShakeTime);
  97. }
  98. }
  99. }
  100. private void Burst()
  101. {
  102. if(burstCurrentBullet == weapon.burstFire)
  103. {
  104. // Burst end
  105. burstCurrentBullet = 0;
  106. }
  107. if(weapon.burstFire > 1)
  108. {
  109. if(burstCurrentBullet > 0)
  110. {
  111. Fire();
  112. }
  113. }
  114. }
  115. }
  116. }