quantum-space-buddies/QSB/Messaging/QSBWorldObjectMessage.cs

99 lines
2.0 KiB
C#
Raw Normal View History

2021-12-24 01:07:29 +00:00
using QSB.WorldSync;
using QuantumUNET.Transport;
2021-12-24 01:07:29 +00:00
using System;
namespace QSB.Messaging
{
public abstract class QSBWorldObjectMessage<T> : QSBMessage where T : IWorldObject
{
/// <summary>
/// set automatically by SendMessage
/// </summary>
2021-12-23 01:13:54 +00:00
internal int ObjectId;
/// <summary>
2021-12-26 07:48:32 +00:00
/// set automatically by ShouldReceive
/// </summary>
2021-12-23 23:46:18 +00:00
protected T WorldObject { get; private set; }
public override void Serialize(QNetworkWriter writer)
{
base.Serialize(writer);
writer.Write(ObjectId);
}
public override void Deserialize(QNetworkReader reader)
{
base.Deserialize(reader);
ObjectId = reader.ReadInt32();
}
2021-12-21 20:38:43 +00:00
public override bool ShouldReceive
{
get
{
if (!WorldObjectManager.AllObjectsReady)
{
return false;
}
WorldObject = ObjectId.GetWorldObject<T>();
return true;
}
}
}
public abstract class QSBBoolWorldObjectMessage<T> : QSBWorldObjectMessage<T> where T : IWorldObject
{
2021-12-23 01:13:54 +00:00
protected bool Value;
public override void Serialize(QNetworkWriter writer)
{
base.Serialize(writer);
writer.Write(Value);
}
public override void Deserialize(QNetworkReader reader)
{
base.Deserialize(reader);
Value = reader.ReadBoolean();
}
}
public abstract class QSBFloatWorldObjectMessage<T> : QSBWorldObjectMessage<T> where T : IWorldObject
{
2021-12-23 01:13:54 +00:00
protected float Value;
public override void Serialize(QNetworkWriter writer)
{
base.Serialize(writer);
writer.Write(Value);
}
public override void Deserialize(QNetworkReader reader)
{
base.Deserialize(reader);
Value = reader.ReadSingle();
}
}
public abstract class QSBEnumWorldObjectMessage<T, E> : QSBWorldObjectMessage<T>
where T : IWorldObject
where E : Enum
{
2021-12-23 01:13:54 +00:00
protected E Value;
public override void Serialize(QNetworkWriter writer)
{
base.Serialize(writer);
writer.Write((int)(object)Value);
}
public override void Deserialize(QNetworkReader reader)
{
base.Deserialize(reader);
Value = (E)(object)reader.ReadInt32();
}
}
}