From 1f6c128893e1f7eee2250ec408468aa2d2a8897a Mon Sep 17 00:00:00 2001 From: Eduard Urbach Date: Mon, 22 Jan 2024 17:12:26 +0100 Subject: [PATCH] Minor improvements --- network/server/Connection.gd | 1 + network/server/Server.gd | 20 ++++++++++++-------- player/Player.gd | 5 +++++ player/PlayerController.gd | 2 ++ 4 files changed, 20 insertions(+), 8 deletions(-) diff --git a/network/server/Connection.gd b/network/server/Connection.gd index 1943be6..110b17d 100644 --- a/network/server/Connection.gd +++ b/network/server/Connection.gd @@ -1,4 +1,5 @@ class_name Connection var peer: PacketPeerUDP +var address: String var last_packet: int \ No newline at end of file diff --git a/network/server/Server.gd b/network/server/Server.gd index 2194cc9..3c85db9 100644 --- a/network/server/Server.gd +++ b/network/server/Server.gd @@ -24,6 +24,7 @@ func _process(_delta): server.poll() now = Time.get_ticks_msec() + # Accept new connections while server.is_connection_available(): var peer: PacketPeerUDP = server.take_connection() var address := "%s:%d" % [peer.get_packet_ip(), peer.get_packet_port()] @@ -31,11 +32,13 @@ func _process(_delta): if !clients.has(address): var connection = Connection.new() connection.peer = peer + connection.address = address clients[address] = connection - peer_connected(address) - + peer_connected(connection) + clients[address].last_packet = now - + + # Process packets from clients for key in clients: var client = clients[key] var peer = client.peer @@ -46,6 +49,7 @@ func _process(_delta): client.last_packet = now packet_count += 1 + # Show statistics if now > last_statistics + 1000: for address in clients.keys(): var last_packet_time = clients[address].last_packet @@ -53,13 +57,13 @@ func _process(_delta): if now - last_packet_time > timeout: peer_disconnected(address) clients.erase(address) - + print("[Server] %d clients | %d packets" % [clients.size(), packet_count]) packet_count = 0 last_statistics = Time.get_ticks_msec() -func peer_connected(_address: String): - pass +func peer_connected(c: Connection): + print("[Server] Connected: ", c.address) -func peer_disconnected(address: String): - print("TIMEOUT ", address) +func peer_disconnected(c: Connection): + print("[Server] Disconnected: ", c.address) diff --git a/player/Player.gd b/player/Player.gd index faa7db2..bf3bea5 100644 --- a/player/Player.gd +++ b/player/Player.gd @@ -1,2 +1,7 @@ class_name Player extends Character + +func _ready(): + var controller := PlayerController.new() + controller.character = self + add_child(controller) \ No newline at end of file diff --git a/player/PlayerController.gd b/player/PlayerController.gd index e60c44d..8e52eb1 100644 --- a/player/PlayerController.gd +++ b/player/PlayerController.gd @@ -1,7 +1,9 @@ class_name PlayerController extends Node +## The character that we're controlling. @export var character: Character + var move: Vector2 func _input(_event):