| | | 1 | | using UnityEngine; |
| | | 2 | | using TMPro; |
| | | 3 | | using UnityEngine.XR.Interaction.Toolkit; |
| | | 4 | | using Random = UnityEngine.Random; |
| | | 5 | | using System.Diagnostics.CodeAnalysis; |
| | | 6 | | using BNG; |
| | | 7 | | public class Gun : MonoBehaviour |
| | | 8 | | { |
| | | 9 | | public enum ShootState { |
| | | 10 | | Ready, |
| | | 11 | | Shooting, |
| | | 12 | | Reloading |
| | | 13 | | } |
| | | 14 | | |
| | | 15 | | // The Gun |
| | | 16 | | XRGrabInteractable m_InteractableBase; |
| | | 17 | | |
| | | 18 | | //Gun Colliders |
| | | 19 | | //public Collider Gun_Collider_Physical = null; |
| | | 20 | | |
| | | 21 | | //Magazine Colliders |
| | | 22 | | //public Collider Magazine_Collider_Physical = null; |
| | | 23 | | |
| | | 24 | | // How far forward the muzzle is from the centre of the gun |
| | | 25 | | private float muzzleOffset; |
| | | 26 | | |
| | | 27 | | //MagazineVariables |
| | | 28 | | //public MagazineVariables MagVar; |
| | | 29 | | |
| | | 30 | | //Trigger Functions |
| | | 31 | | float m_TriggerHeldTime; |
| | | 32 | | bool m_TriggerDown; |
| | | 33 | | |
| | | 34 | | //Graphics |
| | | 35 | | public GameObject muzzleFlash; |
| | | 36 | | public TextMeshPro text; |
| | | 37 | | |
| | | 38 | | //Reference |
| | | 39 | | public GameObject GunPov; |
| | | 40 | | |
| | | 41 | | public AudioManager AudioManager; |
| | | 42 | | |
| | | 43 | | [Header("Magazine")] |
| | | 44 | | public GameObject round; |
| | | 45 | | public int ammunition; |
| | | 46 | | |
| | | 47 | | [Range(0.5f, 10)] public float reloadTime; |
| | | 48 | | |
| | | 49 | | private int remainingAmmunition; |
| | | 50 | | |
| | | 51 | | [Header("Shooting")] |
| | | 52 | | // How many shots the gun can make per second |
| | | 53 | | [Range(0.25f, 25)] public float fireRate; |
| | | 54 | | |
| | | 55 | | // The number of rounds fired each shot |
| | | 56 | | public int roundsPerShot; |
| | | 57 | | |
| | | 58 | | [Range(0.5f, 100)] public float roundSpeed; |
| | | 59 | | |
| | | 60 | | // The maximum angle that the bullet's direction can vary, |
| | | 61 | | // in both the horizontal and vertical axes |
| | | 62 | | [Range(0, 45)] public float maxRoundVariation; |
| | | 63 | | |
| | 14 | 64 | | private ShootState shootState = ShootState.Ready; |
| | | 65 | | |
| | | 66 | | // The next time that the gun is able to shoot at |
| | 14 | 67 | | private float nextShootTime = 0; |
| | | 68 | | |
| | 7 | 69 | | void Start() { |
| | | 70 | | //muzzleOffset = attackPoint.GetComponent<Renderer>().bounds.extents.z; |
| | 7 | 71 | | remainingAmmunition = ammunition; |
| | | 72 | | |
| | 7 | 73 | | AudioManager = FindObjectOfType<AudioManager>(); |
| | 7 | 74 | | } |
| | | 75 | | |
| | 25193 | 76 | | void Update() { |
| | 25193 | 77 | | switch(shootState) { |
| | | 78 | | case ShootState.Shooting: |
| | | 79 | | // If the gun is ready to shoot again... |
| | 88 | 80 | | if(Time.time > nextShootTime) { |
| | 10 | 81 | | shootState = ShootState.Ready; |
| | 10 | 82 | | } |
| | 78 | 83 | | break; |
| | | 84 | | case ShootState.Reloading: |
| | | 85 | | // If the gun has finished reloading... |
| | 1389 | 86 | | if(Time.time > nextShootTime) { |
| | 5 | 87 | | remainingAmmunition = ammunition; |
| | 5 | 88 | | shootState = ShootState.Ready; |
| | 5 | 89 | | } |
| | 1384 | 90 | | break; |
| | | 91 | | } |
| | | 92 | | |
| | | 93 | | //m_Animator = GetComponent<Animator>(); //(For Animations Down the line) |
| | 25193 | 94 | | m_InteractableBase = GetComponent<XRGrabInteractable>(); |
| | 25193 | 95 | | m_InteractableBase.selectExited.AddListener(DroppedGun); |
| | 25193 | 96 | | m_InteractableBase.activated.AddListener(TriggerPulled); |
| | 25193 | 97 | | m_InteractableBase.deactivated.AddListener(TriggerReleased); |
| | | 98 | | |
| | | 99 | | //Show leftover bullets |
| | 25193 | 100 | | text.SetText(remainingAmmunition + " / " + ammunition); |
| | | 101 | | |
| | | 102 | | //update if holding trigger |
| | | 103 | | //if (m_TriggerDown) |
| | | 104 | | //{ |
| | | 105 | | // m_TriggerHeldTime += Time.deltaTime; |
| | | 106 | | // if (m_TriggerHeldTime >= k_HeldThreshold) |
| | | 107 | | // { |
| | | 108 | | // if (!muzzleFlash.isPlaying) |
| | | 109 | | // { |
| | | 110 | | // muzzleFlash.Play(); |
| | | 111 | | // } |
| | | 112 | | // } |
| | | 113 | | // } |
| | 25193 | 114 | | } |
| | | 115 | | |
| | | 116 | | /// Attempts to fire the gun |
| | 13 | 117 | | public void Shoot() { |
| | | 118 | | // Checks that the gun is ready to shoot |
| | 25 | 119 | | if(shootState == ShootState.Ready) { |
| | 168 | 120 | | for(int i = 0; i < roundsPerShot; i++) { |
| | | 121 | | |
| | 48 | 122 | | AudioManager.Play("Gun Fire"); |
| | | 123 | | // Instantiates the round at the muzzle position |
| | 48 | 124 | | GameObject spawnedRound = Instantiate(round, GunPov.transform.position, GunPov.transform.rotation); |
| | | 125 | | //GameObject muzzleflash = Instantiate(muzzleFlash, GunPov.transform.position, GunPov.transform.rotation |
| | | 126 | | |
| | | 127 | | // Add a random variation to the round's direction |
| | 48 | 128 | | spawnedRound.transform.Rotate(new Vector3( |
| | | 129 | | Random.Range(-1f, 1f) * maxRoundVariation, |
| | | 130 | | Random.Range(-1f, 1f) * maxRoundVariation, |
| | | 131 | | 0 |
| | | 132 | | )); |
| | | 133 | | |
| | 48 | 134 | | Rigidbody rb = spawnedRound.GetComponent<Rigidbody>(); |
| | 48 | 135 | | rb.velocity = spawnedRound.transform.forward * roundSpeed; |
| | 48 | 136 | | } |
| | | 137 | | |
| | 12 | 138 | | remainingAmmunition--; |
| | 22 | 139 | | if(remainingAmmunition > 0) { |
| | 10 | 140 | | nextShootTime = Time.time + (1 / fireRate); |
| | 10 | 141 | | shootState = ShootState.Shooting; |
| | 12 | 142 | | } else { |
| | 2 | 143 | | Reload(); |
| | 2 | 144 | | } |
| | 12 | 145 | | } |
| | 13 | 146 | | } |
| | | 147 | | |
| | | 148 | | /// Attempts to reload the gun |
| | 6 | 149 | | public void Reload() { |
| | | 150 | | // Checks that the gun is ready to be reloaded |
| | 11 | 151 | | if(shootState == ShootState.Ready) { |
| | 5 | 152 | | nextShootTime = Time.time + reloadTime; |
| | 5 | 153 | | shootState = ShootState.Reloading; |
| | 5 | 154 | | text.SetText("Reloading"); |
| | 5 | 155 | | AudioManager.Play("Reload"); |
| | 5 | 156 | | } |
| | 6 | 157 | | } |
| | | 158 | | |
| | | 159 | | //Animating Trigger |
| | | 160 | | public void TriggerReleased(DeactivateEventArgs args) |
| | 11321 | 161 | | { |
| | | 162 | | //m_Animator.SetTrigger(k_AnimTriggerUp); |
| | 11321 | 163 | | m_TriggerDown = false; |
| | 11321 | 164 | | m_TriggerHeldTime = 0f; |
| | 11321 | 165 | | } |
| | | 166 | | |
| | | 167 | | |
| | | 168 | | //Animating Trigger |
| | | 169 | | public void TriggerPulled(ActivateEventArgs args) |
| | 15440 | 170 | | { |
| | | 171 | | //m_Animator.SetTrigger(k_AnimTriggerDown); |
| | 15440 | 172 | | m_TriggerDown = true; |
| | 15440 | 173 | | } |
| | | 174 | | |
| | | 175 | | |
| | | 176 | | // In case the gun is dropped while in use. |
| | | 177 | | public void DroppedGun(SelectExitEventArgs args) |
| | 11321 | 178 | | { |
| | | 179 | | //m_Animator.SetTrigger(k_AnimTriggerUp); |
| | 11321 | 180 | | m_TriggerDown = false; |
| | 11321 | 181 | | m_TriggerHeldTime = 0f; |
| | 11321 | 182 | | } |
| | | 183 | | |
| | | 184 | | |
| | | 185 | | // void OnTriggerEnter(Collider magazine) |
| | | 186 | | // { |
| | | 187 | | // if (magazine.gameObject.CompareTag("Magazine")) |
| | | 188 | | // { |
| | | 189 | | // magazine.gameObject.GetComponent<MagazineVariables>().ChangeMagVar(); |
| | | 190 | | // } |
| | | 191 | | // } |
| | | 192 | | |
| | | 193 | | |
| | | 194 | | //Events that happen when the magazine is inserted |
| | | 195 | | public void MagazineInsert() |
| | 0 | 196 | | { |
| | | 197 | | |
| | | 198 | | |
| | | 199 | | //Magvar |
| | | 200 | | //MagVar.ChangeStats(); |
| | | 201 | | |
| | | 202 | | //reload gun |
| | | 203 | | //Reload(); |
| | | 204 | | |
| | | 205 | | //Message |
| | | 206 | | //Debug.Log("Magazine Has Been Inserted"); |
| | | 207 | | |
| | | 208 | | |
| | | 209 | | //Enable Colliders for Magazine |
| | | 210 | | //Gun_Collider_Physical.enabled = false; |
| | | 211 | | |
| | | 212 | | //Disable Magazine Colliders |
| | | 213 | | //Magazine_Collider_Physical.enabled = false; |
| | | 214 | | |
| | | 215 | | //Reload(); |
| | | 216 | | |
| | 0 | 217 | | } |
| | | 218 | | |
| | | 219 | | |
| | | 220 | | //Events that happen when the magazine is inserted |
| | | 221 | | public void MagazineExit() |
| | 0 | 222 | | { |
| | | 223 | | //Magvar other |
| | | 224 | | //MagVar.RemoveMag(); |
| | | 225 | | |
| | | 226 | | //Wait For Colliders to appear again |
| | | 227 | | //Thread.Sleep(100); |
| | | 228 | | |
| | | 229 | | //Enable Colliders for Magazine |
| | | 230 | | //Gun_Collider_Physical.enabled = true; |
| | | 231 | | |
| | | 232 | | //Enable Colliders for Magazine |
| | | 233 | | // Magazine_Collider_Physical.enabled = true; |
| | | 234 | | |
| | | 235 | | //Message |
| | | 236 | | //Debug.Log("Magazine Has Been Removed"); |
| | 0 | 237 | | } |
| | | 238 | | |
| | | 239 | | |
| | | 240 | | } |