using System; using System.Collections; using System.Collections.Generic; using PixelCrushers.DialogueSystem; using Sirenix.OdinInspector; using PixelCrushers; using PixelCrushers.QuestMachine; using Sirenix.Utilities; using UnityEngine; using UnityEngine.Events; namespace Beyond { public class DayNightSettings : Saver, IMessageHandler { [ToggleGroup("m_enableNightConditions", "NightConditions")] public bool m_enableNightConditions; [ToggleGroup("m_enableNightConditions", "NightConditions")] public Condition[] m_nightCondition; [ToggleGroup("m_enableDayConditions", "DayConditions")] public bool m_enableDayConditions; [ToggleGroup("m_enableDayConditions", "DayConditions")] public Condition m_dayCondition; public class SaveData { public bool isNight = false; } private SaveData m_saveData = null; [Serializable] public struct Settings { public Settings(float fd, Color fc, Material sbM, Color aSkyC, Color aGrC, Color aEqC) { fogDensity = fd; fogColor = fc; skyboxMat = sbM; ambientSkyColor = aSkyC; ambientGroundColor = aGrC; ambientEquatorColor = aEqC; toEnable = null; toDisable = null; changeEvent = null; } public float fogDensity; public Color fogColor; public Material skyboxMat; public Color ambientSkyColor; public Color ambientGroundColor; public Color ambientEquatorColor; public GameObject[] toEnable; public GameObject[] toDisable; public UnityEvent changeEvent; } public Settings m_daySettings = new Settings(0.01f, Color.cyan, null, Color.blue, Color.blue, Color.blue); public Settings m_nightSettings = new Settings(0.01f, Color.cyan, null, Color.blue, Color.blue, Color.blue); public enum StartAction { NOTHING, SET_DAY, SET_NIGHT } public StartAction m_startAction = StartAction.NOTHING; [Button] public void SetDay() { ApplySettings(m_daySettings); m_saveData.isNight = false; } [Button] public void SetNight() { ApplySettings(m_nightSettings); m_saveData.isNight = true; } private void Awake() { m_saveData = new SaveData(); MessageSystem.AddListener(this, QuestMachineMessages.QuestStateChangedMessage, string.Empty); PixelCrushers.SaveSystem.saveDataApplied += OnSaveDataApplied; } private void OnDestroy() { PixelCrushers.SaveSystem.saveDataApplied -= OnSaveDataApplied; MessageSystem.RemoveListener(this, QuestMachineMessages.QuestStateChangedMessage, string.Empty); } public override string RecordData() { return SaveSystem.Serialize(m_saveData); } public override void ApplyData(string s) { var data = SaveSystem.Deserialize(s); if (data == null) data = new SaveData(); if (data.isNight) { SetNight(); } else { SetDay(); } } void OnSaveDataApplied() { //PixelCrushers.SaveSystem.saveDataApplied -= OnSaveDataApplied; StartCoroutine(OnSaveDataCoroutine()); } IEnumerator OnSaveDataCoroutine() { yield return null; SetTimeOfDayAccordingToConditions(); yield return null; } private void SetTimeOfDayAccordingToConditions() { if (m_enableDayConditions && m_dayCondition.IsTrue(null)) { SetDay(); } //else if (m_enableNightConditions && m_nightCondition.IsTrue(null)) else if (m_enableNightConditions) { foreach (var c in m_nightCondition) { if (c.IsTrue(null)) { SetNight(); break; } } } } void ApplySettings(Settings settings) { RenderSettings.fogDensity = settings.fogDensity; RenderSettings.fogColor = settings.fogColor; if (settings.skyboxMat != null) RenderSettings.skybox = settings.skyboxMat; RenderSettings.ambientSkyColor = settings.ambientSkyColor; RenderSettings.ambientGroundColor = settings.ambientGroundColor; RenderSettings.ambientEquatorColor = settings.ambientEquatorColor; if (settings.toEnable != null) { foreach (var o in settings.toEnable) o?.SetActive(true); } if (settings.toDisable != null) { foreach (var o in settings.toDisable) o?.SetActive(false); } settings.changeEvent?.Invoke(); } // Start is called before the first frame update void Start() { switch (m_startAction) { case StartAction.SET_DAY: SetDay(); break; case StartAction.SET_NIGHT: SetNight(); break; } } public void OnMessage(MessageArgs messageArgs) { switch (messageArgs.message) { case QuestMachineMessages.QuestStateChangedMessage: SetTimeOfDayAccordingToConditions(); break; case QuestMachineMessages.QuestCounterChangedMessage: case QuestMachineMessages.RefreshUIsMessage: case QuestMachineMessages.QuestTrackToggleChangedMessage: break; } } } }