< Summary

Class:FireVisualsController
Assembly:Test
File(s):D:/--UnityProject/VR/_____ISSTA 26/vr-firefighter-simulator/Assets/Test/FireVisualsController.cs
Covered lines:99
Uncovered lines:16
Coverable lines:115
Total lines:195
Line coverage:86% (99 of 115)
Covered branches:0
Total branches:0
Covered methods:11
Total methods:12
Method coverage:91.6% (11 of 12)

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%
OnDrawGizmosSelected()0%0000%

File(s)

D:/--UnityProject/VR/_____ISSTA 26/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()
 1265646    {
 47        // Light flickering
 1265648        if (this.flickerDelta >= this.timeToFlicker)
 1265449        {
 1265450            this.flickerDelta = 0f;
 1265451            this.FlickerLights();
 1265452        }
 1265653        this.flickerDelta += Time.deltaTime;
 54        // Breaking sounds
 1265655        if (this.breakDelta >= this.timeToBreak)
 7256        {
 7257            this.breakDelta = 0f;
 7258            this.BreakSounds();
 7259        }
 1265660        this.breakDelta += Time.deltaTime;
 1265661    }
 62
 63    public void NotifyFireChange(int fireCount)
 864    {
 865        this.DetermineLightPositions();
 866        this.UpdateSmoke(fireCount);
 867    }
 68
 69    /* ------------------------------------------------------------------------------ */
 70    /* SMOKE HANDLING */
 71    /* ------------------------------------------------------------------------------ */
 72
 73    private void UpdateSmoke(int fireCount)
 874    {
 5675        foreach (ParticleSystem smoke in this.allSmoke)
 1676        {
 1677            var main = smoke.main;
 1678            main.startLifetime = this.lifetimeSettings.x + Mathf.Clamp(fireCount / (float)this.totalFires, 0f, 1f) * (th
 1679        }
 880    }
 81
 82    /* ------------------------------------------------------------------------------ */
 83    /* SOUND HANDLING */
 84    /* ------------------------------------------------------------------------------ */
 85
 86    private void BreakSounds()
 7287    {
 43288        for (int i = 0; i < this.lights.Length; i++)
 14489        {
 90            // If light is active, play the sound with a random delay
 14491            if (this.lights[i].gameObject.activeInHierarchy)
 14492            {
 14493                StartCoroutine(this.PlaySound(this.audioSources[i], this.breakingSounds[Random.Range(0, this.breakingSou
 14494            }
 14495        }
 7296    }
 97
 98    IEnumerator PlaySound(AudioSource audioSource, AudioClip clip, float delay)
 14499    {
 144100        yield return new WaitForSeconds(delay);
 143101        audioSource.PlayOneShot(clip);
 143102    }
 103
 104    /* ------------------------------------------------------------------------------ */
 105    /* FIRE HANDLING */
 106    /* ------------------------------------------------------------------------------ */
 107
 108    private void SeparateFires()
 1109    {
 110        // Get all the fires
 1111        Fire[] allFires = FindObjectsOfType<Fire>();
 112        // Separate them according to the nearest neighbor
 113        List<Fire> tmp;
 6114        for (int i = 0; i < this.lights.Length; i++)
 2115        {
 2116            tmp = new List<Fire>();
 114117            foreach (Fire f in allFires)
 54118            {
 54119                if (Vector3.Distance(this.lights[i].transform.position, f.transform.position) <= this.maxFireDistance)
 31120                {
 31121                    tmp.Add(f);
 31122                }
 54123            }
 2124            this.fires[i] = tmp;
 2125        }
 1126    }
 127
 128    private void DetermineLightPositions()
 9129    {
 54130        for (int i = 0; i < this.lights.Length; i++)
 18131        {
 18132            float newY = 0;
 18133            float newX = 0;
 18134            int on = 0;
 612135            foreach (Fire fire in this.fires[i])
 279136            {
 279137                newY += (fire.isOn ? fire.transform.position.y : 0);
 279138                newX += (fire.isOn ? fire.transform.position.x : 0);
 279139                on += (fire.isOn ? 1 : 0);
 279140            }
 141            // If no more neighbors are on, turn the light off
 18142            if (this.lights[i].gameObject.activeInHierarchy && on == 0)
 0143            {
 0144                this.lights[i].gameObject.SetActive(false);
 0145            }
 18146            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
 18151            if (on > 0)
 18152            {
 18153                this.lights[i].transform.position = new Vector3(newX / this.fires[i].Count, newY / this.fires[i].Count, 
 18154                this.lightPositions[i] = this.lights[i].transform.position;
 18155            }
 18156        }
 9157    }
 158
 159    public void InitializeVisuals()
 1160    {
 1161        this.SeparateFires();
 1162        this.DetermineLightPositions();
 1163        this.totalFires = FireController.Instance.GetTotalFires();
 1164    }
 165
 166    public void FlickerLights()
 12655167    {
 168        Vector3 delta;
 169        float newIntensity;
 75930170        for (int i = 0; i < this.lights.Length; i++)
 25310171        {
 25310172            delta = new Vector3(Random.Range(0f, 1f) * this.flickerMultiplier, Random.Range(0f, 1f) * this.flickerMultip
 25310173            newIntensity = Random.Range(this.flickerIntensity[0], this.flickerIntensity[1]);
 25310174            this.lights[i].transform.position = this.lightPositions[i] + delta;
 25310175            this.lights[i].intensity = newIntensity;
 25310176        }
 12655177    }
 178
 179    /* ------------------------------------------------------------------------------ */
 180    /* DEBUG */
 181    /* ------------------------------------------------------------------------------ */
 182
 183    void OnDrawGizmosSelected()
 0184    {
 0185        if (this.debug)
 0186        {
 187            // Display the explosion radius when selected
 0188            Gizmos.color = Color.red;
 0189            foreach (Light l in this.lights)
 0190            {
 0191                Gizmos.DrawSphere(l.transform.position, this.maxFireDistance);
 0192            }
 0193        }
 0194    }
 195}