quakeball/Assets/Scripts/Networking/Packets/PlayerActionPckt.cs

72 lines
2.0 KiB
C#
Raw Normal View History

2020-08-08 04:26:17 +02:00
using UnityEngine;
2020-08-08 03:21:02 +02:00
using NeonTea.Quakeball.TeaNet.Packets;
namespace NeonTea.Quakeball.Networking.Packets {
public class PlayerActionPckt : Packet {
public ulong PlayerId;
public PlayerAction Action;
2020-08-08 03:53:38 +02:00
public Serializable Serializable;
2020-08-08 03:21:02 +02:00
public PlayerActionPckt() { }
2020-08-08 04:26:17 +02:00
public PlayerActionPckt(PlayerAction action, Serializable serializable, ulong id = 0) {
2020-08-08 03:21:02 +02:00
Action = action;
2020-08-08 04:26:17 +02:00
Serializable = serializable;
2020-08-08 03:21:02 +02:00
PlayerId = id;
}
public override void Read(ByteBuffer buffer) {
PlayerId = buffer.ReadULong();
switch (buffer.Read()) {
case 0:
Action = PlayerAction.Jump;
break;
2020-08-08 03:53:38 +02:00
case 1:
Action = PlayerAction.Shoot;
2020-08-08 04:26:17 +02:00
Serializable = new ShootData();
Serializable.Read(buffer);
2020-08-08 03:53:38 +02:00
break;
2020-08-08 03:21:02 +02:00
}
}
public override void Write(ByteBuffer buffer) {
buffer.Write(PlayerId);
buffer.Write((byte)Action);
2020-08-08 03:53:38 +02:00
if (Action == PlayerAction.Shoot) {
buffer.Write(Serializable);
}
2020-08-08 03:21:02 +02:00
}
}
public enum PlayerAction {
Null = byte.MaxValue,
Jump = 0,
2020-08-08 03:53:38 +02:00
Shoot = 1,
}
public class ShootData : Serializable {
2020-08-08 04:26:17 +02:00
public float Pitch;
public float Yaw;
public PlayerSyncPacket SyncPckt;
public ShootData() { }
public ShootData(float pitch, float yaw, PlayerSyncPacket syncPckt) {
Pitch = pitch;
Yaw = yaw;
SyncPckt = syncPckt;
}
2020-08-08 03:53:38 +02:00
public void Read(ByteBuffer buffer) {
2020-08-08 04:26:17 +02:00
Pitch = buffer.ReadFloat();
Yaw = buffer.ReadFloat();
SyncPckt = new PlayerSyncPacket();
SyncPckt.Read(buffer);
2020-08-08 03:53:38 +02:00
}
public void Write(ByteBuffer buffer) {
2020-08-08 04:26:17 +02:00
buffer.Write(Pitch);
buffer.Write(Yaw);
SyncPckt.Write(buffer);
2020-08-08 03:53:38 +02:00
}
2020-08-08 03:21:02 +02:00
}
}