| | | 1 | | using System.Collections.Generic; |
| | | 2 | | using System.Collections; |
| | | 3 | | using UnityEngine; |
| | | 4 | | |
| | | 5 | | public class FireVisualsController : Framework.MonoBehaviorSingleton<FireVisualsController>, IFireDependant |
| | | 6 | | { |
| | | 7 | | [Header("Flicker")] |
| | | 8 | | public Vector2 flickerIntensity; |
| | 2 | 9 | | public float flickerMultiplier = 1f; |
| | 2 | 10 | | public float timeToFlicker = 0.5f; |
| | 2 | 11 | | public float flickerDelta = 0f; |
| | | 12 | | |
| | | 13 | | [Header("Sounds")] |
| | | 14 | | public AudioSource[] audioSources; |
| | | 15 | | public AudioClip[] breakingSounds; |
| | | 16 | | public float timeToBreak; |
| | 2 | 17 | | 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; |
| | 2 | 26 | | public float maxFireDistance = 10f; |
| | | 27 | | |
| | | 28 | | [Header("Smoke")] |
| | | 29 | | public ParticleSystem[] allSmoke; |
| | | 30 | | public Vector2 lifetimeSettings; |
| | | 31 | | private int totalFires; |
| | | 32 | | |
| | | 33 | | [Header("Debug")] |
| | 2 | 34 | | public bool debug = false; |
| | | 35 | | |
| | | 36 | | void Start() |
| | 1 | 37 | | { |
| | | 38 | | // Separate them according to the closest list |
| | 1 | 39 | | this.fires = new List<Fire>[this.lights.Length]; |
| | | 40 | | // Store the original intensities and positions |
| | 1 | 41 | | this.lightPositions = new Vector3[this.lights.Length]; |
| | 1 | 42 | | this.lightIntensities = new float[this.lights.Length]; |
| | 1 | 43 | | } |
| | | 44 | | |
| | | 45 | | void Update() |
| | 22125 | 46 | | { |
| | | 47 | | // Light flickering |
| | 22125 | 48 | | if (this.flickerDelta >= this.timeToFlicker) |
| | 22123 | 49 | | { |
| | 22123 | 50 | | this.flickerDelta = 0f; |
| | 22123 | 51 | | this.FlickerLights(); |
| | 22123 | 52 | | } |
| | 22125 | 53 | | this.flickerDelta += Time.deltaTime; |
| | | 54 | | // Breaking sounds |
| | 22125 | 55 | | if (this.breakDelta >= this.timeToBreak) |
| | 66 | 56 | | { |
| | 66 | 57 | | this.breakDelta = 0f; |
| | 66 | 58 | | this.BreakSounds(); |
| | 66 | 59 | | } |
| | 22125 | 60 | | this.breakDelta += Time.deltaTime; |
| | 22125 | 61 | | } |
| | | 62 | | |
| | | 63 | | public void NotifyFireChange(int fireCount) |
| | 4 | 64 | | { |
| | 4 | 65 | | this.DetermineLightPositions(); |
| | 4 | 66 | | this.UpdateSmoke(fireCount); |
| | 4 | 67 | | } |
| | | 68 | | |
| | | 69 | | /* ------------------------------------------------------------------------------ */ |
| | | 70 | | /* SMOKE HANDLING */ |
| | | 71 | | /* ------------------------------------------------------------------------------ */ |
| | | 72 | | |
| | | 73 | | private void UpdateSmoke(int fireCount) |
| | 4 | 74 | | { |
| | 28 | 75 | | foreach (ParticleSystem smoke in this.allSmoke) |
| | 8 | 76 | | { |
| | 8 | 77 | | var main = smoke.main; |
| | 8 | 78 | | main.startLifetime = this.lifetimeSettings.x + Mathf.Clamp(fireCount / (float)this.totalFires, 0f, 1f) * (th |
| | 8 | 79 | | } |
| | 4 | 80 | | } |
| | | 81 | | |
| | | 82 | | /* ------------------------------------------------------------------------------ */ |
| | | 83 | | /* SOUND HANDLING */ |
| | | 84 | | /* ------------------------------------------------------------------------------ */ |
| | | 85 | | |
| | | 86 | | private void BreakSounds() |
| | 66 | 87 | | { |
| | 396 | 88 | | for (int i = 0; i < this.lights.Length; i++) |
| | 132 | 89 | | { |
| | | 90 | | // If light is active, play the sound with a random delay |
| | 132 | 91 | | if (this.lights[i].gameObject.activeInHierarchy) |
| | 132 | 92 | | { |
| | 132 | 93 | | StartCoroutine(this.PlaySound(this.audioSources[i], this.breakingSounds[Random.Range(0, this.breakingSou |
| | 132 | 94 | | } |
| | 132 | 95 | | } |
| | 66 | 96 | | } |
| | | 97 | | |
| | | 98 | | IEnumerator PlaySound(AudioSource audioSource, AudioClip clip, float delay) |
| | 132 | 99 | | { |
| | 132 | 100 | | yield return new WaitForSeconds(delay); |
| | 132 | 101 | | audioSource.PlayOneShot(clip); |
| | 132 | 102 | | } |
| | | 103 | | |
| | | 104 | | /* ------------------------------------------------------------------------------ */ |
| | | 105 | | /* FIRE HANDLING */ |
| | | 106 | | /* ------------------------------------------------------------------------------ */ |
| | | 107 | | |
| | | 108 | | private void SeparateFires() |
| | 2 | 109 | | { |
| | | 110 | | // Get all the fires |
| | 2 | 111 | | Fire[] allFires = FindObjectsOfType<Fire>(); |
| | | 112 | | // Separate them according to the nearest neighbor |
| | | 113 | | List<Fire> tmp; |
| | 12 | 114 | | for (int i = 0; i < this.lights.Length; i++) |
| | 4 | 115 | | { |
| | 4 | 116 | | tmp = new List<Fire>(); |
| | 228 | 117 | | foreach (Fire f in allFires) |
| | 108 | 118 | | { |
| | 108 | 119 | | if (Vector3.Distance(this.lights[i].transform.position, f.transform.position) <= this.maxFireDistance) |
| | 66 | 120 | | { |
| | 66 | 121 | | tmp.Add(f); |
| | 66 | 122 | | } |
| | 108 | 123 | | } |
| | 4 | 124 | | this.fires[i] = tmp; |
| | 4 | 125 | | } |
| | 2 | 126 | | } |
| | | 127 | | |
| | | 128 | | private void DetermineLightPositions() |
| | 6 | 129 | | { |
| | 36 | 130 | | for (int i = 0; i < this.lights.Length; i++) |
| | 12 | 131 | | { |
| | 12 | 132 | | float newY = 0; |
| | 12 | 133 | | float newX = 0; |
| | 12 | 134 | | int on = 0; |
| | 448 | 135 | | foreach (Fire fire in this.fires[i]) |
| | 206 | 136 | | { |
| | 206 | 137 | | newY += (fire.isOn ? fire.transform.position.y : 0); |
| | 206 | 138 | | newX += (fire.isOn ? fire.transform.position.x : 0); |
| | 206 | 139 | | on += (fire.isOn ? 1 : 0); |
| | 206 | 140 | | } |
| | | 141 | | // If no more neighbors are on, turn the light off |
| | 12 | 142 | | if (this.lights[i].gameObject.activeInHierarchy && on == 0) |
| | 0 | 143 | | { |
| | 0 | 144 | | this.lights[i].gameObject.SetActive(false); |
| | 0 | 145 | | } |
| | 12 | 146 | | else if (!this.lights[i].gameObject.activeInHierarchy && on > 0) |
| | 0 | 147 | | { |
| | 0 | 148 | | this.lights[i].gameObject.SetActive(true); |
| | 0 | 149 | | } |
| | | 150 | | // If there are on fires, move position |
| | 12 | 151 | | if (on > 0) |
| | 12 | 152 | | { |
| | 12 | 153 | | this.lights[i].transform.position = new Vector3(newX / this.fires[i].Count, newY / this.fires[i].Count, |
| | 12 | 154 | | this.lightPositions[i] = this.lights[i].transform.position; |
| | 12 | 155 | | } |
| | 12 | 156 | | } |
| | 6 | 157 | | } |
| | | 158 | | |
| | | 159 | | public void InitializeVisuals() |
| | 2 | 160 | | { |
| | 2 | 161 | | this.SeparateFires(); |
| | 2 | 162 | | this.DetermineLightPositions(); |
| | 2 | 163 | | this.totalFires = FireController.Instance.GetTotalFires(); |
| | 2 | 164 | | } |
| | | 165 | | |
| | | 166 | | public void FlickerLights() |
| | 22125 | 167 | | { |
| | | 168 | | Vector3 delta; |
| | | 169 | | float newIntensity; |
| | 132750 | 170 | | for (int i = 0; i < this.lights.Length; i++) |
| | 44250 | 171 | | { |
| | 44250 | 172 | | delta = new Vector3(Random.Range(0f, 1f) * this.flickerMultiplier, Random.Range(0f, 1f) * this.flickerMultip |
| | 44250 | 173 | | newIntensity = Random.Range(this.flickerIntensity[0], this.flickerIntensity[1]); |
| | 44250 | 174 | | this.lights[i].transform.position = this.lightPositions[i] + delta; |
| | 44250 | 175 | | this.lights[i].intensity = newIntensity; |
| | 44250 | 176 | | } |
| | 22125 | 177 | | } |
| | | 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 | | } |