diff --git a/Assets/Scripts/Behaviours/World/DayTimeManager.cs b/Assets/Scripts/Behaviours/World/DayTimeManager.cs index 7cb788b4..1da6f5f3 100644 --- a/Assets/Scripts/Behaviours/World/DayTimeManager.cs +++ b/Assets/Scripts/Behaviours/World/DayTimeManager.cs @@ -43,6 +43,7 @@ namespace SanAndreasUnity.Behaviours.World public static int NightMultiplierPropertyId => s_nightMultiplierPropertyId == -1 ? s_nightMultiplierPropertyId = Shader.PropertyToID("_NightMultiplier") : s_nightMultiplierPropertyId; public event System.Action onTimeChanged = delegate {}; + public event System.Action onHourChanged = delegate {}; private void Awake() @@ -103,6 +104,8 @@ namespace SanAndreasUnity.Behaviours.World hours = (byte) Mathf.Clamp(hours, 0, 23); minutes = (byte) Mathf.Clamp(minutes, 0, 59); + byte oldHour = this.CurrentTimeHours; + this.CurrentTimeHours = hours; this.CurrentTimeMinutes = minutes; @@ -136,6 +139,8 @@ namespace SanAndreasUnity.Behaviours.World } F.InvokeEventExceptionSafe(this.onTimeChanged); + if (oldHour != this.CurrentTimeHours) + F.InvokeEventExceptionSafe(this.onHourChanged); } float UpdateLightAngle(float curveTime) diff --git a/Assets/Scripts/Behaviours/World/StaticGeometry.cs b/Assets/Scripts/Behaviours/World/StaticGeometry.cs index 4ea215cd..b4352ee8 100644 --- a/Assets/Scripts/Behaviours/World/StaticGeometry.cs +++ b/Assets/Scripts/Behaviours/World/StaticGeometry.cs @@ -15,9 +15,18 @@ namespace SanAndreasUnity.Behaviours.World { public static StaticGeometry Create() { + if (!s_registeredTimeChangeCallback) + { + s_registeredTimeChangeCallback = true; + DayTimeManager.Singleton.onHourChanged += OnHourChanged; + } + return new GameObject().AddComponent(); } + private static List s_timedObjects = new List(); + public static IReadOnlyList TimedObjects => s_timedObjects; + protected Instance Instance { get; private set; } public ISimpleObjectDefinition ObjectDefinition { get; private set; } @@ -49,11 +58,21 @@ namespace SanAndreasUnity.Behaviours.World public StaticGeometry LodParent { get; private set; } public StaticGeometry LodChild { get; private set; } + private static bool s_registeredTimeChangeCallback = false; + + public bool IsVisibleBasedOnCurrentDayTime => this.ObjectDefinition is TimeObjectDef timeObjectDef ? IsObjectVisibleBasedOnCurrentDayTime(timeObjectDef) : true; + + public void Initialize(Instance inst, Dictionary dict) { Instance = inst; ObjectDefinition = Item.GetDefinition(inst.ObjectId); + if (ObjectDefinition is TimeObjectDef) + { + s_timedObjects.Add(this); + } + Initialize(inst.Position, inst.Rotation); _canLoad = ObjectDefinition != null; @@ -259,7 +278,7 @@ namespace SanAndreasUnity.Behaviours.World mr.SetPropertyBlock(null); - if (!IsVisible) + if (!IsVisible || !IsVisibleBasedOnCurrentDayTime) { gameObject.SetActive(false); } @@ -271,5 +290,29 @@ namespace SanAndreasUnity.Behaviours.World { IsVisible = false; } + + private static void OnHourChanged() + { + foreach (var timedObject in s_timedObjects) + { + if (timedObject.IsVisible) + { + timedObject.gameObject.SetActive(timedObject.IsVisibleBasedOnCurrentDayTime); + } + } + } + + private static bool IsObjectVisibleBasedOnCurrentDayTime(TimeObjectDef timeObjectDef) + { + byte currentHour = DayTimeManager.Singleton.CurrentTimeHours; + if (timeObjectDef.TimeOnHours < timeObjectDef.TimeOffHours) + { + return currentHour >= timeObjectDef.TimeOnHours && currentHour < timeObjectDef.TimeOffHours; + } + else + { + return currentHour >= timeObjectDef.TimeOnHours || currentHour < timeObjectDef.TimeOffHours; + } + } } } \ No newline at end of file