< Summary

Class:Fire
Assembly:Test
File(s):E:/Unity/Unity Project/vr-firefighter-simulator/Assets/Test/Fire.cs
Covered lines:96
Uncovered lines:15
Coverable lines:111
Total lines:244
Line coverage:86.4% (96 of 111)
Covered branches:0
Total branches:0
Covered methods:9
Total methods:9
Method coverage:100% (9 of 9)

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%
SetIsOn(...)0%000100%
FindNeighbors(...)0%000100%
CheckNeighborState()0%000100%
NotifyAllNeighbors()0%000100%

File(s)

E:/Unity/Unity Project/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()
 59737554    {
 55        // Handle turning on
 59737556        if (this.shouldTurnOn && !this.isOn)
 436957        {
 436958            this.timeToOnDelta += (Time.deltaTime * (this.hasDownNeighbor ? this.downMultiplier : 1f));
 59            // If over the limit with the neighbors
 436960            if (this.timeToOnDelta >= this.timeToOn)
 461            {
 462                this.isOn = true;
 463                this.shouldTurnOn = false;
 464                this.timeToOnDelta = 0f;
 465                this.isTurningOff = false;
 466                this.currentHitTime = 0f;
 467                this.UpdateFireState(true);
 468            }
 436969        }
 70        // Handle turning off
 59737571        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        }
 59737589    }
 90
 91    public void NotifyNeighborState(bool fireOn)
 1592    {
 93        // Update the count
 3094        if (fireOn) this.onNeighbors++;
 095        else this.onNeighbors--;
 1596        this.CheckNeighborState();
 1597    }
 98
 99    /* ------------------------------------------------------------------------------ */
 100    /* STATE */
 101    /* ------------------------------------------------------------------------------ */
 102
 103    public void UpdateFireState(bool notify)
 31104    {
 31105        if (this.isOn)
 24106        {
 24107            this.pSystem.Play();
 24108        }
 109        else
 7110        {
 7111            this.pSystem.Stop();
 7112        }
 31113        if (notify)
 4114        {
 4115            this.NotifyAllNeighbors();
 4116            FireController.Instance.FireChanged(this.isOn ? 1 : -1);
 4117        }
 31118    }
 119
 120    public void WaterHit(Vector3 normal)
 121    {
 122        // Reset timer when hit
 123        this.isTurningOff = true;
 124        this.currentHitInterval = 0f;
 125    }
 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)
 52150                {
 52151                    this.onNeighbors++;
 52152                }
 72153            }
 729154        }
 155        // Check on the neighbor state
 27156        this.CheckNeighborState();
 27157    }
 158
 159    private void CheckNeighborState()
 42160    {
 161        /*
 162            If we are over the neighbor limit, the fire is off and is not turning on,
 163            it should start counting
 164        */
 42165        if (this.onNeighbors >= this.onNeighborsThreshold)
 34166        {
 34167            if (!this.isOn && !this.shouldTurnOn)
 4168            {
 4169                this.shouldTurnOn = true;
 4170                this.timeToOnDelta = 0f;
 171                // Find if any of the neihbors is down
 4172                this.hasDownNeighbor = false;
 57173                foreach (Fire neighbor in this.neighbors) {
 15174                    this.hasDownNeighbor = this.hasDownNeighbor || (this.transform.position.y - neighbor.transform.posit
 15175                }
 4176            }
 34177        }
 178        else
 8179        {
 8180            this.shouldTurnOn = false;
 8181            this.timeToOnDelta = 0f;
 8182        }
 42183    }
 184
 185    private void NotifyAllNeighbors()
 4186    {
 57187        foreach (Fire fire in this.neighbors) fire.NotifyNeighborState(this.isOn);
 4188    }
 189
 190    /* ------------------------------------------------------------------------------ */
 191    /* DEBUG */
 192    /* ------------------------------------------------------------------------------ */
 193
 194    void OnDrawGizmosSelected()
 195    {
 196        if (debug)
 197        {
 198            // Display the explosion radius when selected
 199            Gizmos.color = Color.red;
 200            Gizmos.DrawWireSphere(this.transform.position, this.maxNeighborDistance);
 201        }
 202    }
 203
 204    private IEnumerator TurnOffSprite()
 205    {
 206        Component[] particleSystems = GetComponentsInChildren(typeof(ParticleSystem));
 207        for (int i = 0; i < 10; i++)
 208        {
 209            for(int s=0; s < particleSystems.Length; s++)
 210            {
 211                ParticleSystem system = (ParticleSystem)particleSystems[s];
 212                ParticleSystem.MainModule main = system.main;
 213                main.maxParticles = system.main.maxParticles / 2;
 214                yield return new WaitForSeconds(0.01f);
 215            }
 216        }
 217        this.pSystem.Stop();
 218        for (int s = 0; s < particleSystems.Length; s++)
 219        {
 220            ParticleSystem system = (ParticleSystem)particleSystems[s];
 221            system.Stop();
 222        }
 223    }
 224
 225    private IEnumerator TurnOnSprite()
 226    {
 227        Component[] particleSystems = GetComponentsInChildren(typeof(ParticleSystem));
 228        for (int s = 0; s < particleSystems.Length; s++)
 229        {
 230            ParticleSystem system = (ParticleSystem)particleSystems[s];
 231            ParticleSystem.MainModule main = system.main;
 232            main.maxParticles = 1000;
 233        }
 234        this.pSystem.Play();
 235        for (int s = 0; s < particleSystems.Length; s++)
 236        {
 237            ParticleSystem system = (ParticleSystem)particleSystems[s];
 238            system.Play();
 239        }
 240        yield return new WaitForSeconds(0.01f);
 241    }
 242
 243
 244}