Characters
This commit is contained in:
222
Game/Scripts/Networking/NetworkMessages.cs
Normal file
222
Game/Scripts/Networking/NetworkMessages.cs
Normal file
@@ -0,0 +1,222 @@
|
||||
using System;
|
||||
using UnityEngine;
|
||||
|
||||
namespace MegaKoop.Game.Networking
|
||||
{
|
||||
public enum NetworkMessageType : byte
|
||||
{
|
||||
Heartbeat = 0,
|
||||
LobbyState = 1,
|
||||
PlayerInput = 2,
|
||||
CharacterTransform = 3,
|
||||
WeaponFire = 4,
|
||||
HealthSync = 5,
|
||||
ProjectileSpawn = 6
|
||||
}
|
||||
|
||||
public readonly struct NetworkMessage
|
||||
{
|
||||
public readonly NetworkMessageType Type;
|
||||
public readonly byte[] Payload;
|
||||
public readonly ulong Sender;
|
||||
|
||||
public NetworkMessage(NetworkMessageType type, byte[] payload, ulong sender)
|
||||
{
|
||||
Type = type;
|
||||
Payload = payload;
|
||||
Sender = sender;
|
||||
}
|
||||
}
|
||||
|
||||
public readonly struct PlayerInputMessage
|
||||
{
|
||||
public readonly int NetworkId;
|
||||
public readonly Vector2 MoveInput;
|
||||
public readonly bool JumpPressed;
|
||||
public readonly Vector2 LookDelta;
|
||||
|
||||
public PlayerInputMessage(int networkId, Vector2 moveInput, bool jumpPressed, Vector2 lookDelta)
|
||||
{
|
||||
NetworkId = networkId;
|
||||
MoveInput = moveInput;
|
||||
JumpPressed = jumpPressed;
|
||||
LookDelta = lookDelta;
|
||||
}
|
||||
|
||||
public static byte[] Serialize(PlayerInputMessage message)
|
||||
{
|
||||
using var writer = new NetworkWriter();
|
||||
writer.Write(message.NetworkId);
|
||||
writer.Write(message.MoveInput.x);
|
||||
writer.Write(message.MoveInput.y);
|
||||
writer.Write(message.JumpPressed);
|
||||
writer.Write(message.LookDelta.x);
|
||||
writer.Write(message.LookDelta.y);
|
||||
return writer.ToArray();
|
||||
}
|
||||
|
||||
public static PlayerInputMessage Deserialize(byte[] buffer)
|
||||
{
|
||||
using var reader = new NetworkReader(buffer);
|
||||
int id = reader.ReadInt();
|
||||
float moveX = reader.ReadFloat();
|
||||
float moveY = reader.ReadFloat();
|
||||
bool jump = reader.ReadBool();
|
||||
float lookX = reader.ReadFloat();
|
||||
float lookY = reader.ReadFloat();
|
||||
return new PlayerInputMessage(id, new Vector2(moveX, moveY), jump, new Vector2(lookX, lookY));
|
||||
}
|
||||
}
|
||||
|
||||
public readonly struct CharacterTransformMessage
|
||||
{
|
||||
public readonly int NetworkId;
|
||||
public readonly Vector3 Position;
|
||||
public readonly Quaternion Rotation;
|
||||
public readonly Vector3 Velocity;
|
||||
|
||||
public CharacterTransformMessage(int networkId, Vector3 position, Quaternion rotation, Vector3 velocity)
|
||||
{
|
||||
NetworkId = networkId;
|
||||
Position = position;
|
||||
Rotation = rotation;
|
||||
Velocity = velocity;
|
||||
}
|
||||
|
||||
public static byte[] Serialize(CharacterTransformMessage message)
|
||||
{
|
||||
using var writer = new NetworkWriter();
|
||||
writer.Write(message.NetworkId);
|
||||
writer.Write(message.Position);
|
||||
writer.Write(message.Rotation);
|
||||
writer.Write(message.Velocity);
|
||||
return writer.ToArray();
|
||||
}
|
||||
|
||||
public static CharacterTransformMessage Deserialize(byte[] buffer)
|
||||
{
|
||||
using var reader = new NetworkReader(buffer);
|
||||
int id = reader.ReadInt();
|
||||
Vector3 position = reader.ReadVector3();
|
||||
Quaternion rotation = reader.ReadQuaternion();
|
||||
Vector3 velocity = reader.ReadVector3();
|
||||
return new CharacterTransformMessage(id, position, rotation, velocity);
|
||||
}
|
||||
}
|
||||
|
||||
public readonly struct WeaponFireMessage
|
||||
{
|
||||
public readonly int NetworkId;
|
||||
public readonly int WeaponIndex;
|
||||
public readonly Vector3 MuzzlePosition;
|
||||
public readonly Vector3 Direction;
|
||||
public readonly float Timestamp;
|
||||
|
||||
public WeaponFireMessage(int networkId, int weaponIndex, Vector3 muzzlePosition, Vector3 direction, float timestamp)
|
||||
{
|
||||
NetworkId = networkId;
|
||||
WeaponIndex = weaponIndex;
|
||||
MuzzlePosition = muzzlePosition;
|
||||
Direction = direction;
|
||||
Timestamp = timestamp;
|
||||
}
|
||||
|
||||
public static byte[] Serialize(WeaponFireMessage message)
|
||||
{
|
||||
using var writer = new NetworkWriter();
|
||||
writer.Write(message.NetworkId);
|
||||
writer.Write(message.WeaponIndex);
|
||||
writer.Write(message.MuzzlePosition);
|
||||
writer.Write(message.Direction);
|
||||
writer.Write(message.Timestamp);
|
||||
return writer.ToArray();
|
||||
}
|
||||
|
||||
public static WeaponFireMessage Deserialize(byte[] buffer)
|
||||
{
|
||||
using var reader = new NetworkReader(buffer);
|
||||
int networkId = reader.ReadInt();
|
||||
int index = reader.ReadInt();
|
||||
Vector3 muzzle = reader.ReadVector3();
|
||||
Vector3 direction = reader.ReadVector3();
|
||||
float time = reader.ReadFloat();
|
||||
return new WeaponFireMessage(networkId, index, muzzle, direction, time);
|
||||
}
|
||||
}
|
||||
|
||||
public readonly struct HealthSyncMessage
|
||||
{
|
||||
public readonly int NetworkId;
|
||||
public readonly float NormalizedHealth;
|
||||
|
||||
public HealthSyncMessage(int networkId, float normalizedHealth)
|
||||
{
|
||||
NetworkId = networkId;
|
||||
NormalizedHealth = normalizedHealth;
|
||||
}
|
||||
|
||||
public static byte[] Serialize(HealthSyncMessage message)
|
||||
{
|
||||
using var writer = new NetworkWriter();
|
||||
writer.Write(message.NetworkId);
|
||||
writer.Write(message.NormalizedHealth);
|
||||
return writer.ToArray();
|
||||
}
|
||||
|
||||
public static HealthSyncMessage Deserialize(byte[] buffer)
|
||||
{
|
||||
using var reader = new NetworkReader(buffer);
|
||||
int id = reader.ReadInt();
|
||||
float normalized = reader.ReadFloat();
|
||||
return new HealthSyncMessage(id, normalized);
|
||||
}
|
||||
}
|
||||
|
||||
public readonly struct ProjectileSpawnMessage
|
||||
{
|
||||
public readonly int NetworkId;
|
||||
public readonly int WeaponIndex;
|
||||
public readonly Vector3 Position;
|
||||
public readonly Vector3 Direction;
|
||||
public readonly float Speed;
|
||||
public readonly float Life;
|
||||
public readonly float Damage;
|
||||
|
||||
public ProjectileSpawnMessage(int networkId, int weaponIndex, Vector3 position, Vector3 direction, float speed, float life, float damage)
|
||||
{
|
||||
NetworkId = networkId;
|
||||
WeaponIndex = weaponIndex;
|
||||
Position = position;
|
||||
Direction = direction;
|
||||
Speed = speed;
|
||||
Life = life;
|
||||
Damage = damage;
|
||||
}
|
||||
|
||||
public static byte[] Serialize(ProjectileSpawnMessage message)
|
||||
{
|
||||
using var writer = new NetworkWriter();
|
||||
writer.Write(message.NetworkId);
|
||||
writer.Write(message.WeaponIndex);
|
||||
writer.Write(message.Position);
|
||||
writer.Write(message.Direction);
|
||||
writer.Write(message.Speed);
|
||||
writer.Write(message.Life);
|
||||
writer.Write(message.Damage);
|
||||
return writer.ToArray();
|
||||
}
|
||||
|
||||
public static ProjectileSpawnMessage Deserialize(byte[] buffer)
|
||||
{
|
||||
using var reader = new NetworkReader(buffer);
|
||||
int networkId = reader.ReadInt();
|
||||
int index = reader.ReadInt();
|
||||
Vector3 position = reader.ReadVector3();
|
||||
Vector3 direction = reader.ReadVector3();
|
||||
float speed = reader.ReadFloat();
|
||||
float life = reader.ReadFloat();
|
||||
float damage = reader.ReadFloat();
|
||||
return new ProjectileSpawnMessage(networkId, index, position, direction, speed, life, damage);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user