82 lines
2.1 KiB
C#
Raw Normal View History

2022-05-27 19:07:05 -07:00
using Cysharp.Threading.Tasks;
2022-05-28 09:58:19 -07:00
using QSB.EchoesOfTheEye.LightSensorSync.Messages;
using QSB.Messaging;
2022-05-27 19:07:05 -07:00
using QSB.Player;
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-07-23 21:25:28 -07:00
/*
* For those who come here,
* leave while you still can.
*/
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-27 22:28:24 -07:00
internal bool _locallyIlluminated;
public Action OnDetectLocalLight;
public Action OnDetectLocalDarkness;
2022-03-02 19:46:33 -08:00
2022-05-28 09:58:19 -07:00
internal readonly List<uint> _illuminatedBy = new();
2022-05-27 19:15:04 -07:00
public override void SendInitialState(uint to)
{
2022-05-28 09:58:19 -07:00
this.SendMessage(new IlluminatedByMessage(_illuminatedBy.ToArray()) { To = to });
if (AttachedObject._illuminatingDreamLanternList != null)
{
this.SendMessage(new IlluminatingLanternsMessage(AttachedObject._illuminatingDreamLanternList) { To = to });
}
2022-05-27 19:15:04 -07:00
}
2022-05-27 19:07:05 -07:00
2022-07-24 14:32:11 +01:00
public override async UniTask Init(CancellationToken ct)
{
QSBPlayerManager.OnRemovePlayer += OnPlayerLeave;
// normally done in Start, but world objects arent ready by that point
Delay.RunWhen(() => QSBWorldSync.AllObjectsReady, () =>
2022-07-24 14:32:11 +01:00
{
if (AttachedObject._sector != null)
{
if (AttachedObject._startIlluminated)
{
_locallyIlluminated = true;
OnDetectLocalLight?.Invoke();
this.SendMessage(new SetIlluminatedMessage(true));
}
}
});
2022-07-24 14:32:11 +01:00
}
2022-05-27 19:07:05 -07:00
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
public void SetIlluminated(uint playerId, bool locallyIlluminated)
2022-05-27 19:11:04 -07:00
{
2022-05-27 19:38:07 -07:00
var illuminated = _illuminatedBy.Count > 0;
if (locallyIlluminated)
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
{
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
{
AttachedObject._illuminated = false;
AttachedObject.OnDetectDarkness.Invoke();
}
2022-05-27 19:11:04 -07:00
}
}