mirror of
https://github.com/GTA-ASM/SanAndreasUnity
synced 2024-11-27 06:20:17 +00:00
49 lines
1.4 KiB
C#
49 lines
1.4 KiB
C#
|
using System.Globalization;
|
|||
|
using SanAndreasUnity.Behaviours.World;
|
|||
|
using SanAndreasUnity.Utilities;
|
|||
|
using UnityEngine;
|
|||
|
|
|||
|
namespace SanAndreasUnity.Net
|
|||
|
{
|
|||
|
public class DayTimeSyncer : MonoBehaviour
|
|||
|
{
|
|||
|
public const string kDataKey = "day-time";
|
|||
|
|
|||
|
private void Awake()
|
|||
|
{
|
|||
|
SyncedServerData.onInitialSyncDataAvailable += OnInitialSyncDataAvailable;
|
|||
|
}
|
|||
|
|
|||
|
void Start()
|
|||
|
{
|
|||
|
DayTimeManager.Singleton.onTimeChanged += OnTimeChanged;
|
|||
|
|
|||
|
if (!NetUtils.IsServer)
|
|||
|
{
|
|||
|
SyncedServerData.Data.RegisterCallback(kDataKey, OnDayTimeChangedFromServer);
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
private void OnInitialSyncDataAvailable()
|
|||
|
{
|
|||
|
string dayTime = SyncedServerData.Data.GetString(kDataKey);
|
|||
|
this.OnDayTimeChangedFromServer(dayTime);
|
|||
|
}
|
|||
|
|
|||
|
private void OnDayTimeChangedFromServer(string dayTime)
|
|||
|
{
|
|||
|
float curveTime = float.Parse(dayTime, CultureInfo.InvariantCulture);
|
|||
|
DayTimeManager.CurveTimeToHoursAndMinutes(curveTime, out byte hours, out byte minutes);
|
|||
|
DayTimeManager.Singleton.SetTime(hours, minutes, false);
|
|||
|
}
|
|||
|
|
|||
|
private void OnTimeChanged()
|
|||
|
{
|
|||
|
if (!NetUtils.IsServer)
|
|||
|
return;
|
|||
|
|
|||
|
SyncedServerData.Data.SetFloat(kDataKey, DayTimeManager.Singleton.CurrentCurveTime);
|
|||
|
}
|
|||
|
}
|
|||
|
}
|