2022-05-27 19:07:05 -07:00
|
|
|
|
using Cysharp.Threading.Tasks;
|
|
|
|
|
using QSB.Player;
|
2022-05-27 19:20:02 -07:00
|
|
|
|
using QSB.Utility;
|
2022-05-27 19:07:05 -07:00
|
|
|
|
using QSB.WorldSync;
|
2022-02-26 00:26:31 -08:00
|
|
|
|
using System;
|
2022-05-27 19:07:05 -07:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Threading;
|
2022-02-26 00:26:31 -08:00
|
|
|
|
|
2022-03-02 19:46:33 -08:00
|
|
|
|
namespace QSB.EchoesOfTheEye.LightSensorSync.WorldObjects;
|
|
|
|
|
|
|
|
|
|
internal class QSBLightSensor : WorldObject<SingleLightSensor>
|
2022-02-26 00:26:31 -08:00
|
|
|
|
{
|
2022-05-26 21:23:05 -07:00
|
|
|
|
public bool LocallyIlluminated;
|
2022-03-06 23:13:48 -08:00
|
|
|
|
public Action OnDetectLocalLight;
|
|
|
|
|
public Action OnDetectLocalDarkness;
|
2022-03-02 19:46:33 -08:00
|
|
|
|
|
2022-05-27 19:38:07 -07:00
|
|
|
|
internal bool _clientIlluminated;
|
2022-05-27 19:11:04 -07:00
|
|
|
|
private readonly List<uint> _illuminatedBy = new();
|
2022-05-27 15:03:30 -07:00
|
|
|
|
|
2022-05-27 19:15:04 -07:00
|
|
|
|
public override void SendInitialState(uint to)
|
|
|
|
|
{
|
|
|
|
|
// todo
|
|
|
|
|
}
|
2022-05-27 19:07:05 -07:00
|
|
|
|
|
|
|
|
|
public override async UniTask Init(CancellationToken ct) => QSBPlayerManager.OnRemovePlayer += OnPlayerLeave;
|
|
|
|
|
public override void OnRemoval() => QSBPlayerManager.OnRemovePlayer -= OnPlayerLeave;
|
2022-05-27 19:15:04 -07:00
|
|
|
|
private void OnPlayerLeave(PlayerInfo player) => SetIlluminated(player.PlayerId, false);
|
2022-05-27 19:11:04 -07:00
|
|
|
|
|
2022-05-27 19:38:07 -07:00
|
|
|
|
public void SetIlluminated(uint playerId, bool clientIlluminated)
|
2022-05-27 19:11:04 -07:00
|
|
|
|
{
|
2022-05-27 19:38:07 -07:00
|
|
|
|
var illuminated = _illuminatedBy.Count > 0;
|
|
|
|
|
if (clientIlluminated)
|
2022-05-27 19:15:04 -07:00
|
|
|
|
{
|
2022-05-27 19:38:07 -07:00
|
|
|
|
_illuminatedBy.SafeAdd(playerId);
|
2022-05-27 19:15:04 -07:00
|
|
|
|
}
|
2022-05-27 19:38:07 -07:00
|
|
|
|
else
|
2022-05-27 19:15:04 -07:00
|
|
|
|
{
|
2022-05-27 19:38:07 -07:00
|
|
|
|
_illuminatedBy.QuickRemove(playerId);
|
2022-05-27 19:15:04 -07:00
|
|
|
|
}
|
|
|
|
|
|
2022-05-27 19:38:07 -07:00
|
|
|
|
if (!illuminated && _illuminatedBy.Count > 0)
|
2022-05-27 19:15:04 -07:00
|
|
|
|
{
|
2022-05-27 19:33:31 -07:00
|
|
|
|
DebugLog.DebugWrite($"{this} _illuminated = true by {playerId}");
|
2022-05-27 19:15:04 -07:00
|
|
|
|
AttachedObject._illuminated = true;
|
|
|
|
|
AttachedObject.OnDetectLight.Invoke();
|
|
|
|
|
}
|
2022-05-27 19:38:07 -07:00
|
|
|
|
else if (illuminated && _illuminatedBy.Count == 0)
|
2022-05-27 19:15:04 -07:00
|
|
|
|
{
|
2022-05-27 19:33:31 -07:00
|
|
|
|
DebugLog.DebugWrite($"{this} _illuminated = false by {playerId}");
|
2022-05-27 19:15:04 -07:00
|
|
|
|
AttachedObject._illuminated = false;
|
|
|
|
|
AttachedObject.OnDetectDarkness.Invoke();
|
|
|
|
|
}
|
2022-05-27 19:11:04 -07:00
|
|
|
|
}
|
2022-03-06 23:13:48 -08:00
|
|
|
|
}
|