quantum-space-buddies/QSB/RoastingSync/Messages/EnterExitRoastingMessage.cs

68 lines
1.6 KiB
C#
Raw Normal View History

using Mirror;
using OWML.Common;
2021-12-26 06:53:36 +00:00
using QSB.CampfireSync.WorldObjects;
2021-12-24 01:21:11 +00:00
using QSB.Messaging;
using QSB.Player;
2022-01-27 09:46:01 +00:00
using QSB.Player.TransformSync;
2021-12-24 01:21:11 +00:00
using QSB.Utility;
using QSB.WorldSync;
2022-03-03 03:46:33 +00:00
namespace QSB.RoastingSync.Messages;
2023-07-28 18:30:57 +00:00
public class EnterExitRoastingMessage : QSBMessage<bool>
2021-12-24 01:21:11 +00:00
{
2022-03-03 03:46:33 +00:00
static EnterExitRoastingMessage()
2021-12-24 01:21:11 +00:00
{
2022-03-03 03:46:33 +00:00
GlobalMessenger<Campfire>.AddListener(OWEvents.EnterRoastingMode, campfire => Handler(campfire, true));
GlobalMessenger.AddListener(OWEvents.ExitRoastingMode, () => Handler(null, false));
}
2021-12-24 01:21:11 +00:00
2022-03-03 03:46:33 +00:00
private static void Handler(Campfire campfire, bool roasting)
{
if (PlayerTransformSync.LocalInstance)
2021-12-24 01:21:11 +00:00
{
2022-03-03 03:46:33 +00:00
if (campfire == null)
2021-12-24 01:21:11 +00:00
{
2022-03-03 03:46:33 +00:00
new EnterExitRoastingMessage(-1, roasting).Send();
return;
}
2022-03-03 03:46:33 +00:00
var qsbObj = campfire.GetWorldObject<QSBCampfire>();
new EnterExitRoastingMessage(qsbObj.ObjectId, roasting).Send();
2021-12-24 01:21:11 +00:00
}
2022-03-03 03:46:33 +00:00
}
2021-12-24 01:21:11 +00:00
2022-03-03 03:46:33 +00:00
private int ObjectId;
2021-12-24 01:21:11 +00:00
2022-03-11 01:57:50 +00:00
private EnterExitRoastingMessage(int objectId, bool roasting) : base(roasting) =>
2022-03-03 03:46:33 +00:00
ObjectId = objectId;
2021-12-24 01:21:11 +00:00
2022-03-03 03:46:33 +00:00
public override void Serialize(NetworkWriter writer)
{
base.Serialize(writer);
writer.Write(ObjectId);
}
2021-12-24 01:21:11 +00:00
2022-03-03 03:46:33 +00:00
public override void Deserialize(NetworkReader reader)
{
base.Deserialize(reader);
ObjectId = reader.Read<int>();
}
2021-12-24 01:21:11 +00:00
2022-03-03 03:46:33 +00:00
public override bool ShouldReceive => QSBWorldSync.AllObjectsReady;
2021-12-24 01:21:11 +00:00
2022-03-03 03:46:33 +00:00
public override void OnReceiveRemote()
{
if (Data && ObjectId == -1)
2021-12-24 01:21:11 +00:00
{
2022-03-03 03:46:33 +00:00
DebugLog.ToConsole($"Error - Null campfire supplied for start roasting event!", MessageType.Error);
return;
}
2022-03-03 03:46:33 +00:00
var player = QSBPlayerManager.GetPlayer(From);
player.RoastingStick.SetActive(Data);
player.Campfire = Data
? ObjectId.GetWorldObject<QSBCampfire>()
: null;
2021-12-24 01:21:11 +00:00
}
}