< Summary

Class:Fire
Assembly:Test
File(s):D:/--UnityProject/VR/_____ISSTA 26/vr-firefighter-simulator/Assets/Test/Fire.cs
Covered lines:96
Uncovered lines:61
Coverable lines:157
Total lines:244
Line coverage:61.1% (96 of 157)
Covered branches:0
Total branches:0
Covered methods:9
Total methods:13
Method coverage:69.2% (9 of 13)

Coverage History

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
Fire()0%000100%
Awake()0%000100%
Update()0%00047.06%
NotifyNeighborState(...)0%00083.33%
UpdateFireState(...)0%000100%
WaterHit(...)0%0000%
SetIsOn(...)0%000100%
FindNeighbors(...)0%000100%
CheckNeighborState()0%000100%
NotifyAllNeighbors()0%000100%
OnDrawGizmosSelected()0%0000%
TurnOffSprite()0%0000%
TurnOnSprite()0%0000%

File(s)

D:/--UnityProject/VR/_____ISSTA 26/vr-firefighter-simulator/Assets/Test/Fire.cs

#LineLine coverage
 1using System.Collections.Generic;
 2using UnityEngine;
 3using System.Collections;
 4
 5public class Fire : MonoBehaviour, IWaterInteractable
 6{
 7    [Header("Debug")]
 548    public bool debug = false;
 9
 10    [Header("State")]
 5411    public bool isOn = false;
 5412    public float timeToOnMin = 3f;
 5413    public float timeToOnMax = 3f;
 14    public float timeToOn;
 15    [SerializeField]
 5416    private float timeToOnDelta = 0f;
 17    [SerializeField]
 5418    private bool shouldTurnOn = false;
 19
 20    [Header("Neighbors")]
 21    [SerializeField]
 5422    private int onNeighbors = 0;
 5423    private bool hasDownNeighbor = false;
 5424    public float maxNeighborDistance = 4f;
 25    [Range(1f, 3f)]
 5426    public float downMultiplier = 1f;
 5427    public float downNeighborDist = 0.5f;
 28
 29    [Header("Fire Turning Off")]
 5430    private bool isTurningOff = false;
 5431    public float maxHitInterval = 2f;
 5432    public float currentHitInterval = 0f;
 5433    public float totalHitTime = 10f;
 5434    public float currentHitTime = 0f;
 35
 36    [Header("Neighbors")]
 37    // This list shouldn't be initialized in the code
 38    // We can leave it like this, in case the programmer decides to manually input some of them
 39    public List<Fire> neighbors;
 5440    public int onNeighborsThreshold = 2;
 5441    public float minNeighborDownDistance = 3f;
 42
 43    [Header("Particle System")]
 44    public GameObject mainEffectGo;
 45    private ParticleSystem pSystem;
 46
 47    void Awake()
 2748    {
 2749        this.pSystem = this.mainEffectGo.GetComponent<ParticleSystem>();
 2750        this.timeToOn = Random.Range(this.timeToOnMin, this.timeToOnMax);
 2751    }
 52
 53    void Update()
 34171254    {
 55        // Handle turning on
 34171256        if (this.shouldTurnOn && !this.isOn)
 504757        {
 504758            this.timeToOnDelta += (Time.deltaTime * (this.hasDownNeighbor ? this.downMultiplier : 1f));
 59            // If over the limit with the neighbors
 504760            if (this.timeToOnDelta >= this.timeToOn)
 861            {
 862                this.isOn = true;
 863                this.shouldTurnOn = false;
 864                this.timeToOnDelta = 0f;
 865                this.isTurningOff = false;
 866                this.currentHitTime = 0f;
 867                this.UpdateFireState(true);
 868            }
 504769        }
 70        // Handle turning off
 34171271        if (this.isTurningOff && this.isOn) {
 72            // Add to the timers
 073            this.currentHitTime += Time.deltaTime;
 074            this.currentHitInterval += Time.deltaTime;
 75            // If the current interval timer is larger than the limit, it stops turning off
 076            if (this.currentHitInterval >= this.maxHitInterval) {
 077                this.isTurningOff = false;
 078                this.currentHitTime = 0f;
 079            } else if (this.currentHitTime >= this.totalHitTime) {
 80                // Fire turns off
 081                this.isOn = false;
 082                this.shouldTurnOn = false;
 083                this.timeToOnDelta = 0f;
 084                this.isTurningOff = false;
 085                this.currentHitTime = 0f;
 086                this.UpdateFireState(true);
 087            }
 088        }
 34171289    }
 90
 91    public void NotifyNeighborState(bool fireOn)
 2492    {
 93        // Update the count
 4894        if (fireOn) this.onNeighbors++;
 095        else this.onNeighbors--;
 2496        this.CheckNeighborState();
 2497    }
 98
 99    /* ------------------------------------------------------------------------------ */
 100    /* STATE */
 101    /* ------------------------------------------------------------------------------ */
 102
 103    public void UpdateFireState(bool notify)
 35104    {
 35105        if (this.isOn)
 27106        {
 27107            this.pSystem.Play();
 27108        }
 109        else
 8110        {
 8111            this.pSystem.Stop();
 8112        }
 35113        if (notify)
 8114        {
 8115            this.NotifyAllNeighbors();
 8116            FireController.Instance.FireChanged(this.isOn ? 1 : -1);
 8117        }
 35118    }
 119
 120    public void WaterHit(Vector3 normal)
 0121    {
 122        // Reset timer when hit
 0123        this.isTurningOff = true;
 0124        this.currentHitInterval = 0f;
 0125    }
 126
 127    // Method to externally turn on the fire, without notifying the neighbors
 128    public void SetIsOn(bool _isOn)
 27129    {
 27130        this.isOn = _isOn;
 27131        this.UpdateFireState(false);
 27132    }
 133
 134    /* ------------------------------------------------------------------------------ */
 135    /* NEIGHBORS */
 136    /* ------------------------------------------------------------------------------ */
 137
 138    public void FindNeighbors(Fire[] allFires, int fireId)
 27139    {
 140        // Count how many of the neighbors are on
 27141        this.onNeighbors = 0;
 142        // Find all the neighbors within the given distance
 1512143        for (int i = 0; i < allFires.Length; i++)
 729144        {
 145            // If neighbor is within distance, add it
 729146            if (i != fireId && Vector3.Distance(this.gameObject.transform.position, allFires[i].gameObject.transform.pos
 72147            {
 72148                neighbors.Add(allFires[i]);
 72149                if (allFires[i].isOn)
 47150                {
 47151                    this.onNeighbors++;
 47152                }
 72153            }
 729154        }
 155        // Check on the neighbor state
 27156        this.CheckNeighborState();
 27157    }
 158
 159    private void CheckNeighborState()
 51160    {
 161        /*
 162            If we are over the neighbor limit, the fire is off and is not turning on,
 163            it should start counting
 164        */
 51165        if (this.onNeighbors >= this.onNeighborsThreshold)
 40166        {
 40167            if (!this.isOn && !this.shouldTurnOn)
 8168            {
 8169                this.shouldTurnOn = true;
 8170                this.timeToOnDelta = 0f;
 171                // Find if any of the neihbors is down
 8172                this.hasDownNeighbor = false;
 96173                foreach (Fire neighbor in this.neighbors) {
 24174                    this.hasDownNeighbor = this.hasDownNeighbor || (this.transform.position.y - neighbor.transform.posit
 24175                }
 8176            }
 40177        }
 178        else
 11179        {
 11180            this.shouldTurnOn = false;
 11181            this.timeToOnDelta = 0f;
 11182        }
 51183    }
 184
 185    private void NotifyAllNeighbors()
 8186    {
 96187        foreach (Fire fire in this.neighbors) fire.NotifyNeighborState(this.isOn);
 8188    }
 189
 190    /* ------------------------------------------------------------------------------ */
 191    /* DEBUG */
 192    /* ------------------------------------------------------------------------------ */
 193
 194    void OnDrawGizmosSelected()
 0195    {
 0196        if (debug)
 0197        {
 198            // Display the explosion radius when selected
 0199            Gizmos.color = Color.red;
 0200            Gizmos.DrawWireSphere(this.transform.position, this.maxNeighborDistance);
 0201        }
 0202    }
 203
 204    private IEnumerator TurnOffSprite()
 0205    {
 0206        Component[] particleSystems = GetComponentsInChildren(typeof(ParticleSystem));
 0207        for (int i = 0; i < 10; i++)
 0208        {
 0209            for(int s=0; s < particleSystems.Length; s++)
 0210            {
 0211                ParticleSystem system = (ParticleSystem)particleSystems[s];
 0212                ParticleSystem.MainModule main = system.main;
 0213                main.maxParticles = system.main.maxParticles / 2;
 0214                yield return new WaitForSeconds(0.01f);
 0215            }
 0216        }
 0217        this.pSystem.Stop();
 0218        for (int s = 0; s < particleSystems.Length; s++)
 0219        {
 0220            ParticleSystem system = (ParticleSystem)particleSystems[s];
 0221            system.Stop();
 0222        }
 0223    }
 224
 225    private IEnumerator TurnOnSprite()
 0226    {
 0227        Component[] particleSystems = GetComponentsInChildren(typeof(ParticleSystem));
 0228        for (int s = 0; s < particleSystems.Length; s++)
 0229        {
 0230            ParticleSystem system = (ParticleSystem)particleSystems[s];
 0231            ParticleSystem.MainModule main = system.main;
 0232            main.maxParticles = 1000;
 0233        }
 0234        this.pSystem.Play();
 0235        for (int s = 0; s < particleSystems.Length; s++)
 0236        {
 0237            ParticleSystem system = (ParticleSystem)particleSystems[s];
 0238            system.Play();
 0239        }
 0240        yield return new WaitForSeconds(0.01f);
 0241    }
 242
 243
 244}