QSBPlayerLightSensor

This commit is contained in:
JohnCorby 2022-08-16 17:05:13 -07:00
parent f40ffa46ff
commit b4f2463d4a
5 changed files with 19 additions and 50 deletions

View File

@ -2,8 +2,6 @@
using QSB.Messaging;
using QSB.Player;
using QSB.WorldSync;
using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
@ -15,73 +13,37 @@ using UnityEngine;
namespace QSB.EchoesOfTheEye.LightSensorSync;
/// <summary>
/// stores a bit of extra data needed for player light sensor sync
/// only purpose is to handle initial state sync
///
/// we don't have to worry about start illuminated or sectors
///
/// 2 uses:
/// - AlarmTotem.CheckPlayerVisible
/// GhostSensors.FixedUpdate_Sensors
///
/// TODO: this can probably be massively simplified to work with these uses only
///
/// we don't have to worry about start illuminated or sectors
/// - GhostSensors.FixedUpdate_Sensors
/// </summary>
[RequireComponent(typeof(SingleLightSensor))]
public class QSBPlayerLightSensor : MonoBehaviour
{
private SingleLightSensor _lightSensor;
[NonSerialized]
public uint PlayerId;
internal bool _locallyIlluminated;
internal readonly List<uint> _illuminatedBy = new();
private uint _playerId;
private void Awake()
{
_lightSensor = GetComponent<SingleLightSensor>();
PlayerId = QSBPlayerManager.PlayerList.First(x => x.LightSensor == _lightSensor).PlayerId;
_playerId = QSBPlayerManager.PlayerList.First(x => x.LightSensor == _lightSensor).PlayerId;
RequestInitialStatesMessage.SendInitialState += SendInitialState;
QSBPlayerManager.OnRemovePlayer += OnPlayerLeave;
}
private void OnDestroy()
{
private void OnDestroy() =>
RequestInitialStatesMessage.SendInitialState -= SendInitialState;
QSBPlayerManager.OnRemovePlayer -= OnPlayerLeave;
}
private void SendInitialState(uint to)
{
new PlayerIlluminatedByMessage(PlayerId, _illuminatedBy.ToArray()) { To = to }.Send();
new PlayerSetIlluminatedMessage(_playerId, _lightSensor._illuminated) { To = to }.Send();
if (_lightSensor._illuminatingDreamLanternList != null)
{
new PlayerIlluminatingLanternsMessage(PlayerId, _lightSensor._illuminatingDreamLanternList) { To = to }.Send();
}
}
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();
new PlayerIlluminatingLanternsMessage(_playerId, _lightSensor._illuminatingDreamLanternList) { To = to }.Send();
}
}
}

View File

@ -34,7 +34,10 @@ internal class QSBLightSensor : AuthWorldObject<SingleLightSensor>
base.SendInitialState(to);
this.SendMessage(new SetIlluminatedMessage(AttachedObject._illuminated) { To = to });
this.SendMessage(new IlluminatingLanternsMessage(AttachedObject._illuminatingDreamLanternList) { To = to });
if (AttachedObject._illuminatingDreamLanternList != null)
{
this.SendMessage(new IlluminatingLanternsMessage(AttachedObject._illuminatingDreamLanternList) { To = to });
}
}
public override async UniTask Init(CancellationToken ct)

View File

@ -54,6 +54,10 @@ public partial class PlayerInfo
}
private GameObject _body;
/// <summary>
/// remote light sensor is disabled.
/// it only acts as a storage of data and is always synced with the local light sensor.
/// </summary>
public LightSensor LightSensor
{
get

View File

@ -38,7 +38,7 @@ public static class LocalPlayerCreation
player.CameraBody = cameraBody.gameObject;
visibleCameraRoot = cameraBody;
player.QSBPlayerLightSensor = player.LightSensor.gameObject.GetAddComponent<QSBPlayerLightSensor>();
player.LightSensor.gameObject.GetAddComponent<QSBPlayerLightSensor>();
PlayerToolsManager.InitLocal();

View File

@ -80,7 +80,7 @@ public static class RemotePlayerCreation
player.CameraBody = REMOTE_PlayerCamera;
visibleCameraRoot = REMOTE_PlayerCamera.transform;
player.QSBPlayerLightSensor = player.LightSensor.gameObject.GetAddComponent<QSBPlayerLightSensor>();
player.LightSensor.gameObject.GetAddComponent<QSBPlayerLightSensor>();
PlayerToolsManager.InitRemote(player);