< Summary

Class:Gun
Assembly:Test
File(s):D:/--UnityProject/VR/VRExplorer_subjects/VGuns-Unity-VR/Assets/Test/Gun.cs
Covered lines:45
Uncovered lines:19
Coverable lines:64
Total lines:313
Line coverage:70.3% (45 of 64)
Covered branches:0
Total branches:0
Covered methods:7
Total methods:10
Method coverage:70% (7 of 10)

Coverage History

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
Gun()0%000100%
Start()0%000100%
Update()0%00068.42%
Shoot()0%00087.5%
Reload()0%0000%
TriggerReleased(...)0%000100%
TriggerPulled(...)0%000100%
DroppedGun(...)0%000100%
MagazineInsert()0%0000%
MagazineExit()0%0000%

File(s)

D:/--UnityProject/VR/VRExplorer_subjects/VGuns-Unity-VR/Assets/Test/Gun.cs

#LineLine coverage
 1using UnityEngine;
 2using System.Threading;
 3using TMPro;
 4using UnityEngine;
 5using System.Collections;
 6using UnityEngine.XR.Interaction.Toolkit;
 7using Random = UnityEngine.Random;
 8using VRExplorer;
 9using System.Diagnostics.CodeAnalysis;
 10using BNG;
 11
 12public 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
 35137    private ShootState shootState = ShootState.Ready;
 138
 139    // The next time that the gun is able to shoot at
 35140    private float nextShootTime = 0;
 141
 28142    void Start() {
 143        //muzzleOffset = attackPoint.GetComponent<Renderer>().bounds.extents.z;
 28144        remainingAmmunition = ammunition;
 145
 28146        AudioManager = FindObjectOfType<AudioManager>();
 28147    }
 148
 86387149    void Update() {
 86387150        switch(shootState) {
 151            case ShootState.Shooting:
 152                // If the gun is ready to shoot again...
 176153                if(Time.time > nextShootTime) {
 16154                    shootState = ShootState.Ready;
 16155                }
 160156                break;
 157            case ShootState.Reloading:
 158                // If the gun has finished reloading...
 0159                if(Time.time > nextShootTime) {
 0160                    remainingAmmunition = ammunition;
 0161                    shootState = ShootState.Ready;
 0162                }
 0163                break;
 164        }
 165
 166        //m_Animator = GetComponent<Animator>(); //(For Animations Down the line)
 86387167        m_InteractableBase = GetComponent<XRGrabInteractable>();
 86387168        m_InteractableBase.selectExited.AddListener(DroppedGun);
 86387169        m_InteractableBase.activated.AddListener(TriggerPulled);
 86387170        m_InteractableBase.deactivated.AddListener(TriggerReleased);
 171
 172        //Show leftover bullets
 86387173        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        //  }
 86387187    }
 188
 189    /// Attempts to fire the gun
 16190    public void Shoot() {
 191        // Checks that the gun is ready to shoot
 32192        if(shootState == ShootState.Ready) {
 188193            for(int i = 0; i < roundsPerShot; i++) {
 194
 52195                AudioManager.Play("Gun Fire");
 196                // Instantiates the round at the muzzle position
 52197                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
 52201                spawnedRound.transform.Rotate(new Vector3(
 202                    Random.Range(-1f, 1f) * maxRoundVariation,
 203                    Random.Range(-1f, 1f) * maxRoundVariation,
 204                    0
 205                ));
 206
 52207                Rigidbody rb = spawnedRound.GetComponent<Rigidbody>();
 52208                rb.velocity = spawnedRound.transform.forward * roundSpeed;
 52209            }
 210
 16211            remainingAmmunition--;
 32212            if(remainingAmmunition > 0) {
 16213                nextShootTime = Time.time + (1 / fireRate);
 16214                shootState = ShootState.Shooting;
 16215            } else {
 0216                Reload();
 0217            }
 16218        }
 16219    }
 220
 221    /// Attempts to reload the gun
 0222    public void Reload() {
 223        // Checks that the gun is ready to be reloaded
 0224        if(shootState == ShootState.Ready) {
 0225            nextShootTime = Time.time + reloadTime;
 0226            shootState = ShootState.Reloading;
 0227            text.SetText("Reloading");
 0228            AudioManager.Play("Reload");
 0229        }
 0230    }
 231
 232    //Animating Trigger
 233    public void TriggerReleased(DeactivateEventArgs args)
 48393234    {
 235        //m_Animator.SetTrigger(k_AnimTriggerUp);
 48393236        m_TriggerDown = false;
 48393237        m_TriggerHeldTime = 0f;
 48393238    }
 239
 240
 241    //Animating Trigger
 242    public void TriggerPulled(ActivateEventArgs args)
 45586243    {
 244        //m_Animator.SetTrigger(k_AnimTriggerDown);
 45586245        m_TriggerDown = true;
 45586246    }
 247
 248
 249    // In case the gun is dropped while in use.
 250    public void DroppedGun(SelectExitEventArgs args)
 48393251    {
 252        //m_Animator.SetTrigger(k_AnimTriggerUp);
 48393253        m_TriggerDown = false;
 48393254        m_TriggerHeldTime = 0f;
 48393255    }
 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()
 0269    {
 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
 0290    }
 291
 292
 293    //Events that happen when the magazine is inserted
 294    public void MagazineExit()
 0295    {
 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");
 0310    }
 311
 312
 313}