Improved UI

This commit is contained in:
Eduard Urbach 2024-01-25 12:10:29 +01:00
parent 1d0521c0bb
commit b7e7b8abd3
Signed by: akyoto
GPG Key ID: C874F672B1AF20C0
11 changed files with 98 additions and 78 deletions

View File

@ -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:

View File

@ -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):

View File

@ -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()

View File

@ -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()

View File

@ -1,9 +0,0 @@
class_name Packet
enum {
PING = 1,
LOGIN = 2,
LOGOUT = 3,
PLAYER_STATE = 10,
PLAYER_MOVE = 11,
}

View File

@ -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

6
client/ui/UI.gd Normal file
View File

@ -0,0 +1,6 @@
extends Control
var on_ping_changed: Signal
func _enter_tree():
on_ping_changed = %Ping.changed

49
client/ui/UI.tscn Normal file
View File

@ -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")

View File

@ -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"

View File

@ -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"]

View File

@ -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)