< Summary

Class:FireVisualsController
Assembly:Test
File(s):E:/Unity/Unity Project/vr-firefighter-simulator/Assets/Test/FireVisualsController.cs
Covered lines:99
Uncovered lines:6
Coverable lines:105
Total lines:195
Line coverage:94.2% (99 of 105)
Covered branches:0
Total branches:0
Covered methods:11
Total methods:11
Method coverage:100% (11 of 11)

Coverage History

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
FireVisualsController()0%000100%
Start()0%000100%
Update()0%000100%
NotifyFireChange(...)0%000100%
UpdateSmoke(...)0%000100%
BreakSounds()0%000100%
PlaySound()0%000100%
SeparateFires()0%000100%
DetermineLightPositions()0%00081.25%
InitializeVisuals()0%000100%
FlickerLights()0%000100%

File(s)

E:/Unity/Unity Project/vr-firefighter-simulator/Assets/Test/FireVisualsController.cs

#LineLine coverage
 1using System.Collections.Generic;
 2using System.Collections;
 3using UnityEngine;
 4
 5public class FireVisualsController : Framework.MonoBehaviorSingleton<FireVisualsController>, IFireDependant
 6{
 7    [Header("Flicker")]
 8    public Vector2 flickerIntensity;
 29    public float flickerMultiplier = 1f;
 210    public float timeToFlicker = 0.5f;
 211    public float flickerDelta = 0f;
 12
 13    [Header("Sounds")]
 14    public AudioSource[] audioSources;
 15    public AudioClip[] breakingSounds;
 16    public float timeToBreak;
 217    public float breakDelta = 0f;
 18    public Vector2 delayLimits;
 19
 20    [Header("Lights")]
 21    public Light[] lights;
 22    public Vector3[] lightPositions;
 23    public float[] lightIntensities;
 24    [SerializeField]
 25    public List<Fire>[] fires;
 226    public float maxFireDistance = 10f;
 27
 28    [Header("Smoke")]
 29    public ParticleSystem[] allSmoke;
 30    public Vector2 lifetimeSettings;
 31    private int totalFires;
 32
 33    [Header("Debug")]
 234    public bool debug = false;
 35
 36    void Start()
 137    {
 38        // Separate them according to the closest list
 139        this.fires = new List<Fire>[this.lights.Length];
 40        // Store the original intensities and positions
 141        this.lightPositions = new Vector3[this.lights.Length];
 142        this.lightIntensities = new float[this.lights.Length];
 143    }
 44
 45    void Update()
 2212546    {
 47        // Light flickering
 2212548        if (this.flickerDelta >= this.timeToFlicker)
 2212349        {
 2212350            this.flickerDelta = 0f;
 2212351            this.FlickerLights();
 2212352        }
 2212553        this.flickerDelta += Time.deltaTime;
 54        // Breaking sounds
 2212555        if (this.breakDelta >= this.timeToBreak)
 6656        {
 6657            this.breakDelta = 0f;
 6658            this.BreakSounds();
 6659        }
 2212560        this.breakDelta += Time.deltaTime;
 2212561    }
 62
 63    public void NotifyFireChange(int fireCount)
 464    {
 465        this.DetermineLightPositions();
 466        this.UpdateSmoke(fireCount);
 467    }
 68
 69    /* ------------------------------------------------------------------------------ */
 70    /* SMOKE HANDLING */
 71    /* ------------------------------------------------------------------------------ */
 72
 73    private void UpdateSmoke(int fireCount)
 474    {
 2875        foreach (ParticleSystem smoke in this.allSmoke)
 876        {
 877            var main = smoke.main;
 878            main.startLifetime = this.lifetimeSettings.x + Mathf.Clamp(fireCount / (float)this.totalFires, 0f, 1f) * (th
 879        }
 480    }
 81
 82    /* ------------------------------------------------------------------------------ */
 83    /* SOUND HANDLING */
 84    /* ------------------------------------------------------------------------------ */
 85
 86    private void BreakSounds()
 6687    {
 39688        for (int i = 0; i < this.lights.Length; i++)
 13289        {
 90            // If light is active, play the sound with a random delay
 13291            if (this.lights[i].gameObject.activeInHierarchy)
 13292            {
 13293                StartCoroutine(this.PlaySound(this.audioSources[i], this.breakingSounds[Random.Range(0, this.breakingSou
 13294            }
 13295        }
 6696    }
 97
 98    IEnumerator PlaySound(AudioSource audioSource, AudioClip clip, float delay)
 13299    {
 132100        yield return new WaitForSeconds(delay);
 132101        audioSource.PlayOneShot(clip);
 132102    }
 103
 104    /* ------------------------------------------------------------------------------ */
 105    /* FIRE HANDLING */
 106    /* ------------------------------------------------------------------------------ */
 107
 108    private void SeparateFires()
 2109    {
 110        // Get all the fires
 2111        Fire[] allFires = FindObjectsOfType<Fire>();
 112        // Separate them according to the nearest neighbor
 113        List<Fire> tmp;
 12114        for (int i = 0; i < this.lights.Length; i++)
 4115        {
 4116            tmp = new List<Fire>();
 228117            foreach (Fire f in allFires)
 108118            {
 108119                if (Vector3.Distance(this.lights[i].transform.position, f.transform.position) <= this.maxFireDistance)
 66120                {
 66121                    tmp.Add(f);
 66122                }
 108123            }
 4124            this.fires[i] = tmp;
 4125        }
 2126    }
 127
 128    private void DetermineLightPositions()
 6129    {
 36130        for (int i = 0; i < this.lights.Length; i++)
 12131        {
 12132            float newY = 0;
 12133            float newX = 0;
 12134            int on = 0;
 448135            foreach (Fire fire in this.fires[i])
 206136            {
 206137                newY += (fire.isOn ? fire.transform.position.y : 0);
 206138                newX += (fire.isOn ? fire.transform.position.x : 0);
 206139                on += (fire.isOn ? 1 : 0);
 206140            }
 141            // If no more neighbors are on, turn the light off
 12142            if (this.lights[i].gameObject.activeInHierarchy && on == 0)
 0143            {
 0144                this.lights[i].gameObject.SetActive(false);
 0145            }
 12146            else if (!this.lights[i].gameObject.activeInHierarchy && on > 0)
 0147            {
 0148                this.lights[i].gameObject.SetActive(true);
 0149            }
 150            // If there are on fires, move position
 12151            if (on > 0)
 12152            {
 12153                this.lights[i].transform.position = new Vector3(newX / this.fires[i].Count, newY / this.fires[i].Count, 
 12154                this.lightPositions[i] = this.lights[i].transform.position;
 12155            }
 12156        }
 6157    }
 158
 159    public void InitializeVisuals()
 2160    {
 2161        this.SeparateFires();
 2162        this.DetermineLightPositions();
 2163        this.totalFires = FireController.Instance.GetTotalFires();
 2164    }
 165
 166    public void FlickerLights()
 22125167    {
 168        Vector3 delta;
 169        float newIntensity;
 132750170        for (int i = 0; i < this.lights.Length; i++)
 44250171        {
 44250172            delta = new Vector3(Random.Range(0f, 1f) * this.flickerMultiplier, Random.Range(0f, 1f) * this.flickerMultip
 44250173            newIntensity = Random.Range(this.flickerIntensity[0], this.flickerIntensity[1]);
 44250174            this.lights[i].transform.position = this.lightPositions[i] + delta;
 44250175            this.lights[i].intensity = newIntensity;
 44250176        }
 22125177    }
 178
 179    /* ------------------------------------------------------------------------------ */
 180    /* DEBUG */
 181    /* ------------------------------------------------------------------------------ */
 182
 183    void OnDrawGizmosSelected()
 184    {
 185        if (this.debug)
 186        {
 187            // Display the explosion radius when selected
 188            Gizmos.color = Color.red;
 189            foreach (Light l in this.lights)
 190            {
 191                Gizmos.DrawSphere(l.transform.position, this.maxFireDistance);
 192            }
 193        }
 194    }
 195}