quantum-space-buddies/QSB/Player/Messages/PlayerEntangledMessage.cs

54 lines
1.2 KiB
C#
Raw Normal View History

using Mirror;
using QSB.Messaging;
2021-12-23 04:45:25 +00:00
using QSB.QuantumSync.WorldObjects;
using QSB.WorldSync;
2021-12-23 21:49:47 +00:00
namespace QSB.Player.Messages
2021-12-23 04:45:25 +00:00
{
// almost a world object message, but supports null (-1) as well
2022-01-27 05:00:12 +00:00
internal class PlayerEntangledMessage : QSBMessage<int>
2021-12-23 04:45:25 +00:00
{
2022-01-27 05:00:12 +00:00
public PlayerEntangledMessage(int objectId) => Value = objectId;
2021-12-23 04:45:25 +00:00
public override bool ShouldReceive => QSBWorldSync.AllObjectsReady;
2021-12-23 04:45:25 +00:00
public override void OnReceiveLocal()
{
var player = QSBPlayerManager.LocalPlayer;
2022-01-27 05:00:12 +00:00
if (Value == -1)
2021-12-23 04:45:25 +00:00
{
player.EntangledObject = null;
return;
}
2022-01-27 05:00:12 +00:00
var quantumObject = Value.GetWorldObject<IQSBQuantumObject>();
2021-12-23 04:45:25 +00:00
player.EntangledObject = quantumObject;
}
public override void OnReceiveRemote()
{
var player = QSBPlayerManager.GetPlayer(From);
2022-01-27 05:00:12 +00:00
if (Value == -1)
2021-12-23 04:45:25 +00:00
{
player.EntangledObject = null;
return;
}
2022-01-27 05:00:12 +00:00
var quantumObject = Value.GetWorldObject<IQSBQuantumObject>();
2021-12-23 04:45:25 +00:00
player.EntangledObject = quantumObject;
}
public override void Serialize(NetworkWriter writer)
2021-12-23 04:45:25 +00:00
{
base.Serialize(writer);
2022-01-27 05:00:12 +00:00
writer.Write(Value);
2021-12-23 04:45:25 +00:00
}
public override void Deserialize(NetworkReader reader)
2021-12-23 04:45:25 +00:00
{
base.Deserialize(reader);
2022-01-27 05:00:12 +00:00
Value = reader.Read<int>();
2021-12-23 04:45:25 +00:00
}
}
}