using QSB.Anglerfish.WorldObjects; using QSB.Events; using QSB.Player; using QSB.WorldSync; using UnityEngine; namespace QSB.Anglerfish.Events { public class AnglerChangeStateEvent : QSBEvent { public override bool RequireWorldObjectsReady => true; public override void SetupListener() => GlobalMessenger.AddListener(EventNames.QSBAnglerChangeState, Handler); public override void CloseListener() => GlobalMessenger.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(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; } } }