From 5323cccaaf71a221e0eb3c5ddd669b1387922c22 Mon Sep 17 00:00:00 2001 From: NTG_Lenovo Date: Mon, 21 Jul 2025 16:55:51 +0900 Subject: [PATCH] =?UTF-8?q?TimeManager=20=EC=B6=94=EA=B0=80=20(Ui=EC=9D=B4?= =?UTF-8?q?=EB=B2=A4=ED=8A=B8=20=EB=93=B1=20=EC=97=B0=EA=B2=B0)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../_Scripts/GameFramework/TimeManager.cs | 85 +++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100644 Assets/_DDD/_Scripts/GameFramework/TimeManager.cs diff --git a/Assets/_DDD/_Scripts/GameFramework/TimeManager.cs b/Assets/_DDD/_Scripts/GameFramework/TimeManager.cs new file mode 100644 index 000000000..536e4bcb0 --- /dev/null +++ b/Assets/_DDD/_Scripts/GameFramework/TimeManager.cs @@ -0,0 +1,85 @@ +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using UnityEngine; + +namespace DDD +{ + public class TimeManager : Singleton, IManager, IEventHandler + { + private readonly Dictionary _timeScaleRequests = new(); + public float CurrentTimeScale { get; private set; } = 1f; + + private float _baseFixedDeltaTime; + + public void PreInit() + { + _baseFixedDeltaTime = Time.fixedDeltaTime; + EventBus.Register(this); + } + + public Task Init() + { + return Task.CompletedTask; + } + + public void PostInit() + { + + } + + private void OnDestroy() + { + EventBus.Unregister(this); +#if UNITY_EDITOR + Time.fixedDeltaTime = _baseFixedDeltaTime; // 재실행 시 값 복원 (Editor용 안전장치) +#endif + } + + protected override void OnApplicationQuit() + { + base.OnApplicationQuit(); + + Time.fixedDeltaTime = _baseFixedDeltaTime; + } + + private void UpdateTimeScale() + { + // 우선순위: 0이 하나라도 있으면 무조건 정지, 그 외엔 최소값 적용 + float newTimeScale = 1f; + + if (_timeScaleRequests.ContainsValue(0f)) + { + newTimeScale = 0f; + } + else if (_timeScaleRequests.Count > 0) + { + newTimeScale = Mathf.Min(1f, Mathf.Min(float.MaxValue, GetMinTimeScale())); + } + + if (Mathf.Approximately(newTimeScale, CurrentTimeScale)) return; + + CurrentTimeScale = newTimeScale; + Time.timeScale = CurrentTimeScale; + Time.fixedDeltaTime = _baseFixedDeltaTime * CurrentTimeScale; + } + + private float GetMinTimeScale() => _timeScaleRequests.Values.Prepend(1f).Min(); + + public bool IsPaused => Mathf.Approximately(CurrentTimeScale, 0f); + + public void Invoke(TimeScaleChangeEvent evt) + { + if (evt.NewTimeScale < 1f) + { + _timeScaleRequests[evt.Requester] = evt.NewTimeScale; + } + else + { + _timeScaleRequests.Remove(evt.Requester); + } + + UpdateTimeScale(); + } + } +} \ No newline at end of file