quantum-space-buddies/QSB/RoastingSync/Events/EnterExitRoastingEvent.cs

68 lines
1.9 KiB
C#
Raw Normal View History

2021-03-31 09:30:51 +00:00
using QSB.CampfireSync.WorldObjects;
using QSB.Events;
using QSB.Player;
2021-03-30 16:28:05 +00:00
using QSB.Utility;
2021-03-31 09:30:51 +00:00
using QSB.WorldSync;
using QSB.WorldSync.Events;
2021-03-30 16:28:05 +00:00
namespace QSB.RoastingSync.Events
{
2021-03-31 09:30:51 +00:00
internal class EnterExitRoastingEvent : QSBEvent<BoolWorldObjectMessage>
2021-03-30 16:28:05 +00:00
{
public override EventType Type => EventType.Roasting;
public override void SetupListener()
{
2021-03-31 09:30:51 +00:00
GlobalMessenger<Campfire>.AddListener(EventNames.EnterRoastingMode, (Campfire fire) => Handler(fire, true));
GlobalMessenger.AddListener(EventNames.ExitRoastingMode, () => Handler(null, false));
2021-03-30 16:28:05 +00:00
}
2021-03-31 09:30:51 +00:00
public override void CloseListener()
2021-03-30 16:28:05 +00:00
{
2021-03-31 09:30:51 +00:00
GlobalMessenger<Campfire>.RemoveListener(EventNames.EnterRoastingMode, (Campfire fire) => Handler(fire, true));
GlobalMessenger.RemoveListener(EventNames.ExitRoastingMode, () => Handler(null, false));
2021-03-30 16:28:05 +00:00
}
2021-03-31 09:30:51 +00:00
private void Handler(Campfire campfire, bool roasting)
{
if (campfire == null)
{
SendEvent(CreateMessage(-1, roasting));
return;
}
var qsbObj = QSBWorldSync.GetWorldFromUnity<QSBCampfire, Campfire>(campfire);
SendEvent(CreateMessage(qsbObj.ObjectId, roasting));
}
2021-03-30 16:28:05 +00:00
2021-03-31 09:30:51 +00:00
private BoolWorldObjectMessage CreateMessage(int objectId, bool roasting) => new BoolWorldObjectMessage
2021-03-30 16:28:05 +00:00
{
AboutId = LocalPlayerId,
2021-03-31 09:30:51 +00:00
State = roasting,
ObjectId = objectId
2021-03-30 16:28:05 +00:00
};
2021-03-31 09:30:51 +00:00
public override void OnReceiveRemote(bool server, BoolWorldObjectMessage message)
2021-03-30 16:28:05 +00:00
{
2021-03-31 09:30:51 +00:00
if (!QSBCore.HasWokenUp)
{
return;
}
if (message.State && message.ObjectId == -1)
{
DebugLog.ToConsole($"Error - Null campfire supplied for start roasting event!", OWML.Common.MessageType.Error);
return;
}
var player = QSBPlayerManager.GetPlayer(message.AboutId);
player.RoastingStick.SetActive(message.State);
if (message.State)
{
player.Campfire = QSBWorldSync.GetWorldFromId<QSBCampfire>(message.ObjectId);
}
else
{
player.Campfire = null;
}
2021-03-30 16:28:05 +00:00
}
}
}