78 lines
2.0 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 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;
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-05-28 13:59:06 -07:00
///
///
///
///
/// todo you might be able to remove when you simplify light sensor after the fake sector thingy
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 13:50:58 -07:00
private PlayerInfo _player;
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 13:50:58 -07:00
_player = QSBPlayerManager.PlayerList.First(x => x.LightSensor == _lightSensor);
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 13:58:32 -07:00
new PlayerIlluminatedByMessage(_player.PlayerId, _illuminatedBy.ToArray()) { To = to }.Send();
if (_lightSensor._illuminatingDreamLanternList != null)
{
new PlayerIlluminatingLanternsMessage(_player.PlayerId, _lightSensor._illuminatingDreamLanternList) { To = to }.Send();
}
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
}