2024-04-05 13:54:38 +02:00
|
|
|
using System;
|
|
|
|
using System.Collections;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using UnityEngine;
|
|
|
|
using Utility;
|
|
|
|
|
2024-04-05 14:12:03 +02:00
|
|
|
/// <summary>
|
|
|
|
/// Macht genau das was man erwartet: Schaltet je nach Uhrzeit alle Kinder an oder aus.
|
|
|
|
/// </summary>
|
2024-04-05 13:54:38 +02:00
|
|
|
public class Zeitschaltuhr : MonoBehaviour, ISerializationCallbackReceiver
|
|
|
|
{
|
|
|
|
private TimeSpan _turnOnTimeSpan;
|
|
|
|
private TimeSpan _turnOffTimeSpan;
|
2024-04-07 00:41:10 +02:00
|
|
|
private float _activeTimeSpanInSeconds;
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Die Dauer der Active-Time der Kinder in echten Sekunden
|
|
|
|
/// </summary>
|
|
|
|
public float ActiveTimeSpanInSeconds => _activeTimeSpanInSeconds;
|
|
|
|
/// <summary>
|
|
|
|
/// Ob die Kinder gerade aktiv sind oder nicht
|
|
|
|
/// </summary>
|
2024-04-05 13:54:38 +02:00
|
|
|
public bool IsOn;
|
2024-04-07 00:41:10 +02:00
|
|
|
|
2024-04-05 13:54:38 +02:00
|
|
|
[SerializeField] private SimpleTime _turnOnTime;
|
|
|
|
[SerializeField] private SimpleTime _turnOffTime;
|
|
|
|
|
|
|
|
public SimpleTime TurnOnTime
|
|
|
|
{
|
|
|
|
get => _turnOnTime;
|
|
|
|
set
|
|
|
|
{
|
|
|
|
_turnOnTime = value;
|
|
|
|
_turnOnTimeSpan = _turnOnTime.ToTimeSpan();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public SimpleTime TurnOffTime
|
|
|
|
{
|
|
|
|
get => _turnOffTime;
|
|
|
|
set
|
|
|
|
{
|
|
|
|
_turnOffTime = value;
|
|
|
|
_turnOffTimeSpan = _turnOffTime.ToTimeSpan();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Start()
|
|
|
|
{
|
2024-04-07 00:41:10 +02:00
|
|
|
CalculateOnTimeInSeconds();
|
2024-04-05 13:54:38 +02:00
|
|
|
UpdateOn();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Update()
|
|
|
|
{
|
|
|
|
UpdateOn();
|
|
|
|
}
|
|
|
|
|
|
|
|
void UpdateOn()
|
|
|
|
{
|
|
|
|
if (_turnOnTimeSpan < _turnOffTimeSpan)
|
|
|
|
{
|
|
|
|
// beide Zeiten am selben Tag
|
|
|
|
|
|
|
|
bool wasOn = IsOn;
|
|
|
|
|
|
|
|
IsOn = TimeManager.Instance.TimeOfDay.IsBetween(_turnOnTimeSpan, _turnOffTimeSpan);
|
|
|
|
|
|
|
|
if (IsOn != wasOn)
|
|
|
|
{
|
|
|
|
UpdateChildren();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Zeiten über Mitternacht
|
|
|
|
|
|
|
|
bool wasOn = IsOn;
|
|
|
|
|
|
|
|
IsOn = !TimeManager.Instance.TimeOfDay.IsBetween(_turnOffTimeSpan, _turnOnTimeSpan);
|
|
|
|
|
|
|
|
if (IsOn != wasOn)
|
|
|
|
{
|
|
|
|
UpdateChildren();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void UpdateChildren()
|
|
|
|
{
|
|
|
|
for (int i = 0; i < transform.childCount; i++)
|
|
|
|
{
|
|
|
|
transform.GetChild(i).gameObject.SetActive(IsOn);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void OnBeforeSerialize()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
public void OnAfterDeserialize()
|
|
|
|
{
|
|
|
|
_turnOnTimeSpan = _turnOnTime.ToTimeSpan();
|
|
|
|
_turnOffTimeSpan = _turnOffTime.ToTimeSpan();
|
|
|
|
}
|
2024-04-07 00:41:10 +02:00
|
|
|
|
|
|
|
private void CalculateOnTimeInSeconds()
|
|
|
|
{
|
|
|
|
TimeSpan turnOnTimeSpan = _turnOnTime.ToTimeSpan();
|
|
|
|
TimeSpan turnOffTimeSpan = _turnOffTime.ToTimeSpan();
|
|
|
|
|
|
|
|
float onDurationInSeconds;
|
|
|
|
if (turnOnTimeSpan < turnOffTimeSpan)
|
|
|
|
{
|
|
|
|
onDurationInSeconds = (float)(turnOffTimeSpan - turnOnTimeSpan).TotalSeconds;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
TimeSpan fullDay = TimeSpan.FromHours(24);
|
|
|
|
onDurationInSeconds = (float)((fullDay - turnOnTimeSpan) + turnOffTimeSpan).TotalSeconds;
|
|
|
|
}
|
|
|
|
|
|
|
|
_activeTimeSpanInSeconds = onDurationInSeconds * ((float)TimeManager.Instance.SecondsPerDay / 86400f); // 86400 Sekunden pro Tag
|
|
|
|
}
|
2024-04-05 13:54:38 +02:00
|
|
|
}
|