using System; using UnityEngine; namespace LobbyRelaySample { /// /// Runs the countdown to the in-game state. While the start of the countdown is synced via Relay, the countdown itself is handled locally, /// since precise timing isn't necessary. /// [RequireComponent(typeof(UI.CountdownUI))] public class Countdown : MonoBehaviour { CallbackValue TimeLeft = new CallbackValue(); private UI.CountdownUI m_ui; private const int k_countdownTime = 4; public void OnEnable() { if (m_ui == null) m_ui = GetComponent(); TimeLeft.onChanged += m_ui.OnTimeChanged; TimeLeft.Value = -1; } public void StartCountDown() { TimeLeft.Value = k_countdownTime; } public void CancelCountDown() { TimeLeft.Value = -1; } public void Update() { if (TimeLeft.Value < 0) return; TimeLeft.Value -= Time.deltaTime; if (TimeLeft.Value < 0) GameManager.Instance.FinishedCountDown(); } } }