SanAndreasUnity/Assets/Scripts/Networking/DayTimeSyncer.cs

49 lines
1.4 KiB
C#
Raw Normal View History

2021-05-03 00:59:57 +00:00
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);
}
}
}