mirror of
https://github.com/GTA-ASM/SanAndreasUnity
synced 2025-02-16 21:08:28 +00:00
attempt to sync
This commit is contained in:
parent
d5f3657151
commit
613767d6d7
5 changed files with 96 additions and 1 deletions
|
@ -213,6 +213,7 @@ GameObject:
|
|||
m_Component:
|
||||
- component: {fileID: 499186}
|
||||
- component: {fileID: 114077829182571006}
|
||||
- component: {fileID: 3252431705490264232}
|
||||
m_Layer: 0
|
||||
m_Name: World
|
||||
m_TagString: Untagged
|
||||
|
@ -413,6 +414,18 @@ MonoBehaviour:
|
|||
controlLightIntensity: 1
|
||||
disableLightDuringNight: 0
|
||||
moonColor: {r: 0.41006586, g: 0.71558595, b: 0.9150943, a: 1}
|
||||
--- !u!114 &3252431705490264232
|
||||
MonoBehaviour:
|
||||
m_ObjectHideFlags: 0
|
||||
m_CorrespondingSourceObject: {fileID: 0}
|
||||
m_PrefabInstance: {fileID: 0}
|
||||
m_PrefabAsset: {fileID: 0}
|
||||
m_GameObject: {fileID: 143544}
|
||||
m_Enabled: 1
|
||||
m_EditorHideFlags: 0
|
||||
m_Script: {fileID: 11500000, guid: 3defb462a2823b347b81b01a5a3e5dd3, type: 3}
|
||||
m_Name:
|
||||
m_EditorClassIdentifier:
|
||||
--- !u!1 &149528
|
||||
GameObject:
|
||||
m_ObjectHideFlags: 0
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
using UnityEngine;
|
||||
using SanAndreasUnity.Utilities;
|
||||
using UnityEngine;
|
||||
|
||||
namespace SanAndreasUnity.Behaviours.World
|
||||
{
|
||||
|
@ -41,6 +42,8 @@ namespace SanAndreasUnity.Behaviours.World
|
|||
private static int s_nightMultiplierPropertyId = -1;
|
||||
public static int NightMultiplierPropertyId => s_nightMultiplierPropertyId == -1 ? s_nightMultiplierPropertyId = Shader.PropertyToID("_NightMultiplier") : s_nightMultiplierPropertyId;
|
||||
|
||||
public event System.Action onTimeChanged = delegate {};
|
||||
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
|
@ -128,6 +131,8 @@ namespace SanAndreasUnity.Behaviours.World
|
|||
{
|
||||
Debug.Log($"Time set to {hours}:{minutes}, curveTime {curveTime}, lightIntensity {lightIntensity}, lightAngle {lightAngle}, nightMultiplier {nightMultiplier}");
|
||||
}
|
||||
|
||||
F.InvokeEventExceptionSafe(this.onTimeChanged);
|
||||
}
|
||||
|
||||
float UpdateLightAngle(float curveTime)
|
||||
|
@ -136,5 +141,12 @@ namespace SanAndreasUnity.Behaviours.World
|
|||
this.directionalLight.transform.rotation = Quaternion.AngleAxis(lightAngle, Vector3.right);
|
||||
return lightAngle;
|
||||
}
|
||||
|
||||
public static void CurveTimeToHoursAndMinutes(float curveTime, out byte hours, out byte minutes)
|
||||
{
|
||||
hours = (byte) Mathf.FloorToInt(curveTime);
|
||||
float hourPerc = curveTime - Mathf.Floor(curveTime);
|
||||
minutes = (byte) Mathf.FloorToInt(60 * hourPerc);
|
||||
}
|
||||
}
|
||||
}
|
48
Assets/Scripts/Networking/DayTimeSyncer.cs
Normal file
48
Assets/Scripts/Networking/DayTimeSyncer.cs
Normal file
|
@ -0,0 +1,48 @@
|
|||
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);
|
||||
}
|
||||
}
|
||||
}
|
11
Assets/Scripts/Networking/DayTimeSyncer.cs.meta
Normal file
11
Assets/Scripts/Networking/DayTimeSyncer.cs.meta
Normal file
|
@ -0,0 +1,11 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 3defb462a2823b347b81b01a5a3e5dd3
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
|
@ -1,4 +1,5 @@
|
|||
using Mirror;
|
||||
using SanAndreasUnity.Utilities;
|
||||
using UnityEngine;
|
||||
|
||||
namespace SanAndreasUnity.Net
|
||||
|
@ -11,6 +12,8 @@ namespace SanAndreasUnity.Net
|
|||
|
||||
public static SyncedBag Data { get; private set; } = new SyncedBag(new SyncedBag.StringSyncDictionary());
|
||||
|
||||
public static event System.Action onInitialSyncDataAvailable = delegate {};
|
||||
|
||||
|
||||
|
||||
private void Awake()
|
||||
|
@ -48,5 +51,13 @@ namespace SanAndreasUnity.Net
|
|||
// clear data for next server start
|
||||
Data = new SyncedBag(new SyncedBag.StringSyncDictionary());
|
||||
}
|
||||
|
||||
public override void OnStartClient()
|
||||
{
|
||||
if (NetStatus.IsServer)
|
||||
return;
|
||||
|
||||
F.InvokeEventExceptionSafe(onInitialSyncDataAvailable);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue