move Start stuff to Init method of worldobject, since Start doesnt end up running

This commit is contained in:
JohnCorby 2022-07-24 10:32:05 -07:00
parent 7f28ee6553
commit 079762c44d
4 changed files with 21 additions and 77 deletions

View File

@ -3,9 +3,9 @@ using QSB.Player;
namespace QSB.EchoesOfTheEye.LightSensorSync.Messages;
internal class SetPlayerIlluminatedMessage : QSBMessage<(uint playerId, bool illuminated)>
internal class PlayerSetIlluminatedMessage : QSBMessage<(uint playerId, bool illuminated)>
{
public SetPlayerIlluminatedMessage(uint playerId, bool illuminated) : base((playerId, illuminated)) { }
public PlayerSetIlluminatedMessage(uint playerId, bool illuminated) : base((playerId, illuminated)) { }
public override void OnReceiveLocal() => OnReceiveRemote();
public override void OnReceiveRemote() =>

View File

@ -6,7 +6,6 @@ using QSB.Patches;
using QSB.WorldSync;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
/*
* For those who come here,
@ -20,74 +19,6 @@ internal class LightSensorPatches : QSBPatch
{
public override QSBPatchTypes Type => QSBPatchTypes.OnClientConnect;
[HarmonyPrefix]
[HarmonyPatch(nameof(SingleLightSensor.Start))]
private static bool Start(SingleLightSensor __instance)
{
if (!QSBWorldSync.AllObjectsReady)
{
return true;
}
var isPlayerLightSensor = LightSensorManager.IsPlayerLightSensor(__instance);
var qsbPlayerLightSensor = isPlayerLightSensor ? __instance.GetComponent<QSBPlayerLightSensor>() : null;
var qsbLightSensor = isPlayerLightSensor ? null : __instance.GetWorldObject<QSBLightSensor>();
if (__instance._lightDetector != null)
{
__instance._lightSources = new List<ILightSource>();
__instance._lightSourceMask = LightSourceType.VOLUME_ONLY;
if (__instance._detectFlashlight)
{
__instance._lightSourceMask |= LightSourceType.FLASHLIGHT;
}
if (__instance._detectProbe)
{
__instance._lightSourceMask |= LightSourceType.PROBE;
}
if (__instance._detectDreamLanterns)
{
__instance._lightSourceMask |= LightSourceType.DREAM_LANTERN;
}
if (__instance._detectSimpleLanterns)
{
__instance._lightSourceMask |= LightSourceType.SIMPLE_LANTERN;
}
__instance._lightDetector.OnLightVolumeEnter += __instance.OnLightSourceEnter;
__instance._lightDetector.OnLightVolumeExit += __instance.OnLightSourceExit;
}
else
{
Debug.LogError("LightSensor has no LightSourceDetector", __instance);
}
if (__instance._sector != null)
{
__instance.enabled = false;
__instance._lightDetector.GetShape().enabled = false;
if (__instance._startIlluminated)
{
if (isPlayerLightSensor)
{
qsbPlayerLightSensor._locallyIlluminated = true;
new SetPlayerIlluminatedMessage(qsbPlayerLightSensor.PlayerId, true).Send();
}
else
{
qsbLightSensor._locallyIlluminated = true;
qsbLightSensor.OnDetectLocalLight?.Invoke();
qsbLightSensor.SendMessage(new SetIlluminatedMessage(true));
}
}
}
return false;
}
[HarmonyPrefix]
[HarmonyPatch(nameof(SingleLightSensor.OnSectorOccupantsUpdated))]
private static bool OnSectorOccupantsUpdated(SingleLightSensor __instance)
@ -122,7 +53,7 @@ internal class LightSensorPatches : QSBPatch
if (qsbPlayerLightSensor._locallyIlluminated)
{
qsbPlayerLightSensor._locallyIlluminated = false;
new SetPlayerIlluminatedMessage(qsbPlayerLightSensor.PlayerId, false).Send();
new PlayerSetIlluminatedMessage(qsbPlayerLightSensor.PlayerId, false).Send();
}
}
else
@ -190,12 +121,12 @@ internal class LightSensorPatches : QSBPatch
if (!locallyIlluminated && qsbPlayerLightSensor._locallyIlluminated)
{
qsbPlayerLightSensor._locallyIlluminated = true;
new SetPlayerIlluminatedMessage(qsbPlayerLightSensor.PlayerId, true).Send();
new PlayerSetIlluminatedMessage(qsbPlayerLightSensor.PlayerId, true).Send();
}
else if (locallyIlluminated && !qsbPlayerLightSensor._locallyIlluminated)
{
qsbPlayerLightSensor._locallyIlluminated = false;
new SetPlayerIlluminatedMessage(qsbPlayerLightSensor.PlayerId, false).Send();
new PlayerSetIlluminatedMessage(qsbPlayerLightSensor.PlayerId, false).Send();
}
}
else

View File

@ -35,6 +35,15 @@ public class QSBPlayerLightSensor : MonoBehaviour
RequestInitialStatesMessage.SendInitialState += SendInitialState;
QSBPlayerManager.OnRemovePlayer += OnPlayerLeave;
if (_lightSensor._sector != null)
{
if (_lightSensor._startIlluminated)
{
_locallyIlluminated = true;
new PlayerSetIlluminatedMessage(PlayerId, true).Send();
}
}
}
private void OnDestroy()

View File

@ -38,11 +38,15 @@ internal class QSBLightSensor : WorldObject<SingleLightSensor>
public override async UniTask Init(CancellationToken ct)
{
QSBPlayerManager.OnRemovePlayer += OnPlayerLeave;
_locallyIlluminated = AttachedObject._startIlluminated;
if (_locallyIlluminated)
if (AttachedObject._sector != null)
{
_illuminatedBy.Add(QSBPlayerManager.LocalPlayerId);
if (AttachedObject._startIlluminated)
{
_locallyIlluminated = true;
OnDetectLocalLight?.Invoke();
this.SendMessage(new SetIlluminatedMessage(true));
}
}
}