This commit is contained in:
JohnCorby 2021-12-23 22:44:32 -08:00
parent 347f8d1945
commit af82e29e4c
4 changed files with 19 additions and 33 deletions

View File

@ -1,29 +0,0 @@
using QSB.ElevatorSync.WorldObjects;
using QSB.Events;
using QSB.WorldSync;
using QSB.WorldSync.Events;
namespace QSB.ElevatorSync.Messages
{
public class ElevatorEvent : QSBEvent<BoolWorldObjectMessage>
{
public override bool RequireWorldObjectsReady => true;
public override void SetupListener() => GlobalMessenger<int, bool>.AddListener(EventNames.QSBStartLift, Handler);
public override void CloseListener() => GlobalMessenger<int, bool>.RemoveListener(EventNames.QSBStartLift, Handler);
private void Handler(int id, bool isGoingUp) => SendEvent(CreateMessage(id, isGoingUp));
private BoolWorldObjectMessage CreateMessage(int id, bool isGoingUp) => new()
{
State = isGoingUp,
ObjectId = id
};
public override void OnReceiveRemote(bool server, BoolWorldObjectMessage message)
{
var elevator = QSBWorldSync.GetWorldFromId<QSBElevator>(message.ObjectId);
elevator?.RemoteCall(message.State);
}
}
}

View File

@ -0,0 +1,14 @@
using QSB.ElevatorSync.WorldObjects;
using QSB.Messaging;
namespace QSB.ElevatorSync.Messages
{
public class ElevatorMessage : QSBBoolWorldObjectMessage<QSBElevator>
{
public ElevatorMessage(bool isGoingUp) => Value = isGoingUp;
public ElevatorMessage() { }
public override void OnReceiveRemote() => WorldObject.RemoteCall(Value);
}
}

View File

@ -1,7 +1,9 @@
using HarmonyLib;
using OWML.Utils;
using QSB.ElevatorSync.Messages;
using QSB.ElevatorSync.WorldObjects;
using QSB.Events;
using QSB.Messaging;
using QSB.Patches;
using QSB.WorldSync;
@ -16,9 +18,9 @@ namespace QSB.ElevatorSync.Patches
[HarmonyPatch(typeof(Elevator), nameof(Elevator.StartLift))]
public static void Elevator_StartLift(Elevator __instance)
{
var isGoingUp = __instance.GetValue<bool>("_goingToTheEnd");
var id = QSBWorldSync.GetIdFromUnity<QSBElevator>(__instance);
QSBEventManager.FireEvent(EventNames.QSBStartLift, id, isGoingUp);
var isGoingUp = __instance._goingToTheEnd;
var qsbElevator = QSBWorldSync.GetWorldFromUnity<QSBElevator>(__instance);
qsbElevator.SendMessage(new ElevatorMessage(isGoingUp));
}
}
}

View File

@ -27,7 +27,6 @@
// Custom event names -- change if you want! These can be anything, as long as both
// sides of the GlobalMessenger (fireevent and addlistener) reference the same thing.
public const string QSBStartLift = nameof(QSBStartLift);
public const string QSBGeyserState = nameof(QSBGeyserState);
public const string QSBConversation = nameof(QSBConversation);
public const string QSBConversationStartEnd = nameof(QSBConversationStartEnd);