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

74 lines
2.1 KiB
C#
Raw Normal View History

2021-12-26 06:53:36 +00:00
using OWML.Common;
using QSB.Messaging;
using QSB.Player;
using QSB.Utility;
using QSB.WorldSync;
using System.Linq;
using UnityEngine;
2022-03-03 03:46:33 +00:00
namespace QSB.RoastingSync.Messages;
internal class MarshmallowEventMessage : QSBMessage<MarshmallowMessageType>
2021-12-26 06:53:36 +00:00
{
2022-03-11 01:57:50 +00:00
public MarshmallowEventMessage(MarshmallowMessageType type) : base(type) { }
2021-12-26 06:53:36 +00:00
2022-03-03 03:46:33 +00:00
public override bool ShouldReceive => QSBWorldSync.AllObjectsReady;
2021-12-26 06:53:36 +00:00
2022-03-03 03:46:33 +00:00
public override void OnReceiveRemote()
{
var player = QSBPlayerManager.GetPlayer(From);
if (player.Marshmallow == null)
2021-12-26 06:53:36 +00:00
{
2022-03-06 09:01:02 +00:00
DebugLog.ToConsole($"Warning - Marshmallow is null for {player}.", MessageType.Warning);
2022-03-03 03:46:33 +00:00
return;
2021-12-26 06:53:36 +00:00
}
2022-03-03 03:46:33 +00:00
switch (Data)
2021-12-26 06:53:36 +00:00
{
2022-03-03 03:46:33 +00:00
case MarshmallowMessageType.Burn:
player.Marshmallow.Burn();
break;
case MarshmallowMessageType.Extinguish:
player.Marshmallow.Extinguish();
break;
case MarshmallowMessageType.Remove:
player.Marshmallow.RemoveMallow();
break;
case MarshmallowMessageType.Replace:
player.Marshmallow.SpawnMallow();
break;
case MarshmallowMessageType.Shrivel:
player.Marshmallow.Shrivel();
break;
case MarshmallowMessageType.Toss:
TossMarshmallow(player);
break;
}
}
2021-12-26 06:53:36 +00:00
2022-03-03 03:46:33 +00:00
private static void TossMarshmallow(PlayerInfo player)
{
var stick = player.RoastingStick;
var stickTip = stick.transform.GetChild(0);
2021-12-26 06:53:36 +00:00
2022-03-03 03:46:33 +00:00
var mallowPrefab = QSBWorldSync.GetUnityObjects<RoastingStickController>().First()._mallowBodyPrefab;
2021-12-26 06:53:36 +00:00
2022-03-03 03:46:33 +00:00
var tossedMallow = Object.Instantiate(mallowPrefab, stickTip.position, stickTip.rotation);
var rigidbody = tossedMallow.GetComponent<OWRigidbody>();
if (player.Campfire == null)
{
2022-03-06 09:01:02 +00:00
DebugLog.ToConsole($"Error - Campfire for {player} is null.", MessageType.Error);
2022-03-03 03:46:33 +00:00
return;
}
2021-12-26 06:53:36 +00:00
2022-03-03 03:46:33 +00:00
rigidbody.SetVelocity(player.Campfire.AttachedObject.GetAttachedOWRigidbody().GetPointVelocity(stickTip.position) + stickTip.forward * 3f);
rigidbody.SetAngularVelocity(stickTip.right * 10f);
if (player.Marshmallow == null)
{
2022-03-06 09:01:02 +00:00
DebugLog.ToConsole($"Error - Marshmallow for {player} is null.", MessageType.Error);
2022-03-03 03:46:33 +00:00
return;
2021-12-26 06:53:36 +00:00
}
2022-03-03 03:46:33 +00:00
tossedMallow.GetComponentInChildren<MeshRenderer>().material.color = player.Marshmallow._burntColor;
2021-12-26 06:53:36 +00:00
}
}