using System; using UnityEngine.Events; using System.Collections.Generic; namespace UnityEngine.AzureSky { [Serializable] public sealed class AzureEventSystem { /// The event triggered when the minute change in the Time System component. public UnityEvent onMinuteChangeEvent => m_onMinuteChangeEvent; [SerializeField] private UnityEvent m_onMinuteChangeEvent; /// The event triggered when the hour change in the Time System component. public UnityEvent onHourChangeEvent => m_onHourChangeEvent; [SerializeField] private UnityEvent m_onHourChangeEvent; /// The event triggered when the day change in the Time System component. public UnityEvent onDayChangeEvent => m_onDayChangeEvent; [SerializeField] private UnityEvent m_onDayChangeEvent; /// The event triggered when the month change in the Time System component. public UnityEvent onMonthChangeEvent => m_onMonthChangeEvent; [SerializeField] private UnityEvent m_onMonthChangeEvent; /// The event triggered when the year change in the Time System component. public UnityEvent onYearChangeEvent => m_onYearChangeEvent; [SerializeField] private UnityEvent m_onYearChangeEvent; /// The interval time the custom event should be scanned. public AzureCustomEventUpdateMode customEventScanMode { get => m_customEventScanMode; set => m_customEventScanMode = value; } [SerializeField] private AzureCustomEventUpdateMode m_customEventScanMode = AzureCustomEventUpdateMode.ByMinute; /// The custom event list. public List customEventList => m_customEventList; [SerializeField] private List m_customEventList = new List(); /// Register the events when the GameObject is enabled. public void RegisterEvents() { AzureNotificationCenter.OnMinuteChanged += OnMinuteChange; AzureNotificationCenter.OnHourChanged += OnHourChange; AzureNotificationCenter.OnDayChanged += OnDayChange; AzureNotificationCenter.OnMonthChanged += OnMonthChange; AzureNotificationCenter.OnYearChanged += OnYearChange; } /// Register the events when the GameObject is disable. public void UnregisterEvents() { AzureNotificationCenter.OnMinuteChanged -= OnMinuteChange; AzureNotificationCenter.OnHourChanged -= OnHourChange; AzureNotificationCenter.OnDayChanged -= OnDayChange; AzureNotificationCenter.OnMonthChanged -= OnMonthChange; AzureNotificationCenter.OnYearChanged -= OnYearChange; } /// Triggers the UnityEvent linked to the OnMinuteChange event from the TimeSystem component. private void OnMinuteChange(AzureTimeSystem timeSystem) { m_onMinuteChangeEvent?.Invoke(); if (m_customEventScanMode == AzureCustomEventUpdateMode.ByMinute) { ScanCustomEventList(timeSystem); } } /// Triggers the UnityEvent linked to the OnHourChange event from the TimeSystem component. private void OnHourChange(AzureTimeSystem timeSystem) { m_onHourChangeEvent?.Invoke(); if (m_customEventScanMode == AzureCustomEventUpdateMode.ByHour) { ScanCustomEventList(timeSystem); } } /// Triggers the UnityEvent linked to the OnDayChange event from the TimeSystem component. private void OnDayChange(AzureTimeSystem timeSystem) { m_onDayChangeEvent?.Invoke(); } /// Triggers the UnityEvent linked to the OnMonthChange event from the TimeSystem component. private void OnMonthChange(AzureTimeSystem timeSystem) { m_onMonthChangeEvent?.Invoke(); } /// Triggers the UnityEvent linked to the OnYearChange event from the TimeSystem component. private void OnYearChange(AzureTimeSystem timeSystem) { m_onYearChangeEvent?.Invoke(); } /// Scans the custom event list and perform the event that match with the current date and time from the TymeSystem component. private void ScanCustomEventList(AzureTimeSystem timeSystem) { for (int i = 0; i < m_customEventList.Count; i++) { if (m_customEventList[i].eventListenersCount <= 0) continue; if (m_customEventList[i].year != timeSystem.year && m_customEventList[i].year != -1) continue; if (m_customEventList[i].month != timeSystem.month && m_customEventList[i].month != -1) continue; if (m_customEventList[i].day != timeSystem.day && m_customEventList[i].day != -1) continue; if (timeSystem.hour != m_customEventList[i].executedHour) m_customEventList[i].isAlreadyExecutedOnThisHour = false; if (m_customEventList[i].hour != timeSystem.hour && m_customEventList[i].hour != -1) continue; if (m_customEventList[i].minute == -1) { m_customEventList[i].Invoke(); } else { if (!m_customEventList[i].isAlreadyExecutedOnThisHour) { if (timeSystem.minute >= m_customEventList[i].minute) { m_customEventList[i].executedHour = timeSystem.hour; m_customEventList[i].isAlreadyExecutedOnThisHour = true; m_customEventList[i].Invoke(); } } } } } } }