change hatch patch to be open and close

This commit is contained in:
Mister_Nebula 2021-04-12 11:43:22 +01:00
parent d20739d2f3
commit a250a222a0
4 changed files with 60 additions and 37 deletions

View File

@ -63,6 +63,6 @@
public static string QSBExitPlatform = "QSBExitPlatform";
public static string QSBCampfireState = "QSBCampfireState";
public static string QSBMarshmallowEvent = "QSBMarshmallowEvent";
public static string QSBOpenHatch = "QSBOpenHatch";
public static string QSBHatchState = "QSBOpenHatch";
}
}

View File

@ -0,0 +1,39 @@
using OWML.Utils;
using QSB.Events;
using QSB.Messaging;
namespace QSB.ShipSync.Events
{
class HatchEvent : QSBEvent<BoolMessage>
{
public override EventType Type => EventType.OpenHatch;
public override void SetupListener()
=> GlobalMessenger<bool>.AddListener(EventNames.QSBHatchState, Handler);
public override void CloseListener()
=> GlobalMessenger<bool>.RemoveListener(EventNames.QSBHatchState, Handler);
private void Handler(bool open) => SendEvent(CreateMessage(open));
private BoolMessage CreateMessage(bool open) => new BoolMessage
{
AboutId = LocalPlayerId,
Value = open
};
public override void OnReceiveRemote(bool server, BoolMessage message)
{
var shipTransform = Locator.GetShipTransform();
var hatchController = shipTransform.GetComponentInChildren<HatchController>();
if (message.Value)
{
hatchController.Invoke("OpenHatch");
}
else
{
hatchController.Invoke("CloseHatch");
}
}
}
}

View File

@ -1,31 +0,0 @@
using OWML.Utils;
using QSB.Events;
using QSB.Messaging;
namespace QSB.ShipSync.Events
{
class OpenHatchEvent : QSBEvent<PlayerMessage>
{
public override EventType Type => EventType.OpenHatch;
public override void SetupListener()
=> GlobalMessenger.AddListener(EventNames.QSBOpenHatch, Handler);
public override void CloseListener()
=> GlobalMessenger.RemoveListener(EventNames.QSBOpenHatch, Handler);
private void Handler() => SendEvent(CreateMessage());
private PlayerMessage CreateMessage() => new PlayerMessage
{
AboutId = LocalPlayerId
};
public override void OnReceiveRemote(bool server, PlayerMessage message)
{
var shipTransform = Locator.GetShipTransform();
var hatchController = shipTransform.GetComponentInChildren<HatchController>();
hatchController.Invoke("OpenHatch");
}
}
}

View File

@ -7,15 +7,30 @@ namespace QSB.ShipSync.Patches
{
public override QSBPatchTypes Type => QSBPatchTypes.OnClientConnect;
public override void DoPatches()
=> QSBCore.HarmonyHelper.AddPrefix<HatchController>("OnPressInteract", typeof(ShipPatches), nameof(HatchController_OnPressInteract));
public override void DoPatches()
{
QSBCore.HarmonyHelper.AddPrefix<HatchController>("OnPressInteract", typeof(ShipPatches), nameof(HatchController_OnPressInteract));
QSBCore.HarmonyHelper.AddPrefix<HatchController>("OnEntry", typeof(ShipPatches), nameof(HatchController_OnEntry));
}
public override void DoUnpatches()
=> QSBCore.HarmonyHelper.Unpatch<HatchController>("OnPressInteract");
public override void DoUnpatches()
{
QSBCore.HarmonyHelper.Unpatch<HatchController>("OnPressInteract");
QSBCore.HarmonyHelper.Unpatch<HatchController>("OnEntry");
}
public static bool HatchController_OnPressInteract()
{
QSBEventManager.FireEvent(EventNames.QSBOpenHatch);
QSBEventManager.FireEvent(EventNames.QSBHatchState, true);
return true;
}
public static bool HatchController_OnEntry(GameObjectActivationTrigger hitObj)
{
if (hitObj.CompareTag("PlayerDetector"))
{
QSBEventManager.FireEvent(EventNames.QSBHatchState, false);
}
return true;
}
}