89 lines
2.5 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;
2022-08-15 16:39:57 -07:00
/// <summary>
2022-08-15 16:42:04 -07:00
/// TODO: switch this over to some sort of auth system.
2022-08-15 16:39:57 -07:00
/// list of illuminators doesn't work because if a player illuminates and then leaves,
2022-08-15 16:41:24 -07:00
/// it'll be considered illuminated forever until they come back.
///
2022-08-15 22:48:58 -07:00
/// BUG: this breaks in zone2.
2022-08-15 16:41:24 -07:00
/// the sector it's enabled in is bigger than the sector the zone2 walls are enabled in :(
/// maybe this can be fixed by making the collision group use the same sector.
2022-08-15 16:39:57 -07:00
/// </summary>
2022-03-02 19:46:33 -08:00
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;
2022-08-15 22:48:58 -07:00
// do this stuff here instead of Start, since world objects won't be 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();
}
}
});
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
}
}