player light sensor messages

This commit is contained in:
JohnCorby 2022-08-16 16:39:47 -07:00
parent 17c7669e75
commit a6e1ed359a
7 changed files with 28 additions and 143 deletions

View File

@ -1,28 +0,0 @@
using QSB.Messaging;
using QSB.Player;
using System.Linq;
namespace QSB.EchoesOfTheEye.LightSensorSync.Messages;
/// <summary>
/// always sent by host
/// </summary>
internal class PlayerIlluminatedByMessage : QSBMessage<(uint playerId, uint[] illuminatedBy)>
{
public PlayerIlluminatedByMessage(uint playerId, uint[] illuminatedBy) : base((playerId, illuminatedBy)) { }
public override void OnReceiveRemote()
{
var qsbPlayerLightSensor = QSBPlayerManager.GetPlayer(Data.playerId).QSBPlayerLightSensor;
foreach (var added in Data.illuminatedBy.Except(qsbPlayerLightSensor._illuminatedBy).ToList())
{
qsbPlayerLightSensor.SetIlluminated(added, true);
}
foreach (var removed in qsbPlayerLightSensor._illuminatedBy.Except(Data.illuminatedBy).ToList())
{
qsbPlayerLightSensor.SetIlluminated(removed, false);
}
}
}

View File

@ -19,12 +19,6 @@ internal class PlayerIlluminatingLanternsMessage : QSBMessage<(uint playerId, in
{
var lightSensor = (SingleLightSensor)QSBPlayerManager.GetPlayer(Data.playerId).LightSensor;
if (lightSensor.enabled)
{
// sensor is enabled, so this will already be synced
return;
}
lightSensor._illuminatingDreamLanternList.Clear();
lightSensor._illuminatingDreamLanternList.AddRange(
Data.lanterns.Select(x => x.GetWorldObject<QSBDreamLantern>().AttachedObject));

View File

@ -6,8 +6,24 @@ namespace QSB.EchoesOfTheEye.LightSensorSync.Messages;
internal class PlayerSetIlluminatedMessage : QSBMessage<(uint playerId, bool illuminated)>
{
public PlayerSetIlluminatedMessage(uint playerId, bool illuminated) : base((playerId, illuminated)) { }
public override void OnReceiveLocal() => OnReceiveRemote();
public override void OnReceiveRemote() =>
QSBPlayerManager.GetPlayer(Data.playerId).QSBPlayerLightSensor.SetIlluminated(From, Data.illuminated);
public override void OnReceiveRemote()
{
var lightSensor = (SingleLightSensor)QSBPlayerManager.GetPlayer(Data.playerId).LightSensor;
if (lightSensor._illuminated == Data.illuminated)
{
return;
}
lightSensor._illuminated = Data.illuminated;
if (Data.illuminated)
{
lightSensor.OnDetectLight.Invoke();
}
else
{
lightSensor.OnDetectDarkness.Invoke();
}
}
}

View File

@ -1,87 +0,0 @@
using QSB.EchoesOfTheEye.LightSensorSync.Messages;
using QSB.Messaging;
using QSB.Player;
using QSB.WorldSync;
using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
/*
* For those who come here,
* leave while you still can.
*/
namespace QSB.EchoesOfTheEye.LightSensorSync;
/// <summary>
/// stores a bit of extra data needed for player light sensor sync
///
/// 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
/// </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 void Awake()
{
_lightSensor = GetComponent<SingleLightSensor>();
PlayerId = QSBPlayerManager.PlayerList.First(x => x.LightSensor == _lightSensor).PlayerId;
RequestInitialStatesMessage.SendInitialState += SendInitialState;
QSBPlayerManager.OnRemovePlayer += OnPlayerLeave;
}
private void OnDestroy()
{
RequestInitialStatesMessage.SendInitialState -= SendInitialState;
QSBPlayerManager.OnRemovePlayer -= OnPlayerLeave;
}
private void SendInitialState(uint to)
{
new PlayerIlluminatedByMessage(PlayerId, _illuminatedBy.ToArray()) { 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();
}
}
}

View File

@ -1,5 +1,4 @@
using OWML.Common;
using QSB.EchoesOfTheEye.LightSensorSync;
using QSB.Utility;
using UnityEngine;
@ -74,8 +73,6 @@ public partial class PlayerInfo
}
}
public QSBPlayerLightSensor QSBPlayerLightSensor;
public Vector3 Velocity
{
get

View File

@ -1,12 +1,10 @@
using QSB.EchoesOfTheEye.LightSensorSync;
using QSB.Messaging;
using QSB.Messaging;
using QSB.Player;
using QSB.Player.Messages;
using QSB.SectorSync;
using QSB.Tools;
using QSB.Utility;
using QSB.WorldSync;
using System.Linq;
using UnityEngine;
namespace QSB.PlayerBodySetup.Local;
@ -21,7 +19,7 @@ public static class LocalPlayerCreation
out Transform visibleStickPivot,
out Transform visibleStickTip)
{
DebugLog.DebugWrite($"CREATE PLAYER");
DebugLog.DebugWrite("CREATE PLAYER");
sectorDetector.Init(Locator.GetPlayerSectorDetector());
@ -38,8 +36,6 @@ public static class LocalPlayerCreation
player.CameraBody = cameraBody.gameObject;
visibleCameraRoot = cameraBody;
player.QSBPlayerLightSensor = player.LightSensor.gameObject.GetAddComponent<QSBPlayerLightSensor>();
PlayerToolsManager.InitLocal();
// stick
@ -56,4 +52,4 @@ public static class LocalPlayerCreation
return playerBody;
}
}
}

View File

@ -1,5 +1,4 @@
using QSB.Audio;
using QSB.EchoesOfTheEye.LightSensorSync;
using QSB.Player;
using QSB.RoastingSync;
using QSB.Tools;
@ -33,13 +32,13 @@ public static class RemotePlayerCreation
out Transform visibleStickPivot,
out Transform visibleStickTip)
{
DebugLog.DebugWrite($"CREATE PLAYER");
DebugLog.DebugWrite("CREATE PLAYER");
/*
* CREATE PLAYER STRUCTURE
*/
DebugLog.DebugWrite($"CREATE PLAYER STRUCTURE");
DebugLog.DebugWrite("CREATE PLAYER STRUCTURE");
// Variable naming convention is broken here to reflect OW unity project (with REMOTE_ prefixed) for readability
@ -54,7 +53,7 @@ public static class RemotePlayerCreation
* SET UP PLAYER BODY
*/
DebugLog.DebugWrite($"SET UP PLAYER BODY");
DebugLog.DebugWrite("SET UP PLAYER BODY");
player.Body = REMOTE_Player_Body;
player.ThrusterLightTracker = player.Body.GetComponentInChildren<ThrusterLightTracker>();
@ -72,7 +71,7 @@ public static class RemotePlayerCreation
* SET UP PLAYER CAMERA
*/
DebugLog.DebugWrite($"SET UP PLAYER CAMERA");
DebugLog.DebugWrite("SET UP PLAYER CAMERA");
REMOTE_PlayerCamera.GetComponent<Camera>().enabled = false;
var owcamera = REMOTE_PlayerCamera.GetComponent<OWCamera>();
@ -80,15 +79,13 @@ public static class RemotePlayerCreation
player.CameraBody = REMOTE_PlayerCamera;
visibleCameraRoot = REMOTE_PlayerCamera.transform;
player.QSBPlayerLightSensor = player.LightSensor.gameObject.GetAddComponent<QSBPlayerLightSensor>();
PlayerToolsManager.InitRemote(player);
/*
* SET UP ROASTING STICK
*/
DebugLog.DebugWrite($"SET UP ROASTING STICK");
DebugLog.DebugWrite("SET UP ROASTING STICK");
var REMOTE_Stick_Pivot = REMOTE_Stick_Root.transform.GetChild(0);
var mallowRoot = REMOTE_Stick_Pivot.Find("REMOTE_Stick_Tip/Mallow_Root");
@ -104,4 +101,4 @@ public static class RemotePlayerCreation
return REMOTE_Player_Body.transform;
}
}
}