2022-03-14 08:32:35 -07:00
|
|
|
|
using Cysharp.Threading.Tasks;
|
|
|
|
|
using Mirror;
|
|
|
|
|
using QSB.EchoesOfTheEye.LightSensorSync.WorldObjects;
|
2023-05-08 11:30:59 -07:00
|
|
|
|
using QSB.OwnershipSync;
|
2022-03-14 08:32:35 -07:00
|
|
|
|
using QSB.Utility.LinkedWorldObject;
|
|
|
|
|
using QSB.WorldSync;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using UnityEngine;
|
|
|
|
|
|
|
|
|
|
namespace QSB.EchoesOfTheEye;
|
|
|
|
|
|
2023-07-28 19:47:54 +01:00
|
|
|
|
public abstract class QSBRotatingElements<T, U> : LinkedWorldObject<T, U>
|
2022-03-14 08:32:35 -07:00
|
|
|
|
where T : MonoBehaviour
|
|
|
|
|
where U : NetworkBehaviour
|
|
|
|
|
{
|
|
|
|
|
protected abstract IEnumerable<SingleLightSensor> LightSensors { get; }
|
|
|
|
|
private QSBLightSensor[] _qsbLightSensors;
|
2022-03-14 08:50:01 -07:00
|
|
|
|
private int _litSensors;
|
2022-03-14 08:32:35 -07:00
|
|
|
|
|
|
|
|
|
public override async UniTask Init(CancellationToken ct)
|
|
|
|
|
{
|
|
|
|
|
await base.Init(ct);
|
|
|
|
|
|
|
|
|
|
await UniTask.WaitUntil(() => QSBWorldSync.AllObjectsAdded, cancellationToken: ct);
|
|
|
|
|
_qsbLightSensors = LightSensors.Select(x => x.GetWorldObject<QSBLightSensor>()).ToArray();
|
|
|
|
|
|
|
|
|
|
foreach (var lightSensor in _qsbLightSensors)
|
|
|
|
|
{
|
|
|
|
|
lightSensor.OnDetectLocalLight += OnDetectLocalLight;
|
|
|
|
|
lightSensor.OnDetectLocalDarkness += OnDetectLocalDarkness;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override void OnRemoval()
|
|
|
|
|
{
|
|
|
|
|
base.OnRemoval();
|
|
|
|
|
|
2022-11-02 00:00:30 -04:00
|
|
|
|
if (_qsbLightSensors != null)
|
2022-03-14 08:32:35 -07:00
|
|
|
|
{
|
2022-11-02 00:00:30 -04:00
|
|
|
|
foreach (var lightSensor in _qsbLightSensors)
|
|
|
|
|
{
|
|
|
|
|
lightSensor.OnDetectLocalLight -= OnDetectLocalLight;
|
|
|
|
|
lightSensor.OnDetectLocalDarkness -= OnDetectLocalDarkness;
|
|
|
|
|
}
|
2022-03-14 08:32:35 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-14 08:50:01 -07:00
|
|
|
|
private void OnDetectLocalLight()
|
|
|
|
|
{
|
|
|
|
|
_litSensors++;
|
|
|
|
|
if (_litSensors == 1)
|
|
|
|
|
{
|
2023-05-08 11:41:43 -07:00
|
|
|
|
NetworkBehaviour.netIdentity.UpdateOwnerQueue(OwnerQueueAction.Add);
|
2022-03-14 08:50:01 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void OnDetectLocalDarkness()
|
|
|
|
|
{
|
|
|
|
|
_litSensors--;
|
|
|
|
|
if (_litSensors == 0)
|
|
|
|
|
{
|
2023-05-08 11:41:43 -07:00
|
|
|
|
NetworkBehaviour.netIdentity.UpdateOwnerQueue(OwnerQueueAction.Remove);
|
2022-03-14 08:50:01 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
2022-03-14 08:33:45 -07:00
|
|
|
|
|
2023-05-08 11:38:24 -07:00
|
|
|
|
protected override bool SpawnWithServerOwnership => false;
|
2022-03-14 08:32:35 -07:00
|
|
|
|
}
|