From b7e7b8abd37cd320a769a45fb395de232a9072be Mon Sep 17 00:00:00 2001 From: Eduard Urbach Date: Thu, 25 Jan 2024 12:10:29 +0100 Subject: [PATCH] Improved UI --- client/network/Client.gd | 15 ++++--- client/network/Login.gd | 1 - client/network/Ping.gd | 15 +++---- client/network/PlayerState.gd | 1 - client/network/shared/Packet.gd | 9 ---- client/network/shared/PacketHandler.gd | 11 +++++ client/ui/UI.gd | 6 +++ client/ui/UI.tscn | 49 +++++++++++++++++++++ client/ui/debug/PingLabel.gd | 2 +- client/world/Client.tscn | 60 ++++++-------------------- stresstest/main.go | 7 +-- 11 files changed, 98 insertions(+), 78 deletions(-) delete mode 100644 client/network/shared/Packet.gd create mode 100644 client/ui/UI.gd create mode 100644 client/ui/UI.tscn diff --git a/client/network/Client.gd b/client/network/Client.gd index 3bc1587..5dd4c2d 100644 --- a/client/network/Client.gd +++ b/client/network/Client.gd @@ -1,13 +1,18 @@ extends NetworkNode -@export var ip := "127.0.0.1" -@export var port := 4242 +@export var host: String +@export var port: int var socket := PacketPeerUDP.new() -func _init(): - super._init() - socket.connect_to_host(ip, port) +func _enter_tree(): + socket.connect_to_host(host, port) + + for child in get_children(): + if !(child is PacketHandler): + continue + + set_handler(child.packet_type, child) func _process(_delta): while socket.get_available_packet_count() > 0: diff --git a/client/network/Login.gd b/client/network/Login.gd index 142e0aa..f06c5c8 100644 --- a/client/network/Login.gd +++ b/client/network/Login.gd @@ -3,7 +3,6 @@ extends PacketHandler var auth_token: String func _ready(): - %Client.set_handler(Packet.LOGIN, self) send_login() func handle_packet(data: PackedByteArray, _peer: PacketPeer): diff --git a/client/network/Ping.gd b/client/network/Ping.gd index c6e3230..f75a7d8 100644 --- a/client/network/Ping.gd +++ b/client/network/Ping.gd @@ -10,23 +10,22 @@ var history: Array[float] = [] func _init(): history.resize(HISTORY_SIZE) -func _ready(): - %Client.set_handler(Packet.PING, self) +func handle_packet(data: PackedByteArray, _peer: PacketPeer): + var id := data[0] + var ping := get_time() - history[id] + changed.emit(ping) func send_ping(): var buffer := StreamPeerBuffer.new() buffer.put_8(Packet.PING) buffer.put_8(count) %Client.socket.put_packet(buffer.data_array) - - history[count] = Time.get_unix_time_from_system() + history[count] = get_time() count += 1 if count >= HISTORY_SIZE: count = 0 -func handle_packet(data: PackedByteArray, _peer: PacketPeer): - var id := data[0] - var ping := Time.get_unix_time_from_system() - history[id] - changed.emit(ping) +func get_time() -> float: + return Time.get_unix_time_from_system() diff --git a/client/network/PlayerState.gd b/client/network/PlayerState.gd index 3d6c363..4a707a5 100644 --- a/client/network/PlayerState.gd +++ b/client/network/PlayerState.gd @@ -4,7 +4,6 @@ extends PacketHandler func _ready(): assert(player_scene) - %Client.set_handler(Packet.PLAYER_STATE, self) func handle_packet(data: PackedByteArray, _peer: PacketPeer): var player_name := data.get_string_from_utf8() diff --git a/client/network/shared/Packet.gd b/client/network/shared/Packet.gd deleted file mode 100644 index 004a4ee..0000000 --- a/client/network/shared/Packet.gd +++ /dev/null @@ -1,9 +0,0 @@ -class_name Packet - -enum { - PING = 1, - LOGIN = 2, - LOGOUT = 3, - PLAYER_STATE = 10, - PLAYER_MOVE = 11, -} diff --git a/client/network/shared/PacketHandler.gd b/client/network/shared/PacketHandler.gd index aab25ea..95bb5c1 100644 --- a/client/network/shared/PacketHandler.gd +++ b/client/network/shared/PacketHandler.gd @@ -1,5 +1,16 @@ class_name PacketHandler extends Node +enum Packet { + INVALID = 0, + PING = 1, + LOGIN = 2, + LOGOUT = 3, + PLAYER_STATE = 10, + PLAYER_MOVE = 11, +} + +@export var packet_type: Packet + func handle_packet(_data: PackedByteArray, _peer: PacketPeer): pass \ No newline at end of file diff --git a/client/ui/UI.gd b/client/ui/UI.gd new file mode 100644 index 0000000..6521bef --- /dev/null +++ b/client/ui/UI.gd @@ -0,0 +1,6 @@ +extends Control + +var on_ping_changed: Signal + +func _enter_tree(): + on_ping_changed = %Ping.changed \ No newline at end of file diff --git a/client/ui/UI.tscn b/client/ui/UI.tscn new file mode 100644 index 0000000..70ea3d9 --- /dev/null +++ b/client/ui/UI.tscn @@ -0,0 +1,49 @@ +[gd_scene load_steps=7 format=3 uid="uid://dagn5bf7ou3sd"] + +[ext_resource type="PackedScene" uid="uid://cch67vqpsmtej" path="res://ui/debug/DebugLabel.tscn" id="1_7s8uu"] +[ext_resource type="Script" path="res://ui/UI.gd" id="1_l5b6o"] +[ext_resource type="Script" path="res://ui/debug/FPSLabel.gd" id="2_i200p"] +[ext_resource type="Script" path="res://ui/debug/PingLabel.gd" id="3_xjdws"] +[ext_resource type="Script" path="res://ui/debug/PositionLabel.gd" id="4_beqf6"] +[ext_resource type="Script" path="res://ui/debug/VelocityLabel.gd" id="5_8lm6a"] + +[node name="UI" type="Control"] +layout_mode = 3 +anchors_preset = 0 +script = ExtResource("1_l5b6o") + +[node name="CanvasLayer" type="CanvasLayer" parent="."] + +[node name="TopLeftMargin" type="MarginContainer" parent="CanvasLayer"] +offset_right = 40.0 +offset_bottom = 50.0 + +[node name="VBoxContainer" type="VBoxContainer" parent="CanvasLayer/TopLeftMargin"] +layout_mode = 2 + +[node name="FPS" parent="CanvasLayer/TopLeftMargin/VBoxContainer" instance=ExtResource("1_7s8uu")] +layout_mode = 2 +script = ExtResource("2_i200p") + +[node name="Ping" parent="CanvasLayer/TopLeftMargin/VBoxContainer" instance=ExtResource("1_7s8uu")] +layout_mode = 2 +script = ExtResource("3_xjdws") + +[node name="BottomLeftMargin" type="MarginContainer" parent="CanvasLayer"] +anchors_preset = 2 +anchor_top = 1.0 +anchor_bottom = 1.0 +offset_top = -10.0 +offset_right = 10.0 +grow_vertical = 0 + +[node name="VBoxContainer" type="VBoxContainer" parent="CanvasLayer/BottomLeftMargin"] +layout_mode = 2 + +[node name="Position" parent="CanvasLayer/BottomLeftMargin/VBoxContainer" instance=ExtResource("1_7s8uu")] +layout_mode = 2 +script = ExtResource("4_beqf6") + +[node name="Velocity" parent="CanvasLayer/BottomLeftMargin/VBoxContainer" instance=ExtResource("1_7s8uu")] +layout_mode = 2 +script = ExtResource("5_8lm6a") diff --git a/client/ui/debug/PingLabel.gd b/client/ui/debug/PingLabel.gd index 3e735a9..d0a2c71 100644 --- a/client/ui/debug/PingLabel.gd +++ b/client/ui/debug/PingLabel.gd @@ -1,7 +1,7 @@ extends DebugLabel func _ready(): - %Ping.connect("changed", on_ping_changed) + owner.on_ping_changed.connect(on_ping_changed) func on_ping_changed(ping): text = str(snapped(ping * 1000, 1)) + " ms" diff --git a/client/world/Client.tscn b/client/world/Client.tscn index a58d00c..db94b95 100644 --- a/client/world/Client.tscn +++ b/client/world/Client.tscn @@ -1,4 +1,4 @@ -[gd_scene load_steps=21 format=3 uid="uid://b40y7iuskv1ar"] +[gd_scene load_steps=17 format=3 uid="uid://b40y7iuskv1ar"] [ext_resource type="Script" path="res://Game.gd" id="1_pux6q"] [ext_resource type="Script" path="res://network/Client.gd" id="2_1ofik"] @@ -7,16 +7,12 @@ [ext_resource type="PackedScene" uid="uid://2lcnu3dy54lx" path="res://player/Player.tscn" id="5_22pku"] [ext_resource type="Script" path="res://network/PlayerState.gd" id="5_wttxq"] [ext_resource type="Environment" uid="uid://dixa0yso2s1u3" path="res://world/Environment.tres" id="8_5uta8"] -[ext_resource type="PackedScene" uid="uid://cch67vqpsmtej" path="res://ui/debug/DebugLabel.tscn" id="8_jr2jn"] -[ext_resource type="Script" path="res://ui/debug/FPSLabel.gd" id="9_0imyg"] [ext_resource type="CameraAttributesPractical" uid="uid://b835orxyqq6w5" path="res://camera/CameraAttributes.tres" id="9_w4cdu"] [ext_resource type="PackedScene" uid="uid://tgmbtt7u172g" path="res://world/Arena.blend" id="10_cje7b"] [ext_resource type="Script" path="res://world/Sun.gd" id="10_mlmor"] -[ext_resource type="Script" path="res://ui/debug/PingLabel.gd" id="10_pu6sd"] -[ext_resource type="Script" path="res://ui/debug/PositionLabel.gd" id="11_k0017"] [ext_resource type="PackedScene" uid="uid://hnn0n1xc2qt7" path="res://world/Tree.blend" id="11_wlyv1"] [ext_resource type="PackedScene" uid="uid://cb2t7bvvf3gwh" path="res://enemy/slime/Slime.tscn" id="12_6yrwn"] -[ext_resource type="Script" path="res://ui/debug/VelocityLabel.gd" id="12_dgr41"] +[ext_resource type="PackedScene" uid="uid://dagn5bf7ou3sd" path="res://ui/UI.tscn" id="13_s76b0"] [ext_resource type="Script" path="res://camera/Camera.gd" id="13_y4waa"] [ext_resource type="Material" uid="uid://ddy5gkw0k16dq" path="res://shader/OutlineMaterial.tres" id="14_hobco"] @@ -31,16 +27,20 @@ script = ExtResource("1_pux6q") [node name="Client" type="Node" parent="."] unique_name_in_owner = true script = ExtResource("2_1ofik") +host = "127.0.0.1" +port = 4242 [node name="Ping" type="Node" parent="Client"] unique_name_in_owner = true script = ExtResource("3_4h5la") +packet_type = 1 [node name="Timer" type="Timer" parent="Client/Ping"] autostart = true [node name="Login" type="Node" parent="Client"] script = ExtResource("4_k8n1i") +packet_type = 2 [node name="Timer" type="Timer" parent="Client/Login"] wait_time = 5.0 @@ -49,9 +49,7 @@ autostart = true [node name="PlayerState" type="Node" parent="Client"] script = ExtResource("5_wttxq") player_scene = ExtResource("5_22pku") - -[node name="Players" type="Node3D" parent="."] -unique_name_in_owner = true +packet_type = 10 [node name="World" type="Node3D" parent="."] @@ -86,45 +84,10 @@ transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 7.00829, 1.28057, -1.95247) [node name="Slime3" parent="World/Enemies" instance=ExtResource("12_6yrwn")] transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 8.53558, 1.28057, -0.306177) -[node name="UI" type="Control" parent="."] -layout_mode = 3 -anchors_preset = 0 +[node name="Players" type="Node3D" parent="World"] +unique_name_in_owner = true -[node name="CanvasLayer" type="CanvasLayer" parent="UI"] - -[node name="TopLeftMargin" type="MarginContainer" parent="UI/CanvasLayer"] -offset_right = 40.0 -offset_bottom = 50.0 - -[node name="VBoxContainer" type="VBoxContainer" parent="UI/CanvasLayer/TopLeftMargin"] -layout_mode = 2 - -[node name="FPS" parent="UI/CanvasLayer/TopLeftMargin/VBoxContainer" instance=ExtResource("8_jr2jn")] -layout_mode = 2 -script = ExtResource("9_0imyg") - -[node name="Ping" parent="UI/CanvasLayer/TopLeftMargin/VBoxContainer" instance=ExtResource("8_jr2jn")] -layout_mode = 2 -script = ExtResource("10_pu6sd") - -[node name="BottomLeftMargin" type="MarginContainer" parent="UI/CanvasLayer"] -anchors_preset = 2 -anchor_top = 1.0 -anchor_bottom = 1.0 -offset_top = -10.0 -offset_right = 10.0 -grow_vertical = 0 - -[node name="VBoxContainer" type="VBoxContainer" parent="UI/CanvasLayer/BottomLeftMargin"] -layout_mode = 2 - -[node name="Position" parent="UI/CanvasLayer/BottomLeftMargin/VBoxContainer" instance=ExtResource("8_jr2jn")] -layout_mode = 2 -script = ExtResource("11_k0017") - -[node name="Velocity" parent="UI/CanvasLayer/BottomLeftMargin/VBoxContainer" instance=ExtResource("8_jr2jn")] -layout_mode = 2 -script = ExtResource("12_dgr41") +[node name="UI" parent="." instance=ExtResource("13_s76b0")] [node name="Viewport" type="SubViewportContainer" parent="."] texture_filter = 1 @@ -134,10 +97,11 @@ anchor_bottom = 1.0 grow_horizontal = 2 grow_vertical = 2 stretch = true +stretch_shrink = 3 [node name="SubViewport" type="SubViewport" parent="Viewport"] handle_input_locally = false -size = Vector2i(1152, 648) +size = Vector2i(384, 216) render_target_update_mode = 4 [node name="CameraPivot" type="Node3D" parent="Viewport/SubViewport"] diff --git a/stresstest/main.go b/stresstest/main.go index da932c4..b2041f0 100644 --- a/stresstest/main.go +++ b/stresstest/main.go @@ -8,11 +8,8 @@ import ( "time" ) -const ( - serverAddress = "127.0.0.1:4242" -) - var ( + address = flag.String("a", "127.0.0.1:4242", "address (host and port)") numClients = flag.Int("c", 10, "number of clients") sleepTime = flag.Duration("s", time.Second, "sleep time for each client") message = []byte{1, 0} @@ -25,7 +22,7 @@ func init() { func udpClient(wg *sync.WaitGroup) { defer wg.Done() - clientAddr, err := net.ResolveUDPAddr("udp", serverAddress) + clientAddr, err := net.ResolveUDPAddr("udp", *address) if err != nil { fmt.Println("Error resolving UDP address:", err)