| | | 1 | | using System.Linq; |
| | | 2 | | using UnityEngine; |
| | | 3 | | |
| | | 4 | | public class FireController : Framework.MonoBehaviorSingleton<FireController> |
| | | 5 | | { |
| | | 6 | | [Header("Fires")] |
| | | 7 | | [SerializeField] |
| | 2 | 8 | | private int totalLitFires = 0; |
| | | 9 | | private IFireDependant[] fireDependants; |
| | | 10 | | private Fire[] fires; |
| | | 11 | | [Range(0,1)] |
| | | 12 | | public float initialFireProbability; |
| | | 13 | | public AudienceController audienceController; |
| | | 14 | | public float maximumTurnOffLimit; |
| | | 15 | | |
| | | 16 | | void Start() |
| | 1 | 17 | | { |
| | | 18 | | // Get all the fire dependents |
| | 1 | 19 | | this.fireDependants = FindObjectsOfType<MonoBehaviour>().OfType<IFireDependant>().ToArray(); |
| | | 20 | | // Get all the fires |
| | 1 | 21 | | this.fires = FindObjectsOfType<Fire>(); |
| | | 22 | | // Initialize the fires to their on/off state |
| | | 23 | | // Must be done before initializing neighbors to avoid errors |
| | 84 | 24 | | foreach (Fire fire in this.fires) { |
| | 27 | 25 | | fire.SetIsOn(Random.Range(0f, 1f) <= this.initialFireProbability); |
| | 42 | 26 | | if (fire.isOn) this.totalLitFires++; |
| | 27 | 27 | | } |
| | | 28 | | // Initialize the fires to find their neighbors |
| | 83 | 29 | | for (int i = 0; i < this.fires.Length; i++) { |
| | 27 | 30 | | this.fires[i].FindNeighbors(this.fires, i); |
| | 27 | 31 | | } |
| | | 32 | | // Initialize visuals |
| | 1 | 33 | | FireVisualsController.Instance.InitializeVisuals(); |
| | | 34 | | // Set the start time |
| | 1 | 35 | | State.Instance.Reset(); |
| | 1 | 36 | | State.Instance.approvalLimit = this.maximumTurnOffLimit; |
| | 1 | 37 | | State.Instance.hasPlayed = true; |
| | 1 | 38 | | State.Instance.startTime = Time.time; |
| | 1 | 39 | | } |
| | | 40 | | |
| | 7 | 41 | | public void FireChanged(int delta) { |
| | 7 | 42 | | this.totalLitFires += delta; |
| | 7 | 43 | | this.NotifyAllFireDependants(); |
| | | 44 | | // GAME OVER SITUATION |
| | 7 | 45 | | if (this.totalLitFires == 0) { |
| | 0 | 46 | | State.Instance.endTime = Time.time; |
| | 0 | 47 | | audienceController.StartVictory(); |
| | 0 | 48 | | } |
| | 7 | 49 | | } |
| | | 50 | | |
| | | 51 | | void NotifyAllFireDependants() |
| | 7 | 52 | | { |
| | 91 | 53 | | foreach (IFireDependant fd in this.fireDependants) |
| | 35 | 54 | | { |
| | 35 | 55 | | fd.NotifyFireChange(this.totalLitFires); |
| | 35 | 56 | | } |
| | 7 | 57 | | } |
| | | 58 | | |
| | 3 | 59 | | public int GetTotalFires() { |
| | 3 | 60 | | return this.fires.Length; |
| | 3 | 61 | | } |
| | | 62 | | } |