(de)activate time objects based on current time

This commit is contained in:
in0finite 2021-05-04 19:54:47 +02:00
parent 49596eb613
commit 197089aab3
2 changed files with 49 additions and 1 deletions

View file

@ -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)

View file

@ -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<StaticGeometry>();
}
private static List<StaticGeometry> s_timedObjects = new List<StaticGeometry>();
public static IReadOnlyList<StaticGeometry> 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<Instance, StaticGeometry> dict)
{
Instance = inst;
ObjectDefinition = Item.GetDefinition<Importing.Items.Definitions.ISimpleObjectDefinition>(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;
}
}
}
}