using Cyber.Entities;
using UnityEngine;
using UnityEngine.Networking;
namespace Cyber.Networking.Messages {
///
/// Packet which tells the client to spawn an entity corresponding to the data inside.
///
public class SpawnEntityPkt : MessageBase {
///
/// The type of entity to spawn.
///
public EntityType EntityType;
///
/// The position of the entity, where it should be spawned
///
public Vector3 Position;
///
/// The List of Sync ID's for the entity.
///
public int[] SyncBaseIDList;
///
/// Connection ID of the owner of this entity
///
public int OwnerID;
///
/// Create a packet of information about spawning an entity to send the clients.
///
/// Type of entity to spawn.
/// Position where the entity will be spawned.
/// List of Sync ID's for the entity.
/// The Connection ID who owns this entity (or -1).
public SpawnEntityPkt(EntityType entityType, Vector3 position, int[] syncIDList, int ownerID) {
EntityType = entityType;
Position = position;
SyncBaseIDList = syncIDList;
OwnerID = ownerID;
}
///
/// Parameter-less constructor using when deserializing the message.
///
public SpawnEntityPkt() {
}
///
/// Used to deserialize a message received via networking.
///
///
public override void Deserialize(NetworkReader reader) {
EntityType = (EntityType) reader.ReadInt16();
Position = reader.ReadVector3();
OwnerID = reader.ReadInt32();
byte[][] ByteArray = new byte[4][];
ByteArray[0] = reader.ReadBytesAndSize();
ByteArray[1] = reader.ReadBytesAndSize();
ByteArray[2] = reader.ReadBytesAndSize();
ByteArray[3] = reader.ReadBytesAndSize();
SyncBaseIDList = NetworkHelper.DeserializeIntArray(ByteArray);
}
///
/// Used to serialize the message before it is sent.
///
///
public override void Serialize(NetworkWriter writer) {
writer.Write((short) EntityType);
writer.Write(Position);
writer.Write(OwnerID);
byte[][] ByteArray = NetworkHelper.SerializeIntArray(SyncBaseIDList);
writer.WriteBytesFull(ByteArray[0]);
writer.WriteBytesFull(ByteArray[1]);
writer.WriteBytesFull(ByteArray[2]);
writer.WriteBytesFull(ByteArray[3]);
}
}
}