< Summary

Class:Gun
Assembly:Test
File(s):D:/--UnityProject/VR/VRExplorer_subjects/VGuns-Unity-VR/Assets/Test/Gun.cs
Covered lines:60
Uncovered lines:4
Coverable lines:64
Total lines:240
Line coverage:93.7% (60 of 64)
Covered branches:0
Total branches:0
Covered methods:8
Total methods:10
Method coverage:80% (8 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%000100%
Reload()0%000100%
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 TMPro;
 3using UnityEngine.XR.Interaction.Toolkit;
 4using Random = UnityEngine.Random;
 5using System.Diagnostics.CodeAnalysis;
 6using BNG;
 7public 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
 1464    private ShootState shootState = ShootState.Ready;
 65
 66    // The next time that the gun is able to shoot at
 1467    private float nextShootTime = 0;
 68
 769    void Start() {
 70        //muzzleOffset = attackPoint.GetComponent<Renderer>().bounds.extents.z;
 771        remainingAmmunition = ammunition;
 72
 773        AudioManager = FindObjectOfType<AudioManager>();
 774    }
 75
 2519376    void Update() {
 2519377        switch(shootState) {
 78            case ShootState.Shooting:
 79                // If the gun is ready to shoot again...
 8880                if(Time.time > nextShootTime) {
 1081                    shootState = ShootState.Ready;
 1082                }
 7883                break;
 84            case ShootState.Reloading:
 85                // If the gun has finished reloading...
 138986                if(Time.time > nextShootTime) {
 587                    remainingAmmunition = ammunition;
 588                    shootState = ShootState.Ready;
 589                }
 138490                break;
 91        }
 92
 93        //m_Animator = GetComponent<Animator>(); //(For Animations Down the line)
 2519394        m_InteractableBase = GetComponent<XRGrabInteractable>();
 2519395        m_InteractableBase.selectExited.AddListener(DroppedGun);
 2519396        m_InteractableBase.activated.AddListener(TriggerPulled);
 2519397        m_InteractableBase.deactivated.AddListener(TriggerReleased);
 98
 99        //Show leftover bullets
 25193100        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        //  }
 25193114    }
 115
 116    /// Attempts to fire the gun
 13117    public void Shoot() {
 118        // Checks that the gun is ready to shoot
 25119        if(shootState == ShootState.Ready) {
 168120            for(int i = 0; i < roundsPerShot; i++) {
 121
 48122                AudioManager.Play("Gun Fire");
 123                // Instantiates the round at the muzzle position
 48124                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
 48128                spawnedRound.transform.Rotate(new Vector3(
 129                    Random.Range(-1f, 1f) * maxRoundVariation,
 130                    Random.Range(-1f, 1f) * maxRoundVariation,
 131                    0
 132                ));
 133
 48134                Rigidbody rb = spawnedRound.GetComponent<Rigidbody>();
 48135                rb.velocity = spawnedRound.transform.forward * roundSpeed;
 48136            }
 137
 12138            remainingAmmunition--;
 22139            if(remainingAmmunition > 0) {
 10140                nextShootTime = Time.time + (1 / fireRate);
 10141                shootState = ShootState.Shooting;
 12142            } else {
 2143                Reload();
 2144            }
 12145        }
 13146    }
 147
 148    /// Attempts to reload the gun
 6149    public void Reload() {
 150        // Checks that the gun is ready to be reloaded
 11151        if(shootState == ShootState.Ready) {
 5152            nextShootTime = Time.time + reloadTime;
 5153            shootState = ShootState.Reloading;
 5154            text.SetText("Reloading");
 5155            AudioManager.Play("Reload");
 5156        }
 6157    }
 158
 159    //Animating Trigger
 160    public void TriggerReleased(DeactivateEventArgs args)
 11321161    {
 162        //m_Animator.SetTrigger(k_AnimTriggerUp);
 11321163        m_TriggerDown = false;
 11321164        m_TriggerHeldTime = 0f;
 11321165    }
 166
 167
 168    //Animating Trigger
 169    public void TriggerPulled(ActivateEventArgs args)
 15440170    {
 171        //m_Animator.SetTrigger(k_AnimTriggerDown);
 15440172        m_TriggerDown = true;
 15440173    }
 174
 175
 176    // In case the gun is dropped while in use.
 177    public void DroppedGun(SelectExitEventArgs args)
 11321178    {
 179        //m_Animator.SetTrigger(k_AnimTriggerUp);
 11321180        m_TriggerDown = false;
 11321181        m_TriggerHeldTime = 0f;
 11321182    }
 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()
 0196    {
 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
 0217    }
 218
 219
 220    //Events that happen when the magazine is inserted
 221    public void MagazineExit()
 0222    {
 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");
 0237    }
 238
 239
 240}