using UnityEngine; /// --------------------------------------------- /// Senses Pack for Behavior Designer Pro /// Copyright (c) Opsive. All Rights Reserved. /// https://www.opsive.com /// --------------------------------------------- namespace Opsive.BehaviorDesigner.AddOns.SensesPack.Runtime.Emitters { /// /// Represents a temperature value with optional fluctuation over time. /// [System.Serializable] public class TempeatureValue { [Tooltip("The base temperature value.")] public float Value; [Tooltip("An animation curve that defines how the temperature fluctuates over time.")] public AnimationCurve Fluctuation = new AnimationCurve(new Keyframe[] { new Keyframe(0, 0.75f, 0, 0.333f), new Keyframe(0.5f, 1.25f), new Keyframe(1, 0.75f) }); /// /// Evaluates the temperature at a given time, including any fluctuations. /// /// The time to evaluate the temperature at. /// The calculated temperature value. public float Evaluate(float time) { return Value * Fluctuation.Evaluate(time); } } /// /// Manages the global scene temperature, which can vary based on both seasonal (day of year) and daily (time of day) cycles. /// public class SceneTemperature : MonoBehaviour { private static SceneTemperature s_Instance; public static SceneTemperature Instance { get { if (s_Instance == null) { s_Instance = new GameObject("SceneTemperature").AddComponent(); } return s_Instance; } } [Tooltip("The temperature for the season (0-365). Use the Fluctuation curve to define seasonal temperature variations.")] [SerializeField] protected TempeatureValue m_DayTemperature; [Tooltip("The temperature for the time of day (0-24). Use the Fluctuation curve to define daily temperature variations.")] [SerializeField] protected TempeatureValue m_TimeTemperature; [Tooltip("The current day of year (0-365).")] [SerializeField] protected float m_CurrentDay = 0; [Tooltip("The current time of day (0-24).")] [SerializeField] protected float m_CurrentTime = 12; [Tooltip("The speed at which the day progresses (days per second). Set to 0 to keep the day static.")] [SerializeField] protected float m_DayProgressionSpeed = 0; [Tooltip("The speed at which the time of day progresses (hours per second). Set to 0 to keep the time static.")] [SerializeField] protected float m_TimeProgressionSpeed = 0; public float CurrentDay { get => m_CurrentDay; set => m_CurrentDay = value; } public float CurrentTime { get => m_CurrentTime; set => m_CurrentTime = value; } /// /// The object has been enabled. /// private void OnEnable() { s_Instance = this; } /// /// Updates the current day and time based on the progression speeds. /// private void Update() { m_CurrentDay += m_DayProgressionSpeed * Time.deltaTime; m_CurrentTime += m_TimeProgressionSpeed * Time.deltaTime; m_CurrentDay = Mathf.Repeat(m_CurrentDay, 365); m_CurrentTime = Mathf.Repeat(m_CurrentTime, 24); } /// /// Evaluates the current scene temperature by combining the seasonal and daily temperature values. /// /// The combined temperature value based on the current day and time. public float Evaluate() { return m_DayTemperature.Evaluate(m_CurrentDay / 365) + m_TimeTemperature.Evaluate(m_CurrentTime / 24); } /// /// Called when the object is disabled. Cleans up the singleton instance. /// private void OnDisable() { s_Instance = null; } /// /// Reset the static variables for domain reloading. /// [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.SubsystemRegistration)] private static void DomainReset() { s_Instance = null; } } }