| | | 1 | | using UnityEngine; |
| | | 2 | | using System.Threading; |
| | | 3 | | using TMPro; |
| | | 4 | | using UnityEngine; |
| | | 5 | | using System.Collections; |
| | | 6 | | using UnityEngine.XR.Interaction.Toolkit; |
| | | 7 | | using Random = UnityEngine.Random; |
| | | 8 | | using VRExplorer; |
| | | 9 | | using System.Diagnostics.CodeAnalysis; |
| | | 10 | | using BNG; |
| | | 11 | | |
| | | 12 | | public class Gun : MonoBehaviour, IGrabbableEntity, ITriggerableEntity |
| | | 13 | | { |
| | | 14 | | [ExcludeFromCodeCoverage] public float TriggeringTime => 2.5f; |
| | | 15 | | [ExcludeFromCodeCoverage] public string Name => Str.Triggerable; |
| | | 16 | | |
| | | 17 | | [ExcludeFromCodeCoverage] |
| | | 18 | | public void Triggerring() |
| | | 19 | | { |
| | | 20 | | var obj = EntityManager.Instance.vrexplorerMono.gameObject; |
| | | 21 | | XRDirectInteractor interactor; |
| | | 22 | | if(!obj.TryGetComponent(out interactor)) |
| | | 23 | | { |
| | | 24 | | interactor = obj.AddComponent<XRDirectInteractor>(); |
| | | 25 | | } |
| | | 26 | | if(!obj.GetComponent<ActionBasedController>()) |
| | | 27 | | { |
| | | 28 | | obj.AddComponent<ActionBasedController>(); |
| | | 29 | | } |
| | | 30 | | var interactable = GetComponent<XRBaseInteractable>(); |
| | | 31 | | var e = new SelectEnterEventArgs() { interactorObject = interactor }; |
| | | 32 | | var h = new HoverEnterEventArgs() { interactorObject = interactor }; |
| | | 33 | | var a = new ActivateEventArgs() { interactorObject = interactor }; |
| | | 34 | | interactable.selectEntered.Invoke(e); |
| | | 35 | | interactable.hoverEntered.Invoke(h); |
| | | 36 | | interactable.firstSelectEntered.Invoke(e); |
| | | 37 | | interactable.firstHoverEntered.Invoke(h); |
| | | 38 | | interactable.activated.Invoke(a); |
| | | 39 | | } |
| | | 40 | | |
| | | 41 | | [ExcludeFromCodeCoverage] |
| | | 42 | | public void Triggerred() |
| | | 43 | | { |
| | | 44 | | var obj = EntityManager.Instance.vrexplorerMono.gameObject; |
| | | 45 | | XRDirectInteractor interactor; |
| | | 46 | | if(!obj.TryGetComponent(out interactor)) |
| | | 47 | | { |
| | | 48 | | interactor = obj.AddComponent<XRDirectInteractor>(); |
| | | 49 | | } |
| | | 50 | | if(!obj.GetComponent<ActionBasedController>()) |
| | | 51 | | { |
| | | 52 | | obj.AddComponent<ActionBasedController>(); |
| | | 53 | | } |
| | | 54 | | var e = new SelectExitEventArgs() { interactorObject = interactor }; |
| | | 55 | | var h = new HoverExitEventArgs() { interactorObject = interactor }; |
| | | 56 | | var a = new DeactivateEventArgs() { interactorObject = interactor }; |
| | | 57 | | var interactable = GetComponent<XRBaseInteractable>(); |
| | | 58 | | interactable.selectExited.Invoke(e); |
| | | 59 | | interactable.hoverExited.Invoke(h); |
| | | 60 | | interactable.lastSelectExited.Invoke(e); |
| | | 61 | | interactable.lastHoverExited.Invoke(h); |
| | | 62 | | interactable.deactivated.Invoke(a); |
| | | 63 | | |
| | | 64 | | } |
| | | 65 | | |
| | | 66 | | [ExcludeFromCodeCoverage] |
| | | 67 | | public Grabbable Grabbable |
| | | 68 | | { |
| | | 69 | | get |
| | | 70 | | { |
| | | 71 | | var g = GetComponent<Grabbable>(); |
| | | 72 | | if(g) return g; |
| | | 73 | | return gameObject.AddComponent<Grabbable>(); |
| | | 74 | | } |
| | | 75 | | } |
| | | 76 | | |
| | | 77 | | [ExcludeFromCodeCoverage] |
| | | 78 | | public void OnGrabbed() |
| | | 79 | | { |
| | | 80 | | } |
| | | 81 | | |
| | | 82 | | public enum ShootState { |
| | | 83 | | Ready, |
| | | 84 | | Shooting, |
| | | 85 | | Reloading |
| | | 86 | | } |
| | | 87 | | |
| | | 88 | | // The Gun |
| | | 89 | | XRGrabInteractable m_InteractableBase; |
| | | 90 | | |
| | | 91 | | //Gun Colliders |
| | | 92 | | //public Collider Gun_Collider_Physical = null; |
| | | 93 | | |
| | | 94 | | //Magazine Colliders |
| | | 95 | | //public Collider Magazine_Collider_Physical = null; |
| | | 96 | | |
| | | 97 | | // How far forward the muzzle is from the centre of the gun |
| | | 98 | | private float muzzleOffset; |
| | | 99 | | |
| | | 100 | | //MagazineVariables |
| | | 101 | | //public MagazineVariables MagVar; |
| | | 102 | | |
| | | 103 | | //Trigger Functions |
| | | 104 | | float m_TriggerHeldTime; |
| | | 105 | | bool m_TriggerDown; |
| | | 106 | | |
| | | 107 | | //Graphics |
| | | 108 | | public GameObject muzzleFlash; |
| | | 109 | | public TextMeshPro text; |
| | | 110 | | |
| | | 111 | | //Reference |
| | | 112 | | public GameObject GunPov; |
| | | 113 | | |
| | | 114 | | public AudioManager AudioManager; |
| | | 115 | | |
| | | 116 | | [Header("Magazine")] |
| | | 117 | | public GameObject round; |
| | | 118 | | public int ammunition; |
| | | 119 | | |
| | | 120 | | [Range(0.5f, 10)] public float reloadTime; |
| | | 121 | | |
| | | 122 | | private int remainingAmmunition; |
| | | 123 | | |
| | | 124 | | [Header("Shooting")] |
| | | 125 | | // How many shots the gun can make per second |
| | | 126 | | [Range(0.25f, 25)] public float fireRate; |
| | | 127 | | |
| | | 128 | | // The number of rounds fired each shot |
| | | 129 | | public int roundsPerShot; |
| | | 130 | | |
| | | 131 | | [Range(0.5f, 100)] public float roundSpeed; |
| | | 132 | | |
| | | 133 | | // The maximum angle that the bullet's direction can vary, |
| | | 134 | | // in both the horizontal and vertical axes |
| | | 135 | | [Range(0, 45)] public float maxRoundVariation; |
| | | 136 | | |
| | 35 | 137 | | private ShootState shootState = ShootState.Ready; |
| | | 138 | | |
| | | 139 | | // The next time that the gun is able to shoot at |
| | 35 | 140 | | private float nextShootTime = 0; |
| | | 141 | | |
| | 28 | 142 | | void Start() { |
| | | 143 | | //muzzleOffset = attackPoint.GetComponent<Renderer>().bounds.extents.z; |
| | 28 | 144 | | remainingAmmunition = ammunition; |
| | | 145 | | |
| | 28 | 146 | | AudioManager = FindObjectOfType<AudioManager>(); |
| | 28 | 147 | | } |
| | | 148 | | |
| | 86387 | 149 | | void Update() { |
| | 86387 | 150 | | switch(shootState) { |
| | | 151 | | case ShootState.Shooting: |
| | | 152 | | // If the gun is ready to shoot again... |
| | 176 | 153 | | if(Time.time > nextShootTime) { |
| | 16 | 154 | | shootState = ShootState.Ready; |
| | 16 | 155 | | } |
| | 160 | 156 | | break; |
| | | 157 | | case ShootState.Reloading: |
| | | 158 | | // If the gun has finished reloading... |
| | 0 | 159 | | if(Time.time > nextShootTime) { |
| | 0 | 160 | | remainingAmmunition = ammunition; |
| | 0 | 161 | | shootState = ShootState.Ready; |
| | 0 | 162 | | } |
| | 0 | 163 | | break; |
| | | 164 | | } |
| | | 165 | | |
| | | 166 | | //m_Animator = GetComponent<Animator>(); //(For Animations Down the line) |
| | 86387 | 167 | | m_InteractableBase = GetComponent<XRGrabInteractable>(); |
| | 86387 | 168 | | m_InteractableBase.selectExited.AddListener(DroppedGun); |
| | 86387 | 169 | | m_InteractableBase.activated.AddListener(TriggerPulled); |
| | 86387 | 170 | | m_InteractableBase.deactivated.AddListener(TriggerReleased); |
| | | 171 | | |
| | | 172 | | //Show leftover bullets |
| | 86387 | 173 | | text.SetText(remainingAmmunition + " / " + ammunition); |
| | | 174 | | |
| | | 175 | | //update if holding trigger |
| | | 176 | | //if (m_TriggerDown) |
| | | 177 | | //{ |
| | | 178 | | // m_TriggerHeldTime += Time.deltaTime; |
| | | 179 | | // if (m_TriggerHeldTime >= k_HeldThreshold) |
| | | 180 | | // { |
| | | 181 | | // if (!muzzleFlash.isPlaying) |
| | | 182 | | // { |
| | | 183 | | // muzzleFlash.Play(); |
| | | 184 | | // } |
| | | 185 | | // } |
| | | 186 | | // } |
| | 86387 | 187 | | } |
| | | 188 | | |
| | | 189 | | /// Attempts to fire the gun |
| | 16 | 190 | | public void Shoot() { |
| | | 191 | | // Checks that the gun is ready to shoot |
| | 32 | 192 | | if(shootState == ShootState.Ready) { |
| | 188 | 193 | | for(int i = 0; i < roundsPerShot; i++) { |
| | | 194 | | |
| | 52 | 195 | | AudioManager.Play("Gun Fire"); |
| | | 196 | | // Instantiates the round at the muzzle position |
| | 52 | 197 | | GameObject spawnedRound = Instantiate(round, GunPov.transform.position, GunPov.transform.rotation); |
| | | 198 | | //GameObject muzzleflash = Instantiate(muzzleFlash, GunPov.transform.position, GunPov.transform.rotation |
| | | 199 | | |
| | | 200 | | // Add a random variation to the round's direction |
| | 52 | 201 | | spawnedRound.transform.Rotate(new Vector3( |
| | | 202 | | Random.Range(-1f, 1f) * maxRoundVariation, |
| | | 203 | | Random.Range(-1f, 1f) * maxRoundVariation, |
| | | 204 | | 0 |
| | | 205 | | )); |
| | | 206 | | |
| | 52 | 207 | | Rigidbody rb = spawnedRound.GetComponent<Rigidbody>(); |
| | 52 | 208 | | rb.velocity = spawnedRound.transform.forward * roundSpeed; |
| | 52 | 209 | | } |
| | | 210 | | |
| | 16 | 211 | | remainingAmmunition--; |
| | 32 | 212 | | if(remainingAmmunition > 0) { |
| | 16 | 213 | | nextShootTime = Time.time + (1 / fireRate); |
| | 16 | 214 | | shootState = ShootState.Shooting; |
| | 16 | 215 | | } else { |
| | 0 | 216 | | Reload(); |
| | 0 | 217 | | } |
| | 16 | 218 | | } |
| | 16 | 219 | | } |
| | | 220 | | |
| | | 221 | | /// Attempts to reload the gun |
| | 0 | 222 | | public void Reload() { |
| | | 223 | | // Checks that the gun is ready to be reloaded |
| | 0 | 224 | | if(shootState == ShootState.Ready) { |
| | 0 | 225 | | nextShootTime = Time.time + reloadTime; |
| | 0 | 226 | | shootState = ShootState.Reloading; |
| | 0 | 227 | | text.SetText("Reloading"); |
| | 0 | 228 | | AudioManager.Play("Reload"); |
| | 0 | 229 | | } |
| | 0 | 230 | | } |
| | | 231 | | |
| | | 232 | | //Animating Trigger |
| | | 233 | | public void TriggerReleased(DeactivateEventArgs args) |
| | 48393 | 234 | | { |
| | | 235 | | //m_Animator.SetTrigger(k_AnimTriggerUp); |
| | 48393 | 236 | | m_TriggerDown = false; |
| | 48393 | 237 | | m_TriggerHeldTime = 0f; |
| | 48393 | 238 | | } |
| | | 239 | | |
| | | 240 | | |
| | | 241 | | //Animating Trigger |
| | | 242 | | public void TriggerPulled(ActivateEventArgs args) |
| | 45586 | 243 | | { |
| | | 244 | | //m_Animator.SetTrigger(k_AnimTriggerDown); |
| | 45586 | 245 | | m_TriggerDown = true; |
| | 45586 | 246 | | } |
| | | 247 | | |
| | | 248 | | |
| | | 249 | | // In case the gun is dropped while in use. |
| | | 250 | | public void DroppedGun(SelectExitEventArgs args) |
| | 48393 | 251 | | { |
| | | 252 | | //m_Animator.SetTrigger(k_AnimTriggerUp); |
| | 48393 | 253 | | m_TriggerDown = false; |
| | 48393 | 254 | | m_TriggerHeldTime = 0f; |
| | 48393 | 255 | | } |
| | | 256 | | |
| | | 257 | | |
| | | 258 | | // void OnTriggerEnter(Collider magazine) |
| | | 259 | | // { |
| | | 260 | | // if (magazine.gameObject.CompareTag("Magazine")) |
| | | 261 | | // { |
| | | 262 | | // magazine.gameObject.GetComponent<MagazineVariables>().ChangeMagVar(); |
| | | 263 | | // } |
| | | 264 | | // } |
| | | 265 | | |
| | | 266 | | |
| | | 267 | | //Events that happen when the magazine is inserted |
| | | 268 | | public void MagazineInsert() |
| | 0 | 269 | | { |
| | | 270 | | |
| | | 271 | | |
| | | 272 | | //Magvar |
| | | 273 | | //MagVar.ChangeStats(); |
| | | 274 | | |
| | | 275 | | //reload gun |
| | | 276 | | //Reload(); |
| | | 277 | | |
| | | 278 | | //Message |
| | | 279 | | //Debug.Log("Magazine Has Been Inserted"); |
| | | 280 | | |
| | | 281 | | |
| | | 282 | | //Enable Colliders for Magazine |
| | | 283 | | //Gun_Collider_Physical.enabled = false; |
| | | 284 | | |
| | | 285 | | //Disable Magazine Colliders |
| | | 286 | | //Magazine_Collider_Physical.enabled = false; |
| | | 287 | | |
| | | 288 | | //Reload(); |
| | | 289 | | |
| | 0 | 290 | | } |
| | | 291 | | |
| | | 292 | | |
| | | 293 | | //Events that happen when the magazine is inserted |
| | | 294 | | public void MagazineExit() |
| | 0 | 295 | | { |
| | | 296 | | //Magvar other |
| | | 297 | | //MagVar.RemoveMag(); |
| | | 298 | | |
| | | 299 | | //Wait For Colliders to appear again |
| | | 300 | | //Thread.Sleep(100); |
| | | 301 | | |
| | | 302 | | //Enable Colliders for Magazine |
| | | 303 | | //Gun_Collider_Physical.enabled = true; |
| | | 304 | | |
| | | 305 | | //Enable Colliders for Magazine |
| | | 306 | | // Magazine_Collider_Physical.enabled = true; |
| | | 307 | | |
| | | 308 | | //Message |
| | | 309 | | //Debug.Log("Magazine Has Been Removed"); |
| | 0 | 310 | | } |
| | | 311 | | |
| | | 312 | | |
| | | 313 | | } |