2020-08-16 15:15:36 +00:00
|
|
|
|
using UnityEngine;
|
2020-08-10 17:24:28 +00:00
|
|
|
|
using QSB.Events;
|
2020-02-13 19:23:26 +00:00
|
|
|
|
|
2020-02-21 22:36:07 +00:00
|
|
|
|
namespace QSB.TransformSync
|
2020-02-15 19:48:02 +00:00
|
|
|
|
{
|
2020-02-21 22:36:07 +00:00
|
|
|
|
public class SectorSync : MonoBehaviour
|
2020-02-15 19:48:02 +00:00
|
|
|
|
{
|
2020-08-16 15:15:36 +00:00
|
|
|
|
private const float CheckInterval = 0.5f;
|
|
|
|
|
private float _checkTimer = CheckInterval;
|
2020-08-09 12:54:05 +00:00
|
|
|
|
|
2020-08-16 15:15:36 +00:00
|
|
|
|
private void Update()
|
2020-02-15 19:48:02 +00:00
|
|
|
|
{
|
2020-08-16 15:15:36 +00:00
|
|
|
|
if (!QSBSectorManager.Instance.IsReady)
|
2020-08-09 10:02:59 +00:00
|
|
|
|
{
|
2020-08-16 15:15:36 +00:00
|
|
|
|
return;
|
2020-08-09 10:02:59 +00:00
|
|
|
|
}
|
2020-08-16 15:15:36 +00:00
|
|
|
|
_checkTimer += Time.unscaledDeltaTime;
|
|
|
|
|
if (_checkTimer < CheckInterval)
|
2020-02-15 19:48:02 +00:00
|
|
|
|
{
|
2020-08-05 19:59:50 +00:00
|
|
|
|
return;
|
2020-02-13 20:48:23 +00:00
|
|
|
|
}
|
2020-08-16 15:15:36 +00:00
|
|
|
|
PlayerRegistry.LocalTransformSyncs.ForEach(CheckTransformSyncSector);
|
|
|
|
|
_checkTimer = 0;
|
2020-02-13 20:48:23 +00:00
|
|
|
|
}
|
2020-02-15 19:48:02 +00:00
|
|
|
|
|
2020-08-16 15:15:36 +00:00
|
|
|
|
private void CheckTransformSyncSector(TransformSync transformSync)
|
2020-08-05 19:59:50 +00:00
|
|
|
|
{
|
|
|
|
|
var syncedTransform = transformSync.SyncedTransform;
|
2020-08-16 15:15:36 +00:00
|
|
|
|
if (syncedTransform == null || syncedTransform.position == Vector3.zero)
|
2020-08-05 19:59:50 +00:00
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
2020-08-16 15:15:36 +00:00
|
|
|
|
var closestSector = QSBSectorManager.Instance.GetClosestSector(syncedTransform);
|
2020-08-14 19:04:29 +00:00
|
|
|
|
if (closestSector == transformSync.ReferenceSector)
|
2020-08-05 19:59:50 +00:00
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
2020-08-14 19:04:29 +00:00
|
|
|
|
transformSync.ReferenceSector = closestSector;
|
2020-08-16 15:15:36 +00:00
|
|
|
|
SendSector(transformSync.netId.Value, closestSector);
|
2020-08-05 19:59:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-08-16 15:15:36 +00:00
|
|
|
|
private void SendSector(uint id, QSBSector sector)
|
2020-08-05 19:59:50 +00:00
|
|
|
|
{
|
2020-08-16 15:15:36 +00:00
|
|
|
|
GlobalMessenger<uint, QSBSector>.FireEvent(EventNames.QSBSectorChange, id, sector);
|
2020-08-05 19:59:50 +00:00
|
|
|
|
}
|
2020-02-13 19:23:26 +00:00
|
|
|
|
}
|
|
|
|
|
}
|