using System.Collections; using System.Collections.Generic; using UnityEngine; public class DayNightCycle : MonoBehaviour { public interface IDaytimeSensitive { void OnDusk(); void OnDawn_AfterSunrise(); void OnDawn_BeforeSunrise(); } public enum Timestate { Day, Night } private static DayNightCycle instance; public float sunriseTime = 2.5f; private Timestate currentTimestate; private float currentNightLength; private List daytimeSensitiveObjects = new List(); private bool afterSunrise = true; public static DayNightCycle Instance => instance; public Timestate CurrentTimestate => currentTimestate; public float CurrentNightLength => currentNightLength; public bool AfterSunrise => afterSunrise; public int CoinCountToBeHarvested { get { int num = 0; foreach (BuildingInteractor playerBuildingInteractor in TagManager.instance.playerBuildingInteractors) { if ((bool)playerBuildingInteractor.coinSpawner) { num += playerBuildingInteractor.coinSpawner.CoinsLeft; } if (playerBuildingInteractor.canBeHarvested) { num += playerBuildingInteractor.GoldIncome; } } return num; } } private void Awake() { if (instance != null) { Object.Destroy(this); } else { instance = this; } } private void Update() { if (LocalGamestate.Instance.CurrentState == LocalGamestate.State.InMatch) { if (currentTimestate == Timestate.Night) { currentNightLength += Time.deltaTime; } if (currentTimestate == Timestate.Night && (bool)TagManager.instance && (bool)EnemySpawner.instance && !EnemySpawner.instance.SpawningInProgress && TagManager.instance.CountAllTaggedObjectsWithTag(TagManager.ETag.EnemyOwned) < 1 && base.gameObject.activeInHierarchy) { StartCoroutine(SwitchToDayCoroutine()); } } } private void DawnCallAfterSunrise() { afterSunrise = true; Hp.ReviveAllKnockedOutPlayerUnitsAndBuildings(); for (int num = daytimeSensitiveObjects.Count - 1; num >= 0; num--) { if (daytimeSensitiveObjects[num] != null) { daytimeSensitiveObjects[num].OnDawn_AfterSunrise(); } else { daytimeSensitiveObjects.RemoveAt(num); } } ThronefallAudioManager.Oneshot(ThronefallAudioManager.AudioOneShot.BuildingRepair); LevelData levelDataForActiveScene = LevelProgressManager.instance.GetLevelDataForActiveScene(); int networth = PlayerInteraction.instance.Networth; networth += TagManager.instance.freeCoins.Count; networth += CoinCountToBeHarvested; levelDataForActiveScene.dayToDayNetworth.Add(networth); PlayerInteraction component = TagManager.instance.Players[0].GetComponent(); foreach (Coin freeCoin in TagManager.instance.freeCoins) { if (freeCoin.IsFree) { freeCoin.SetTarget(component); } } } private void DawnCallBeforeSunrise() { afterSunrise = false; for (int num = daytimeSensitiveObjects.Count - 1; num >= 0; num--) { if (daytimeSensitiveObjects[num] != null) { daytimeSensitiveObjects[num].OnDawn_BeforeSunrise(); } else { daytimeSensitiveObjects.RemoveAt(num); } } ThronefallAudioManager.Oneshot(ThronefallAudioManager.AudioOneShot.NightSurvived); } private void DuskCall() { afterSunrise = false; currentNightLength = 0f; for (int num = daytimeSensitiveObjects.Count - 1; num >= 0; num--) { if (daytimeSensitiveObjects[num] != null) { daytimeSensitiveObjects[num].OnDusk(); } else { daytimeSensitiveObjects.RemoveAt(num); } } } public void ToggleDaytime() { if (currentTimestate == Timestate.Day) { currentTimestate = Timestate.Night; DuskCall(); } else { currentTimestate = Timestate.Day; DawnCallBeforeSunrise(); DawnCallAfterSunrise(); } } private IEnumerator SwitchToDayCoroutine() { if (currentTimestate == Timestate.Night) { currentTimestate = Timestate.Day; DawnCallBeforeSunrise(); yield return new WaitForSeconds(sunriseTime); DawnCallAfterSunrise(); if (EnemySpawner.instance.Wavenumber >= EnemySpawner.instance.waves.Count - 1) { LocalGamestate.Instance.SetState(LocalGamestate.State.AfterMatchVictory); } } } public void SwitchToNight() { if (currentTimestate != Timestate.Night) { currentTimestate = Timestate.Night; DuskCall(); } } public void RegisterDaytimeSensitiveObject(IDaytimeSensitive obj) { daytimeSensitiveObjects.Add(obj); } public void UnregisterDaytimeSensitiveObject(IDaytimeSensitive obj) { daytimeSensitiveObjects.Remove(obj); } }