< Summary

Class:Gun
Assembly:Test
File(s):D:/--UnityProject/VR/_____ISSTA 26/VGuns-Unity-VR/Assets/Test/Gun.cs
Covered lines:47
Uncovered lines:17
Coverable lines:64
Total lines:317
Line coverage:73.4% (47 of 64)
Covered branches:0
Total branches:0
Covered methods:5
Total methods:10
Method coverage:50% (5 of 10)

Coverage History

Metrics

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

File(s)

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

#LineLine coverage
 1using UnityEngine;
 2using TMPro;
 3using UnityEngine.XR.Interaction.Toolkit;
 4using Random = UnityEngine.Random;
 5using System.Diagnostics.CodeAnalysis;
 6using BNG;
 7using HenryLab;
 8
 9public class Gun : MonoBehaviour, IGrabbableEntity, ITriggerableEntity
 10{
 11    [ExcludeFromCodeCoverage] public float TriggeringTime => 2.5f;
 12    [ExcludeFromCodeCoverage] public string Name => Str.Triggerable;
 13
 14    [ExcludeFromCodeCoverage]
 15    public void Triggerring()
 16    {
 17        var obj = EntityManager.Instance.vrexplorerMono.gameObject;
 18        XRDirectInteractor interactor;
 19        if(!obj.TryGetComponent(out interactor))
 20        {
 21            interactor = obj.AddComponent<XRDirectInteractor>();
 22        }
 23        if(!obj.GetComponent<ActionBasedController>())
 24        {
 25            obj.AddComponent<ActionBasedController>();
 26        }
 27        var interactable = GetComponent<XRBaseInteractable>();
 28        var e = new SelectEnterEventArgs() { interactorObject = interactor };
 29        var h = new HoverEnterEventArgs() { interactorObject = interactor };
 30        var a = new ActivateEventArgs() { interactorObject = interactor };
 31        interactable.selectEntered.Invoke(e);
 32        interactable.hoverEntered.Invoke(h);
 33        interactable.firstSelectEntered.Invoke(e);
 34        interactable.firstHoverEntered.Invoke(h);
 35        interactable.activated.Invoke(a);
 36    }
 37
 38    [ExcludeFromCodeCoverage]
 39    public void Triggerred()
 40    {
 41        var obj = EntityManager.Instance.vrexplorerMono.gameObject;
 42        XRDirectInteractor interactor;
 43        if(!obj.TryGetComponent(out interactor))
 44        {
 45            interactor = obj.AddComponent<XRDirectInteractor>();
 46        }
 47        if(!obj.GetComponent<ActionBasedController>())
 48        {
 49            obj.AddComponent<ActionBasedController>();
 50        }
 51        var e = new SelectExitEventArgs() { interactorObject = interactor };
 52        var h = new HoverExitEventArgs() { interactorObject = interactor };
 53        var a = new DeactivateEventArgs() { interactorObject = interactor };
 54        var interactable = GetComponent<XRBaseInteractable>();
 55        interactable.selectExited.Invoke(e);
 56        interactable.hoverExited.Invoke(h);
 57        interactable.lastSelectExited.Invoke(e);
 58        interactable.lastHoverExited.Invoke(h);
 59        interactable.deactivated.Invoke(a);
 60
 61    }
 62
 63    [ExcludeFromCodeCoverage]
 64    public Grabbable Grabbable
 65    {
 66        get
 67        {
 68            var g = GetComponent<Grabbable>();
 69            if(g) return g;
 70            return gameObject.AddComponent<Grabbable>();
 71        }
 72    }
 73
 74    [ExcludeFromCodeCoverage] public Transform Destination => null;
 75
 76    [ExcludeFromCodeCoverage]
 77    public void OnGrabbed()
 78    {
 79    }
 80
 81    [ExcludeFromCodeCoverage]
 82    public void OnReleased()
 83    {
 84    }
 85
 86    public enum ShootState {
 87        Ready,
 88        Shooting,
 89        Reloading
 90    }
 91
 92    // The Gun
 93    XRGrabInteractable m_InteractableBase;
 94
 95    //Gun Colliders
 96    //public Collider Gun_Collider_Physical = null;
 97
 98    //Magazine Colliders
 99    //public Collider Magazine_Collider_Physical = null;
 100
 101    // How far forward the muzzle is from the centre of the gun
 102    private float muzzleOffset;
 103
 104    //MagazineVariables
 105    //public MagazineVariables MagVar;
 106
 107    //Trigger Functions
 108    float m_TriggerHeldTime;
 109    bool m_TriggerDown;
 110
 111    //Graphics
 112    public GameObject muzzleFlash;
 113    public TextMeshPro text;
 114
 115    //Reference
 116    public GameObject GunPov;
 117
 118    public AudioManager AudioManager;
 119
 120    [Header("Magazine")]
 121    public GameObject round;
 122    public int ammunition;
 123
 124    [Range(0.5f, 10)] public float reloadTime;
 125
 126    private int remainingAmmunition;
 127
 128    [Header("Shooting")]
 129    // How many shots the gun can make per second
 130    [Range(0.25f, 25)] public float fireRate;
 131
 132    // The number of rounds fired each shot
 133    public int roundsPerShot;
 134
 135    [Range(0.5f, 100)] public float roundSpeed;
 136
 137    // The maximum angle that the bullet's direction can vary,
 138    // in both the horizontal and vertical axes
 139    [Range(0, 45)] public float maxRoundVariation;
 140
 17141    private ShootState shootState = ShootState.Ready;
 142
 143    // The next time that the gun is able to shoot at
 17144    private float nextShootTime = 0;
 145
 7146    void Start() {
 147        //muzzleOffset = attackPoint.GetComponent<Renderer>().bounds.extents.z;
 7148        remainingAmmunition = ammunition;
 149
 7150        AudioManager = FindObjectOfType<AudioManager>();
 7151    }
 152
 33460153    void Update() {
 33460154        switch(shootState) {
 155            case ShootState.Shooting:
 156                // If the gun is ready to shoot again...
 8157                if(Time.time > nextShootTime) {
 2158                    shootState = ShootState.Ready;
 2159                }
 6160                break;
 161            case ShootState.Reloading:
 162                // If the gun has finished reloading...
 82163                if(Time.time > nextShootTime) {
 2164                    remainingAmmunition = ammunition;
 2165                    shootState = ShootState.Ready;
 2166                }
 80167                break;
 168        }
 169
 170        //m_Animator = GetComponent<Animator>(); //(For Animations Down the line)
 33460171        m_InteractableBase = GetComponent<XRGrabInteractable>();
 33460172        m_InteractableBase.selectExited.AddListener(DroppedGun);
 33460173        m_InteractableBase.activated.AddListener(TriggerPulled);
 33460174        m_InteractableBase.deactivated.AddListener(TriggerReleased);
 175
 176        //Show leftover bullets
 33460177        text.SetText(remainingAmmunition + " / " + ammunition);
 178
 179        //update if holding trigger
 180        //if (m_TriggerDown)
 181        //{
 182        //    m_TriggerHeldTime += Time.deltaTime;
 183        //    if (m_TriggerHeldTime >= k_HeldThreshold)
 184        //    {
 185        //        if (!muzzleFlash.isPlaying)
 186        //         {
 187        //             muzzleFlash.Play();
 188        //          }
 189        //      }
 190        //  }
 33460191    }
 192
 193    /// Attempts to fire the gun
 2194    public void Shoot() {
 195        // Checks that the gun is ready to shoot
 4196        if(shootState == ShootState.Ready) {
 10197            for(int i = 0; i < roundsPerShot; i++) {
 198
 2199                AudioManager.Play("Gun Fire");
 200                // Instantiates the round at the muzzle position
 2201                GameObject spawnedRound = Instantiate(round, GunPov.transform.position, GunPov.transform.rotation);
 202                //GameObject muzzleflash = Instantiate(muzzleFlash, GunPov.transform.position, GunPov.transform.rotation
 203
 204                // Add a random variation to the round's direction
 2205                spawnedRound.transform.Rotate(new Vector3(
 206                    Random.Range(-1f, 1f) * maxRoundVariation,
 207                    Random.Range(-1f, 1f) * maxRoundVariation,
 208                    0
 209                ));
 210
 2211                Rigidbody rb = spawnedRound.GetComponent<Rigidbody>();
 2212                rb.velocity = spawnedRound.transform.forward * roundSpeed;
 2213            }
 214
 2215            remainingAmmunition--;
 4216            if(remainingAmmunition > 0) {
 2217                nextShootTime = Time.time + (1 / fireRate);
 2218                shootState = ShootState.Shooting;
 2219            } else {
 0220                Reload();
 0221            }
 2222        }
 2223    }
 224
 225    /// Attempts to reload the gun
 2226    public void Reload() {
 227        // Checks that the gun is ready to be reloaded
 4228        if(shootState == ShootState.Ready) {
 2229            nextShootTime = Time.time + reloadTime;
 2230            shootState = ShootState.Reloading;
 2231            text.SetText("Reloading");
 2232            AudioManager.Play("Reload");
 2233        }
 2234    }
 235
 236    //Animating Trigger
 237    public void TriggerReleased(DeactivateEventArgs args)
 0238    {
 239        //m_Animator.SetTrigger(k_AnimTriggerUp);
 0240        m_TriggerDown = false;
 0241        m_TriggerHeldTime = 0f;
 0242    }
 243
 244
 245    //Animating Trigger
 246    public void TriggerPulled(ActivateEventArgs args)
 0247    {
 248        //m_Animator.SetTrigger(k_AnimTriggerDown);
 0249        m_TriggerDown = true;
 0250    }
 251
 252
 253    // In case the gun is dropped while in use.
 254    public void DroppedGun(SelectExitEventArgs args)
 0255    {
 256        //m_Animator.SetTrigger(k_AnimTriggerUp);
 0257        m_TriggerDown = false;
 0258        m_TriggerHeldTime = 0f;
 0259    }
 260
 261
 262    //     void OnTriggerEnter(Collider magazine)
 263    //    {
 264    //        if (magazine.gameObject.CompareTag("Magazine"))
 265    //        {
 266    //             magazine.gameObject.GetComponent<MagazineVariables>().ChangeMagVar();
 267    //         }
 268    //     }
 269
 270
 271    //Events that happen when the magazine is inserted
 272    public void MagazineInsert()
 0273    {
 274
 275
 276        //Magvar
 277        //MagVar.ChangeStats();
 278
 279        //reload gun
 280        //Reload();
 281
 282        //Message
 283        //Debug.Log("Magazine Has Been Inserted");
 284
 285
 286        //Enable Colliders for Magazine
 287        //Gun_Collider_Physical.enabled = false;
 288
 289        //Disable Magazine Colliders
 290        //Magazine_Collider_Physical.enabled = false;
 291
 292        //Reload();
 293
 0294    }
 295
 296
 297    //Events that happen when the magazine is inserted
 298    public void MagazineExit()
 0299    {
 300        //Magvar other
 301        //MagVar.RemoveMag();
 302
 303        //Wait For Colliders to appear again
 304        //Thread.Sleep(100);
 305
 306        //Enable Colliders for Magazine
 307        //Gun_Collider_Physical.enabled = true;
 308
 309        //Enable Colliders for Magazine
 310       // Magazine_Collider_Physical.enabled = true;
 311
 312        //Message
 313        //Debug.Log("Magazine Has Been Removed");
 0314    }
 315
 316
 317}