< 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()
 9595854    {
 55        // Handle turning on
 9595856        if (this.shouldTurnOn && !this.isOn)
 1271257        {
 1271258            this.timeToOnDelta += (Time.deltaTime * (this.hasDownNeighbor ? this.downMultiplier : 1f));
 59            // If over the limit with the neighbors
 1271260            if (this.timeToOnDelta >= this.timeToOn)
 561            {
 562                this.isOn = true;
 563                this.shouldTurnOn = false;
 564                this.timeToOnDelta = 0f;
 565                this.isTurningOff = false;
 566                this.currentHitTime = 0f;
 567                this.UpdateFireState(true);
 568            }
 1271269        }
 70        // Handle turning off
 9595871        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        }
 9595889    }
 90
 91    public void NotifyNeighborState(bool fireOn)
 1692    {
 93        // Update the count
 3294        if (fireOn) this.onNeighbors++;
 095        else this.onNeighbors--;
 1696        this.CheckNeighborState();
 1697    }
 98
 99    /* ------------------------------------------------------------------------------ */
 100    /* STATE */
 101    /* ------------------------------------------------------------------------------ */
 102
 103    public void UpdateFireState(bool notify)
 32104    {
 32105        if (this.isOn)
 26106        {
 26107            this.pSystem.Play();
 26108        }
 109        else
 6110        {
 6111            this.pSystem.Stop();
 6112        }
 32113        if (notify)
 5114        {
 5115            this.NotifyAllNeighbors();
 5116            FireController.Instance.FireChanged(this.isOn ? 1 : -1);
 5117        }
 32118    }
 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)
 54150                {
 54151                    this.onNeighbors++;
 54152                }
 72153            }
 729154        }
 155        // Check on the neighbor state
 27156        this.CheckNeighborState();
 27157    }
 158
 159    private void CheckNeighborState()
 43160    {
 161        /*
 162            If we are over the neighbor limit, the fire is off and is not turning on,
 163            it should start counting
 164        */
 43165        if (this.onNeighbors >= this.onNeighborsThreshold)
 34166        {
 34167            if (!this.isOn && !this.shouldTurnOn)
 5168            {
 5169                this.shouldTurnOn = true;
 5170                this.timeToOnDelta = 0f;
 171                // Find if any of the neihbors is down
 5172                this.hasDownNeighbor = false;
 63173                foreach (Fire neighbor in this.neighbors) {
 16174                    this.hasDownNeighbor = this.hasDownNeighbor || (this.transform.position.y - neighbor.transform.posit
 16175                }
 5176            }
 34177        }
 178        else
 9179        {
 9180            this.shouldTurnOn = false;
 9181            this.timeToOnDelta = 0f;
 9182        }
 43183    }
 184
 185    private void NotifyAllNeighbors()
 5186    {
 63187        foreach (Fire fire in this.neighbors) fire.NotifyNeighborState(this.isOn);
 5188    }
 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}