Hlavní menu, synchronizace jmen hráčů. Client hru spustí až ve chvíli kdy dostane správný packet. Oprava bugu v se scalováním UIElementu

This commit is contained in:
Perry 2026-03-11 22:35:30 +01:00
parent e6128dc9f5
commit 7656707177
19 changed files with 315 additions and 53 deletions

View file

@ -2,6 +2,7 @@ using System;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using FNAF_Clone.GUI;
using FNAF_Clone.Map;
using GlobalClassLib;
@ -11,7 +12,21 @@ using PacketLib;
namespace FNAF_Clone;
public class Client {
public class Client {
public enum ConnectionState {
IDLE,
CONNECTING,
CONNECTED,
ACCEPTED,
GAME_STARTING,
GAME_IN_PROGRESS,
SERVER_NOT_FOUND,
SERVER_FULL,
ERROR
}
public static ConnectionState State { get; private set; } = ConnectionState.IDLE;
private static EventBasedNetListener listener = new();
private static NetManager client;
private static NetPeer server;
@ -24,6 +39,7 @@ public class Client {
public static ClientPlayer GetPlayer(int pid) => Player.state.pid == pid ? Player : Opponent;
public static void Connect(string endPoint, int port) {
State = ConnectionState.CONNECTING;
writer = new NetDataWriter();
processor = new NetPacketProcessor();
@ -32,7 +48,11 @@ public class Client {
processor.SubscribeReusable<JoinAcceptPacket>(OnJoinAccept);
processor.SubscribeReusable<UpdatePlayerPacket>(OnPlayerUpdate);
processor.SubscribeReusable<MapInitPacket>(OnMapInit);
processor.SubscribeReusable<OpponentInitPacket>(packet => Opponent.state = packet.state);
processor.SubscribeReusable<OpponentInitPacket>(packet => { // TODO: move this to a method
Opponent.state = packet.state;
Opponent.username = packet.username;
State = ConnectionState.GAME_STARTING;
});
client = new NetManager(listener){
AutoRecycle = true
@ -48,10 +68,11 @@ public class Client {
listener.PeerConnectedEvent += peer => {
Console.WriteLine("Connected to Server");
server = peer;
SendPacket(new JoinPacket {username = "Player1"}, DeliveryMethod.ReliableOrdered);
State = ConnectionState.CONNECTED;
SendPacket(new JoinPacket {username = Player.username == "" ? "Anonymous" : Player.username}, DeliveryMethod.ReliableOrdered);
};
client.Connect(endPoint, port, ""); // TODO: figure out how keys work
new Thread(() => client.Connect(endPoint, port, "")).Start(); // TODO: figure out how keys work
}
public static void SendPacket<T>(T packet, DeliveryMethod deliveryMethod) where T : class, new() {
@ -71,6 +92,7 @@ public class Client {
private static void OnJoinAccept(JoinAcceptPacket packet) {
Console.WriteLine($"Accepted by server, pid: {packet.state.pid}");
State = ConnectionState.ACCEPTED;
Player.state = packet.state;
}
@ -83,8 +105,7 @@ public class Client {
TileConnectorProjection[] connectors = connectorsData.Select(c => new TileConnectorProjection(ClientMapManager.Get(c.id1), ClientMapManager.Get(c.id2), c.type)).ToArray();
ClientMapManager.InitConnectors(connectors);
UIManager.InitUI();
UIManager.SpawnDoors(connectors.Where(c => c.Type == ConnectorType.DOOR_REMOTE).ToArray());
UIManager.SpawnMapElements(connectors.Where(c => c.Type == ConnectorType.DOOR_REMOTE).ToArray());
}