angler change state

This commit is contained in:
JohnCorby 2021-12-23 15:57:41 -08:00
parent e88ef0cc9c
commit 2232e889ab
5 changed files with 88 additions and 106 deletions

View File

@ -1,67 +0,0 @@
using QSB.Anglerfish.WorldObjects;
using QSB.Events;
using QSB.Player;
using QSB.WorldSync;
using UnityEngine;
namespace QSB.Anglerfish.Events
{
public class AnglerChangeStateEvent : QSBEvent<AnglerChangeStateMessage>
{
public override bool RequireWorldObjectsReady => true;
public override void SetupListener()
=> GlobalMessenger<QSBAngler>.AddListener(EventNames.QSBAnglerChangeState, Handler);
public override void CloseListener()
=> GlobalMessenger<QSBAngler>.RemoveListener(EventNames.QSBAnglerChangeState, Handler);
private void Handler(QSBAngler qsbAngler) => SendEvent(CreateMessage(qsbAngler));
private AnglerChangeStateMessage CreateMessage(QSBAngler qsbAngler) => new()
{
ObjectId = qsbAngler.ObjectId,
EnumValue = qsbAngler.AttachedObject._currentState,
TargetId = TargetToId(qsbAngler.TargetTransform),
LocalDisturbancePos = qsbAngler.AttachedObject._localDisturbancePos
};
public override void OnReceiveRemote(bool isHost, AnglerChangeStateMessage message)
{
var qsbAngler = QSBWorldSync.GetWorldFromId<QSBAngler>(message.ObjectId);
qsbAngler.TargetTransform = IdToTarget(message.TargetId);
qsbAngler.AttachedObject._localDisturbancePos = message.LocalDisturbancePos;
qsbAngler.AttachedObject.ChangeState(message.EnumValue);
}
private static uint TargetToId(Transform transform)
{
if (transform == null)
{
return uint.MaxValue;
}
if (transform == Locator.GetShipTransform())
{
return uint.MaxValue - 1;
}
return QSBPlayerManager.LocalPlayerId;
}
private static Transform IdToTarget(uint id)
{
if (id == uint.MaxValue)
{
return null;
}
if (id == uint.MaxValue - 1)
{
return Locator.GetShipTransform();
}
return QSBPlayerManager.GetPlayer(id).Body.transform;
}
}
}

View File

@ -1,26 +0,0 @@
using QSB.WorldSync.Events;
using QuantumUNET.Transport;
using UnityEngine;
namespace QSB.Anglerfish.Events
{
public class AnglerChangeStateMessage : EnumWorldObjectMessage<AnglerfishController.AnglerState>
{
public uint TargetId;
public Vector3 LocalDisturbancePos;
public override void Deserialize(QNetworkReader reader)
{
base.Deserialize(reader);
TargetId = reader.ReadUInt32();
LocalDisturbancePos = reader.ReadVector3();
}
public override void Serialize(QNetworkWriter writer)
{
base.Serialize(writer);
writer.Write(TargetId);
writer.Write(LocalDisturbancePos);
}
}
}

View File

@ -0,0 +1,74 @@
using QSB.Anglerfish.WorldObjects;
using QSB.Messaging;
using QSB.Player;
using QuantumUNET.Transport;
using UnityEngine;
namespace QSB.Anglerfish.Messages
{
public class AnglerChangeStateMessage : QSBEnumWorldObjectMessage<QSBAngler, AnglerfishController.AnglerState>
{
private uint TargetId;
private Vector3 LocalDisturbancePos;
public AnglerChangeStateMessage(QSBAngler qsbAngler)
{
Value = qsbAngler.AttachedObject._currentState;
TargetId = TargetToId(qsbAngler.TargetTransform);
LocalDisturbancePos = qsbAngler.AttachedObject._localDisturbancePos;
}
public AnglerChangeStateMessage() { }
public override void Serialize(QNetworkWriter writer)
{
base.Serialize(writer);
writer.Write(TargetId);
writer.Write(LocalDisturbancePos);
}
public override void Deserialize(QNetworkReader reader)
{
base.Deserialize(reader);
TargetId = reader.ReadUInt32();
LocalDisturbancePos = reader.ReadVector3();
}
public override void OnReceiveRemote()
{
WorldObject.TargetTransform = IdToTarget(TargetId);
WorldObject.AttachedObject._localDisturbancePos = LocalDisturbancePos;
WorldObject.AttachedObject.ChangeState(Value);
}
private static uint TargetToId(Transform transform)
{
if (transform == null)
{
return uint.MaxValue;
}
if (transform == Locator.GetShipTransform())
{
return uint.MaxValue - 1;
}
return QSBPlayerManager.LocalPlayerId;
}
private static Transform IdToTarget(uint id)
{
if (id == uint.MaxValue)
{
return null;
}
if (id == uint.MaxValue - 1)
{
return Locator.GetShipTransform();
}
return QSBPlayerManager.GetPlayer(id).Body.transform;
}
}
}

View File

@ -1,7 +1,9 @@
using HarmonyLib;
using QSB.Anglerfish.Messages;
using QSB.Anglerfish.WorldObjects;
using QSB.AuthoritySync;
using QSB.Events;
using QSB.Messaging;
using QSB.Patches;
using QSB.Utility;
using QSB.WorldSync;
@ -40,7 +42,7 @@ namespace QSB.Anglerfish.Patches
if (qsbAngler.TargetTransform != null && sectorDetector.GetAttachedOWRigidbody().transform == qsbAngler.TargetTransform)
{
qsbAngler.TargetTransform = null;
QSBEventManager.FireEvent(EventNames.QSBAnglerChangeState, qsbAngler);
qsbAngler.SendMessage(new AnglerChangeStateMessage(qsbAngler));
}
return false;
@ -62,7 +64,7 @@ namespace QSB.Anglerfish.Patches
if ((__instance._brambleBody.transform.TransformPoint(__instance._localDisturbancePos) - __instance._anglerBody.GetPosition()).sqrMagnitude < __instance._arrivalDistance * __instance._arrivalDistance)
{
__instance.ChangeState(AnglerfishController.AnglerState.Lurking);
QSBEventManager.FireEvent(EventNames.QSBAnglerChangeState, qsbAngler);
qsbAngler.SendMessage(new AnglerChangeStateMessage(qsbAngler));
return false;
}
break;
@ -70,14 +72,14 @@ namespace QSB.Anglerfish.Patches
if (qsbAngler.TargetTransform == null)
{
__instance.ChangeState(AnglerfishController.AnglerState.Lurking);
QSBEventManager.FireEvent(EventNames.QSBAnglerChangeState, qsbAngler);
qsbAngler.SendMessage(new AnglerChangeStateMessage(qsbAngler));
return false;
}
if ((qsbAngler.TargetTransform.position - __instance._anglerBody.GetPosition()).sqrMagnitude > __instance._escapeDistance * __instance._escapeDistance)
{
qsbAngler.TargetTransform = null;
__instance.ChangeState(AnglerfishController.AnglerState.Lurking);
QSBEventManager.FireEvent(EventNames.QSBAnglerChangeState, qsbAngler);
qsbAngler.SendMessage(new AnglerChangeStateMessage(qsbAngler));
return false;
}
break;
@ -87,7 +89,7 @@ namespace QSB.Anglerfish.Patches
if (qsbAngler.TargetTransform == null)
{
__instance.ChangeState(AnglerfishController.AnglerState.Lurking);
QSBEventManager.FireEvent(EventNames.QSBAnglerChangeState, qsbAngler);
qsbAngler.SendMessage(new AnglerChangeStateMessage(qsbAngler));
return false;
}
var num = Time.time - __instance._consumeStartTime;
@ -117,7 +119,7 @@ namespace QSB.Anglerfish.Patches
else
{
qsbAngler.TargetTransform = null;
QSBEventManager.FireEvent(EventNames.QSBAnglerChangeState, qsbAngler);
qsbAngler.SendMessage(new AnglerChangeStateMessage(qsbAngler));
}
break;
case AnglerfishController.AnglerState.Stunned:
@ -127,11 +129,11 @@ namespace QSB.Anglerfish.Patches
if (qsbAngler.TargetTransform != null)
{
__instance.ChangeState(AnglerfishController.AnglerState.Chasing);
QSBEventManager.FireEvent(EventNames.QSBAnglerChangeState, qsbAngler);
qsbAngler.SendMessage(new AnglerChangeStateMessage(qsbAngler));
return false;
}
__instance.ChangeState(AnglerfishController.AnglerState.Lurking);
QSBEventManager.FireEvent(EventNames.QSBAnglerChangeState, qsbAngler);
qsbAngler.SendMessage(new AnglerChangeStateMessage(qsbAngler));
}
break;
default:
@ -237,7 +239,7 @@ namespace QSB.Anglerfish.Patches
{
qsbAngler.TargetTransform = attachedOWRigidbody.transform;
__instance.ChangeState(AnglerfishController.AnglerState.Chasing);
QSBEventManager.FireEvent(EventNames.QSBAnglerChangeState, qsbAngler);
qsbAngler.SendMessage(new AnglerChangeStateMessage(qsbAngler));
return false;
}
@ -264,7 +266,7 @@ namespace QSB.Anglerfish.Patches
{
__instance.ChangeState(AnglerfishController.AnglerState.Chasing);
}
QSBEventManager.FireEvent(EventNames.QSBAnglerChangeState, qsbAngler);
qsbAngler.SendMessage(new AnglerChangeStateMessage(qsbAngler));
return false;
}
}
@ -275,7 +277,7 @@ namespace QSB.Anglerfish.Patches
{
__instance.ChangeState(AnglerfishController.AnglerState.Investigating);
}
QSBEventManager.FireEvent(EventNames.QSBAnglerChangeState, qsbAngler);
qsbAngler.SendMessage(new AnglerChangeStateMessage(qsbAngler));
}
return false;
@ -301,7 +303,7 @@ namespace QSB.Anglerfish.Patches
qsbAngler.TargetTransform = caughtBody.transform;
__instance._consumeStartTime = Time.time;
__instance.ChangeState(AnglerfishController.AnglerState.Consuming);
QSBEventManager.FireEvent(EventNames.QSBAnglerChangeState, qsbAngler);
qsbAngler.SendMessage(new AnglerChangeStateMessage(qsbAngler));
}
return false;

View File

@ -68,7 +68,6 @@
public const string QSBEnterSatelliteCamera = nameof(QSBEnterSatelliteCamera);
public const string QSBExitSatelliteCamera = nameof(QSBExitSatelliteCamera);
public const string QSBSatelliteSnapshot = nameof(QSBSatelliteSnapshot);
public const string QSBAnglerChangeState = nameof(QSBAnglerChangeState);
public const string QSBSatelliteRepairTick = nameof(QSBSatelliteRepairTick);
public const string QSBSatelliteRepaired = nameof(QSBSatelliteRepairTick);
public const string QSBJellyfishRising = nameof(QSBJellyfishRising);