Oprava spawnování monster, optimalizace v CommandProcessor a EventProcessor. Přesunutí některých tříd do vlastních namespaců, pročištění kódu, úpravy formátování, odstranění nepoužívaných souborů a zakomentovaného kódu

This commit is contained in:
Perry 2026-03-28 09:59:31 +01:00
parent e5d746d597
commit 243f071a43
62 changed files with 873 additions and 1217 deletions

View file

@ -12,6 +12,4 @@ public struct EventQueue : INetSerializable {
public void Deserialize(NetDataReader reader) {
Events = reader.GetArray<GameEvent>();
}
// public GameEvent this[int index] => Events[index];
}

View file

@ -1,44 +1,44 @@
using System.Diagnostics.CodeAnalysis;
using GlobalClassLib;
using LiteNetLib.Utils;
namespace PacketLib;
#nullable disable
public struct GameEvent : INetSerializable{
public static GameEvent PLAYER_JOIN(int pid) => new(){ID = 0, Args = [pid] };
public static GameEvent PLAYER_LEAVE(int pid) => new(){ID = 1, Args = [pid] };
public static GameEvent SWITCH_CAM(int pid, int id) => new(){ID = 2, Args = [pid, id] };
public static GameEvent TOGGLE_MONITOR(int pid, bool state) => new(){ID = 3, Args = [pid, state ? 1 : 0]};
public static GameEvent TOGGLE_DOOR_OFFICE(int pid, int doorId, bool state) => new(){ID = 4, Args = [pid, doorId, state ? 1 : 0]};
public static GameEvent TOGGLE_DOOR_REMOTE(int pid, (int, int) doorId, bool state) => new(){ID = 5, Args = [pid, doorId.Item1, doorId.Item2, state ? 1 : 0]};
public static GameEvent PLAYER_JOIN(int pid) => new(){Id = 0, Args = [pid] };
public static GameEvent PLAYER_LEAVE(int pid) => new(){Id = 1, Args = [pid] };
public static GameEvent SWITCH_CAM(int pid, int id) => new(){Id = 2, Args = [pid, id] };
public static GameEvent TOGGLE_MONITOR(int pid, bool state) => new(){Id = 3, Args = [pid, state ? 1 : 0]};
public static GameEvent TOGGLE_DOOR_OFFICE(int pid, int doorId, bool state) => new(){Id = 4, Args = [pid, doorId, state ? 1 : 0]};
public static GameEvent TOGGLE_DOOR_REMOTE(int pid, (int, int) doorId, bool state) => new(){Id = 5, Args = [pid, doorId.Item1, doorId.Item2, state ? 1 : 0]};
public static GameEvent ENEMY_SPAWN(int enemyTypeId, int enemyId, int difficulty, int camId) => new(){ ID = 6, Args = [enemyTypeId, enemyId, difficulty, camId] };
public static GameEvent ENEMY_MOVEMENT(int enemyId, int camId) => new(){ID = 7, Args = [enemyId, camId]};
public static GameEvent ENEMY_ATTACK(int enemyId, int pid) => new(){ ID = 8, Args =[enemyId, pid] };
public static GameEvent ENEMY_RESET(int enemyId, int camId) => new(){ID = 9, Args = [enemyId, camId]};
public static GameEvent ENEMY_SPAWN(EnemyType enemyTypeId, int enemyId, int difficulty, int camId) => new(){ Id = 6, Args = [(int)enemyTypeId, enemyId, difficulty, camId] };
public static GameEvent ENEMY_MOVEMENT(int enemyId, int camId) => new(){Id = 7, Args = [enemyId, camId]};
public static GameEvent ENEMY_ATTACK(int enemyId, int pid) => new(){ Id = 8, Args =[enemyId, pid] };
public static GameEvent ENEMY_RESET(int enemyId, int camId) => new(){Id = 9, Args = [enemyId, camId]};
public static GameEvent SPOT_SET_ACTIVE(int enemyId, bool state) => new(){ ID = 10, Args = [enemyId, state ? 1 : 0] };
public static GameEvent SPOT_SET_ACTIVE(int enemyId, bool state) => new(){ Id = 10, Args = [enemyId, state ? 1 : 0] };
public static GameEvent PLAYER_WIN(int pid) => new(){ID = 11, Args = [pid]};
public static GameEvent GAME_START() => new(){ID = 12};
public static GameEvent POWER_TICK(int pid, int power) => new(){ID = 13, Args = [pid, power]};
public static GameEvent POWER_OUT(int pid) => new(){ID = 14, Args = [pid]};
public static GameEvent TOGGLE_LIGHT(int pid, int camId, bool state) => new(){ID = 15, Args = [pid, camId, state ? 1 : 0]};
public static GameEvent NEKO_ANGERED(int pid, int enemyId) => new(){ID = 16, Args = [pid, enemyId]};
public static GameEvent VENT_USED() => new(){ID = 17};
public static GameEvent NEXT_PHASE() => new(){ID = 18};
public static GameEvent PLAYER_WIN(int pid) => new(){Id = 11, Args = [pid]};
public static GameEvent GAME_START() => new(){Id = 12};
public static GameEvent POWER_TICK(int pid, int power) => new(){Id = 13, Args = [pid, power]};
public static GameEvent POWER_OUT(int pid) => new(){Id = 14, Args = [pid]};
public static GameEvent TOGGLE_LIGHT(int pid, int camId, bool state) => new(){Id = 15, Args = [pid, camId, state ? 1 : 0]};
public static GameEvent NEKO_ANGERED(int pid, int enemyId) => new(){Id = 16, Args = [pid, enemyId]};
public static GameEvent VENT_USED() => new(){Id = 17};
public static GameEvent NEXT_PHASE() => new(){Id = 18};
public int ID{ get; set; }
public bool Hideable => ID < 0;
public int Id{ get; set; }
public int[] Args{ get; private set; }
public void Serialize(NetDataWriter writer) {
writer.Put(ID);
writer.Put(Id);
writer.PutArray(Args);
}
public void Deserialize(NetDataReader reader) {
ID = reader.GetInt();
Id = reader.GetInt();
Args = reader.GetIntArray();
}

View file

@ -1,6 +1,5 @@
namespace PacketLib;
public class JoinAcceptPacket {
public PlayerState state { get; set; }
// public PlayerState otherPlayerState { get; set; }
public PlayerState State { get; set; }
}

View file

@ -1,5 +1,5 @@
namespace PacketLib;
public class JoinPacket {
public string username { get; set; }
public string Username { get; set; }
}

View file

@ -1,13 +0,0 @@
using System.Xml;
using LiteNetLib.Utils;
namespace PacketLib;
public static class NetDataWriterExtensions {
public static GameEvent GetGameEvent(this NetDataReader reader) {
GameEvent gevent = new();
gevent.Deserialize(reader);
return gevent;
}
}

View file

@ -1,6 +1,6 @@
namespace PacketLib;
public class OpponentInitPacket {
public PlayerState state { get; set; }
public string username { get; set; }
public PlayerState State { get; set; }
public string Username { get; set; }
}

View file

@ -4,22 +4,21 @@ using LiteNetLib.Utils;
namespace PacketLib;
public struct PlayerCommand : INetSerializable {
public static PlayerCommand SWITCH_CAM(int id) => new(){ID = 0, Args = [id] };
public static PlayerCommand SET_MONITOR(bool state) => new(){ID = 1, Args = [state ? 1 : 0]};
public static PlayerCommand SET_DOOR_OFFICE(Direction direction, bool state) => new(){ID = 2, Args = [(int)direction, state ? 1 : 0]};
public static PlayerCommand SET_DOOR_REMOTE((int, int) remoteDoorId, bool state) => new(){ID = 3, Args = [remoteDoorId.Item1, remoteDoorId.Item2, state ? 1 : 0]};
public static PlayerCommand SET_LIGHT(int camId, bool state) => new(){ID = 4, Args = [camId, state ? 1 : 0]};
public int ID{ get; set; }
public bool Hideable => ID < 0;
public static PlayerCommand SWITCH_CAM(int id) => new(){Id = 0, Args = [id] };
public static PlayerCommand SET_MONITOR(bool state) => new(){Id = 1, Args = [state ? 1 : 0]};
public static PlayerCommand SET_DOOR_OFFICE(Direction direction, bool state) => new(){Id = 2, Args = [(int)direction, state ? 1 : 0]};
public static PlayerCommand SET_DOOR_REMOTE((int, int) remoteDoorId, bool state) => new(){Id = 3, Args = [remoteDoorId.Item1, remoteDoorId.Item2, state ? 1 : 0]};
public static PlayerCommand SET_LIGHT(int camId, bool state) => new(){Id = 4, Args = [camId, state ? 1 : 0]};
public int Id{ get; set; }
public int[] Args{ get; private set; }
public void Serialize(NetDataWriter writer) {
writer.Put(ID);
writer.Put(Id);
writer.PutArray(Args);
}
public void Deserialize(NetDataReader reader) {
ID = reader.GetInt();
Id = reader.GetInt();
Args = reader.GetIntArray();
}

View file

@ -1,5 +1,5 @@
namespace PacketLib;
public class PlayerCommandPacket {
public PlayerCommand[] commands { get; set; }
public PlayerCommand[] Commands { get; set; }
}

View file

@ -1,40 +1,39 @@
using LiteNetLib;
using LiteNetLib.Utils;
using LiteNetLib.Utils;
namespace PacketLib;
public struct PlayerState : INetSerializable { // TODO: make a constructor
public int pid;
public int camera;
public bool monitorUp;
public struct PlayerState(int pid, int camera, bool[] doorStates, int power, bool poweredOut) : INetSerializable {
public int Pid = pid;
public int Camera = camera;
public bool MonitorUp;
public int officeTileId;
public bool[] doorStates;
public int[] neighbouringTiles; // the indexes should correspond in both arrays
public int OfficeTileId;
public bool[] DoorStates = doorStates;
public int[] NeighbouringTiles = []; // the indexes should correspond in both arrays
public int Power = power;
public bool PoweredOut = poweredOut;
public int power;
public bool poweredOut;
public void Serialize(NetDataWriter writer) {
writer.Put(pid);
writer.Put(camera);
writer.Put(monitorUp);
writer.PutArray(doorStates);
writer.Put(officeTileId);
writer.PutArray(neighbouringTiles);
writer.Put(power);
writer.Put(poweredOut);
writer.Put(Pid);
writer.Put(Camera);
writer.Put(MonitorUp);
writer.PutArray(DoorStates);
writer.Put(OfficeTileId);
writer.PutArray(NeighbouringTiles);
writer.Put(Power);
writer.Put(PoweredOut);
}
public void Deserialize(NetDataReader reader) {
pid = reader.GetInt();
camera = reader.GetInt();
monitorUp = reader.GetBool();
doorStates = reader.GetBoolArray();
officeTileId = reader.GetInt();
neighbouringTiles = reader.GetIntArray();
power = reader.GetInt();
poweredOut = reader.GetBool();
Pid = reader.GetInt();
Camera = reader.GetInt();
MonitorUp = reader.GetBool();
DoorStates = reader.GetBoolArray();
OfficeTileId = reader.GetInt();
NeighbouringTiles = reader.GetIntArray();
Power = reader.GetInt();
PoweredOut = reader.GetBool();
}
}

View file

@ -1,11 +1,5 @@
namespace PacketLib;
public class UpdatePlayerPacket {
// public PlayerState stateP1{ get; set; }
// public PlayerState stateP2{ get; set; }
// TODO: implement anti-desync measures by comparing server and client states
public GameEvent[] events { get; set; }
// public EventQueue eventQueue { get; set; }
public GameEvent[] Events { get; set; }
}