< 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()
 1160946    {
 47        // Light flickering
 1160948        if (this.flickerDelta >= this.timeToFlicker)
 1137849        {
 1137850            this.flickerDelta = 0f;
 1137851            this.FlickerLights();
 1137852        }
 1160953        this.flickerDelta += Time.deltaTime;
 54        // Breaking sounds
 1160955        if (this.breakDelta >= this.timeToBreak)
 4356        {
 4357            this.breakDelta = 0f;
 4358            this.BreakSounds();
 4359        }
 1160960        this.breakDelta += Time.deltaTime;
 1160961    }
 62
 63    public void NotifyFireChange(int fireCount)
 564    {
 565        this.DetermineLightPositions();
 566        this.UpdateSmoke(fireCount);
 567    }
 68
 69    /* ------------------------------------------------------------------------------ */
 70    /* SMOKE HANDLING */
 71    /* ------------------------------------------------------------------------------ */
 72
 73    private void UpdateSmoke(int fireCount)
 574    {
 3575        foreach (ParticleSystem smoke in this.allSmoke)
 1076        {
 1077            var main = smoke.main;
 1078            main.startLifetime = this.lifetimeSettings.x + Mathf.Clamp(fireCount / (float)this.totalFires, 0f, 1f) * (th
 1079        }
 580    }
 81
 82    /* ------------------------------------------------------------------------------ */
 83    /* SOUND HANDLING */
 84    /* ------------------------------------------------------------------------------ */
 85
 86    private void BreakSounds()
 4387    {
 25888        for (int i = 0; i < this.lights.Length; i++)
 8689        {
 90            // If light is active, play the sound with a random delay
 8691            if (this.lights[i].gameObject.activeInHierarchy)
 8692            {
 8693                StartCoroutine(this.PlaySound(this.audioSources[i], this.breakingSounds[Random.Range(0, this.breakingSou
 8694            }
 8695        }
 4396    }
 97
 98    IEnumerator PlaySound(AudioSource audioSource, AudioClip clip, float delay)
 8699    {
 86100        yield return new WaitForSeconds(delay);
 86101        audioSource.PlayOneShot(clip);
 86102    }
 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)
 65120                {
 65121                    tmp.Add(f);
 65122                }
 108123            }
 4124            this.fires[i] = tmp;
 4125        }
 2126    }
 127
 128    private void DetermineLightPositions()
 7129    {
 42130        for (int i = 0; i < this.lights.Length; i++)
 14131        {
 14132            float newY = 0;
 14133            float newX = 0;
 14134            int on = 0;
 512135            foreach (Fire fire in this.fires[i])
 235136            {
 235137                newY += (fire.isOn ? fire.transform.position.y : 0);
 235138                newX += (fire.isOn ? fire.transform.position.x : 0);
 235139                on += (fire.isOn ? 1 : 0);
 235140            }
 141            // If no more neighbors are on, turn the light off
 14142            if (this.lights[i].gameObject.activeInHierarchy && on == 0)
 0143            {
 0144                this.lights[i].gameObject.SetActive(false);
 0145            }
 14146            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
 14151            if (on > 0)
 14152            {
 14153                this.lights[i].transform.position = new Vector3(newX / this.fires[i].Count, newY / this.fires[i].Count, 
 14154                this.lightPositions[i] = this.lights[i].transform.position;
 14155            }
 14156        }
 7157    }
 158
 159    public void InitializeVisuals()
 2160    {
 2161        this.SeparateFires();
 2162        this.DetermineLightPositions();
 2163        this.totalFires = FireController.Instance.GetTotalFires();
 2164    }
 165
 166    public void FlickerLights()
 11380167    {
 168        Vector3 delta;
 169        float newIntensity;
 68280170        for (int i = 0; i < this.lights.Length; i++)
 22760171        {
 22760172            delta = new Vector3(Random.Range(0f, 1f) * this.flickerMultiplier, Random.Range(0f, 1f) * this.flickerMultip
 22760173            newIntensity = Random.Range(this.flickerIntensity[0], this.flickerIntensity[1]);
 22760174            this.lights[i].transform.position = this.lightPositions[i] + delta;
 22760175            this.lights[i].intensity = newIntensity;
 22760176        }
 11380177    }
 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}