88 lines
2.2 KiB
C#
Raw Normal View History

2022-05-28 13:58:32 -07:00
using QSB.EchoesOfTheEye.LightSensorSync.Messages;
using QSB.Messaging;
using QSB.Player;
2022-05-28 13:49:18 -07:00
using QSB.WorldSync;
2022-05-28 14:53:01 -07:00
using System;
2022-05-28 13:42:19 -07:00
using System.Collections.Generic;
2022-05-28 13:50:58 -07:00
using System.Linq;
2022-05-28 13:42:19 -07:00
using UnityEngine;
2022-07-23 21:25:28 -07:00
/*
* For those who come here,
* leave while you still can.
*/
2022-05-28 13:42:19 -07:00
namespace QSB.EchoesOfTheEye.LightSensorSync;
2022-05-28 13:49:18 -07:00
/// <summary>
/// stores a bit of extra data needed for player light sensor sync
2022-08-15 16:39:57 -07:00
///
/// 2 uses:
/// - AlarmTotem.CheckPlayerVisible
/// GhostSensors.FixedUpdate_Sensors
///
2022-08-15 16:42:04 -07:00
/// TODO: this can probably be massively simplified to work with these uses only
2022-08-15 21:09:59 -07:00
///
/// we don't have to worry about start illuminated or sectors
2022-05-28 13:49:18 -07:00
/// </summary>
[RequireComponent(typeof(SingleLightSensor))]
2022-05-28 13:42:19 -07:00
public class QSBPlayerLightSensor : MonoBehaviour
{
2022-05-28 13:49:18 -07:00
private SingleLightSensor _lightSensor;
2022-05-28 14:53:01 -07:00
[NonSerialized]
public uint PlayerId;
2022-05-28 13:42:19 -07:00
internal bool _locallyIlluminated;
2022-05-28 13:49:18 -07:00
internal readonly List<uint> _illuminatedBy = new();
2022-05-28 13:42:19 -07:00
2022-05-28 13:49:18 -07:00
private void Awake()
{
_lightSensor = GetComponent<SingleLightSensor>();
2022-05-28 14:14:25 -07:00
PlayerId = QSBPlayerManager.PlayerList.First(x => x.LightSensor == _lightSensor).PlayerId;
2022-05-28 13:42:19 -07:00
2022-05-28 13:49:18 -07:00
RequestInitialStatesMessage.SendInitialState += SendInitialState;
QSBPlayerManager.OnRemovePlayer += OnPlayerLeave;
}
private void OnDestroy()
{
RequestInitialStatesMessage.SendInitialState -= SendInitialState;
QSBPlayerManager.OnRemovePlayer -= OnPlayerLeave;
}
private void SendInitialState(uint to)
{
2022-05-28 14:14:25 -07:00
new PlayerIlluminatedByMessage(PlayerId, _illuminatedBy.ToArray()) { To = to }.Send();
2022-05-28 13:58:32 -07:00
if (_lightSensor._illuminatingDreamLanternList != null)
{
2022-05-28 14:14:25 -07:00
new PlayerIlluminatingLanternsMessage(PlayerId, _lightSensor._illuminatingDreamLanternList) { To = to }.Send();
2022-05-28 13:58:32 -07:00
}
2022-05-28 13:49:18 -07:00
}
private void OnPlayerLeave(PlayerInfo player) => SetIlluminated(player.PlayerId, false);
public void SetIlluminated(uint playerId, bool locallyIlluminated)
{
var illuminated = _illuminatedBy.Count > 0;
if (locallyIlluminated)
{
_illuminatedBy.SafeAdd(playerId);
}
else
{
_illuminatedBy.QuickRemove(playerId);
}
if (!illuminated && _illuminatedBy.Count > 0)
{
_lightSensor._illuminated = true;
_lightSensor.OnDetectLight.Invoke();
}
else if (illuminated && _illuminatedBy.Count == 0)
{
_lightSensor._illuminated = false;
_lightSensor.OnDetectDarkness.Invoke();
}
}
2022-05-28 13:42:19 -07:00
}