64 lines
1.5 KiB
C#
Raw Normal View History

2022-05-28 13:49:18 -07:00
using QSB.Player;
using QSB.WorldSync;
2022-05-28 13:42:19 -07:00
using System.Collections.Generic;
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
/// </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: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: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)
{
// todo send the messages
}
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
}