From 0daaaac0f908d760646c82a38fe80ee2db4525c6 Mon Sep 17 00:00:00 2001 From: Nikolai Rodionov Date: Wed, 19 Feb 2025 09:32:03 +0100 Subject: [PATCH] WIP: Some stuff --- godot/assets/icons/home.svg | 3 + godot/assets/icons/home.svg.import | 3 + godot/assets/icons/settings.svg | 3 + godot/assets/icons/settings.svg.import | 3 + godot/assets/icons/web-window-xmark.svg | 3 + .../assets/icons/web-window-xmark.svg.import | 3 + godot/examples/server_config/config.ini | 4 ++ godot/examples/server_config/map_list.txt | 1 + godot/project.godot | 6 ++ godot/src/entrypoint.gd | 28 +++++++-- godot/src/entrypoint.tscn | 19 +++---- godot/src/interfaces/main_menu/main_menu.gd | 10 +--- godot/src/interfaces/main_menu/main_menu.tscn | 57 ++++++++++++++++++- godot/src/server/server.gd | 27 +++++++++ godot/src/server/server.tscn | 6 ++ godot/src/utils/consts.gd | 4 ++ godot/src/utils/functions.gd | 54 ++++++++++++++++++ godot/src/utils/logger.gd | 54 ++++++++++++++++++ rust/src/entrypoint.rs | 6 +- 19 files changed, 268 insertions(+), 26 deletions(-) create mode 100644 godot/assets/icons/home.svg create mode 100644 godot/assets/icons/home.svg.import create mode 100644 godot/assets/icons/settings.svg create mode 100644 godot/assets/icons/settings.svg.import create mode 100644 godot/assets/icons/web-window-xmark.svg create mode 100644 godot/assets/icons/web-window-xmark.svg.import create mode 100644 godot/examples/server_config/config.ini create mode 100644 godot/examples/server_config/map_list.txt create mode 100644 godot/src/server/server.gd create mode 100644 godot/src/server/server.tscn create mode 100644 godot/src/utils/consts.gd create mode 100644 godot/src/utils/functions.gd create mode 100644 godot/src/utils/logger.gd diff --git a/godot/assets/icons/home.svg b/godot/assets/icons/home.svg new file mode 100644 index 0000000..55c439e --- /dev/null +++ b/godot/assets/icons/home.svg @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:aee5f49a5e43b19144b3f4ce1418152280923f572001f71b928fb7d1441bcee7 +size 520 diff --git a/godot/assets/icons/home.svg.import b/godot/assets/icons/home.svg.import new file mode 100644 index 0000000..df4b8a5 --- /dev/null +++ b/godot/assets/icons/home.svg.import @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:6743782ca33f3f3a2d100a54189191aedfed96299ddd0372f94d5b439f2d5c52 +size 856 diff --git a/godot/assets/icons/settings.svg b/godot/assets/icons/settings.svg new file mode 100644 index 0000000..ffa05bd --- /dev/null +++ b/godot/assets/icons/settings.svg @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:483fbd5ec717db88d93277115af80c54f0242664f01d36e7733f01f2172ed5e7 +size 880 diff --git a/godot/assets/icons/settings.svg.import b/godot/assets/icons/settings.svg.import new file mode 100644 index 0000000..4e99f6b --- /dev/null +++ b/godot/assets/icons/settings.svg.import @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:d6aee30676ea1d48e82eddf8e100ddbbd2d1c5c63024ba1c89c895fa55b326ef +size 868 diff --git a/godot/assets/icons/web-window-xmark.svg b/godot/assets/icons/web-window-xmark.svg new file mode 100644 index 0000000..60f3849 --- /dev/null +++ b/godot/assets/icons/web-window-xmark.svg @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:34bf6b6486e1a9a7f632795edd5865c8e7b765768af553f5a8df14045c615cdf +size 657 diff --git a/godot/assets/icons/web-window-xmark.svg.import b/godot/assets/icons/web-window-xmark.svg.import new file mode 100644 index 0000000..35f345c --- /dev/null +++ b/godot/assets/icons/web-window-xmark.svg.import @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:d04c8c2a0e8477a689d0ba3822dfb97005a3573aa298fec6a9dcf2727268bea2 +size 891 diff --git a/godot/examples/server_config/config.ini b/godot/examples/server_config/config.ini new file mode 100644 index 0000000..cfea29c --- /dev/null +++ b/godot/examples/server_config/config.ini @@ -0,0 +1,4 @@ +[main] +starting_map="br_lowpoly" +port=27015 +player_limit=10 diff --git a/godot/examples/server_config/map_list.txt b/godot/examples/server_config/map_list.txt new file mode 100644 index 0000000..e5372a0 --- /dev/null +++ b/godot/examples/server_config/map_list.txt @@ -0,0 +1 @@ +lowpoly_tdm diff --git a/godot/project.godot b/godot/project.godot index 440d163..b43090b 100644 --- a/godot/project.godot +++ b/godot/project.godot @@ -16,6 +16,12 @@ run/main_scene="res://src/entrypoint.tscn" config/features=PackedStringArray("4.3", "Forward Plus") config/icon="res://icon.svg" +[autoload] + +consts="*res://src/utils/consts.gd" +helpers="*res://src/utils/functions.gd" +logger="*res://src/utils/logger.gd" + [display] window/size/mode=4 diff --git a/godot/src/entrypoint.gd b/godot/src/entrypoint.gd index b5182e0..b46b8ee 100644 --- a/godot/src/entrypoint.gd +++ b/godot/src/entrypoint.gd @@ -1,14 +1,21 @@ -extends EntryPoint +class_name EntryPoint extends Node + +signal load_level(level: String) + +func _on_load_level(): + pass # -- A path to the config file for setting up the dedicated server var config_file_path: String = "" -@export var main_menu: MainMenu = null +@onready var main_menu: MainMenu = $MainMenu +@onready var server: Server = $Server # -- Called when the node enters the scene tree for the first time. func _ready() -> void: parse_args() if OS.has_feature("dedicated_server") or DisplayServer.get_name() == "headless": + main_menu.queue_free() var err := start_dedicated_server() if err != OK: push_error("Couldn't start the dedicated server, err: " + str(err)) @@ -16,6 +23,11 @@ func _ready() -> void: var err := start_game() if err != OK: push_error("Couldn't start the game, err: " + str(err)) + #load_level.connect(_on_load_level) + main_menu.start_game.connect(start_server) + +func start_server(): + server.start_server() # -- Parse command line arguments func parse_args() -> void: @@ -26,10 +38,16 @@ func parse_args() -> void: config_file_path = args[index + 1] func start_dedicated_server() -> Error: - push_error("Dedicated server is not yet implemented") - return ERR_METHOD_NOT_FOUND + # -- Build the server from the file + var err := server.read_config_file(config_file_path) + if err != OK: + return err + return OK # -- When not dedicated server, just start the full game and load menu func start_game() -> Error: - # -- Draw the main menu return OK + + +func _on_main_menu_start_game() -> void: + pass # Replace with function body. diff --git a/godot/src/entrypoint.tscn b/godot/src/entrypoint.tscn index 772c132..85aaf2a 100644 --- a/godot/src/entrypoint.tscn +++ b/godot/src/entrypoint.tscn @@ -1,18 +1,17 @@ -[gd_scene load_steps=3 format=3 uid="uid://0hsqnr1kunv5"] +[gd_scene load_steps=4 format=3 uid="uid://0hsqnr1kunv5"] [ext_resource type="Script" path="res://src/entrypoint.gd" id="1_ce80t"] -[ext_resource type="Script" path="res://src/interfaces/main_menu/main_menu.gd" id="2_nmo60"] +[ext_resource type="PackedScene" uid="uid://dt7rhpcor1wh7" path="res://src/interfaces/main_menu/main_menu.tscn" id="2_dd6gs"] +[ext_resource type="PackedScene" uid="uid://dkvbrav2sgs7m" path="res://src/server/server.tscn" id="3_uv83k"] -[node name="Entrypoint" type="EntryPoint" node_paths=PackedStringArray("main_menu")] +[node name="Entrypoint" type="EntryPoint"] script = ExtResource("1_ce80t") -main_menu = NodePath("MainMenu") [node name="LevelPlaceholder" type="Node3D" parent="."] editor_description = "This node should be used for storing the map that is currently loaded" -[node name="MainMenu" type="Control" parent="."] -layout_mode = 3 -anchors_preset = 0 -offset_right = 40.0 -offset_bottom = 40.0 -script = ExtResource("2_nmo60") +[node name="MainMenu" parent="." instance=ExtResource("2_dd6gs")] + +[node name="Server" parent="." instance=ExtResource("3_uv83k")] + +[connection signal="start_game" from="MainMenu" to="." method="_on_main_menu_start_game"] diff --git a/godot/src/interfaces/main_menu/main_menu.gd b/godot/src/interfaces/main_menu/main_menu.gd index d1168b8..b34c350 100644 --- a/godot/src/interfaces/main_menu/main_menu.gd +++ b/godot/src/interfaces/main_menu/main_menu.gd @@ -1,11 +1,7 @@ class_name MainMenu extends Control - -# Called when the node enters the scene tree for the first time. -func _ready() -> void: - pass # Replace with function body. +signal start_game() -# Called every frame. 'delta' is the elapsed time since the previous frame. -func _process(delta: float) -> void: - pass +func _on_create_server_pressed() -> void: + emit_signal("start_game") diff --git a/godot/src/interfaces/main_menu/main_menu.tscn b/godot/src/interfaces/main_menu/main_menu.tscn index bc3726a..c519d30 100644 --- a/godot/src/interfaces/main_menu/main_menu.tscn +++ b/godot/src/interfaces/main_menu/main_menu.tscn @@ -1,6 +1,9 @@ -[gd_scene load_steps=2 format=3 uid="uid://dt7rhpcor1wh7"] +[gd_scene load_steps=5 format=3 uid="uid://dt7rhpcor1wh7"] [ext_resource type="Script" path="res://src/interfaces/main_menu/main_menu.gd" id="1_dfg40"] +[ext_resource type="Texture2D" uid="uid://dyymlervrwv87" path="res://assets/icons/home.svg" id="2_q7q32"] +[ext_resource type="Texture2D" uid="uid://d0thk6bpmkkgo" path="res://assets/icons/settings.svg" id="3_7rsvq"] +[ext_resource type="Texture2D" uid="uid://7pnrehlqgw0v" path="res://assets/icons/web-window-xmark.svg" id="4_svtqg"] [node name="MainMenu" type="Control"] layout_mode = 3 @@ -9,4 +12,56 @@ anchor_right = 1.0 anchor_bottom = 1.0 grow_horizontal = 2 grow_vertical = 2 +scale = Vector2(1.5, 1.5) +size_flags_horizontal = 3 +size_flags_vertical = 3 script = ExtResource("1_dfg40") + +[node name="MainContainer" type="VBoxContainer" parent="."] +layout_mode = 1 +anchors_preset = 15 +anchor_right = 1.0 +anchor_bottom = 1.0 +grow_horizontal = 2 +grow_vertical = 2 + +[node name="MainButtonsRow" type="HBoxContainer" parent="MainContainer"] +layout_mode = 2 +size_flags_vertical = 0 + +[node name="SystemButtons" type="HBoxContainer" parent="MainContainer/MainButtonsRow"] +layout_mode = 2 + +[node name="Home" type="Button" parent="MainContainer/MainButtonsRow/SystemButtons"] +layout_mode = 2 +tooltip_text = "Show main menu" +icon = ExtResource("2_q7q32") + +[node name="Settings" type="Button" parent="MainContainer/MainButtonsRow/SystemButtons"] +layout_mode = 2 +tooltip_text = "Settings" +icon = ExtResource("3_7rsvq") + +[node name="Exit" type="Button" parent="MainContainer/MainButtonsRow/SystemButtons"] +layout_mode = 2 +tooltip_text = "Exit game" +icon = ExtResource("4_svtqg") + +[node name="MainButtons" type="HBoxContainer" parent="MainContainer/MainButtonsRow"] +layout_mode = 2 +size_flags_horizontal = 4 +size_flags_vertical = 4 + +[node name="Create Server" type="Button" parent="MainContainer/MainButtonsRow/MainButtons"] +layout_mode = 2 +text = "Create server" + +[node name="Join Server" type="Button" parent="MainContainer/MainButtonsRow/MainButtons"] +layout_mode = 2 +text = "Join Server" + +[node name="PlayContaner" type="HBoxContainer" parent="MainContainer"] +layout_mode = 2 +size_flags_vertical = 3 + +[connection signal="pressed" from="MainContainer/MainButtonsRow/MainButtons/Create Server" to="." method="_on_create_server_pressed"] diff --git a/godot/src/server/server.gd b/godot/src/server/server.gd new file mode 100644 index 0000000..03aa96e --- /dev/null +++ b/godot/src/server/server.gd @@ -0,0 +1,27 @@ +class_name Server extends Node + +# -- TODO: Server must have a list of maps instead of just one +# var map_list_file_path: String = "" + +@export var staring_map: String = "" +var current_map_hash: String = "" + +var port: int = 27015 + +@export var players: Dictionary = {} + +func read_config_file(path: String) -> Error : + var err : Error + var config := ConfigFile.new() + err = config.load("user://scores.cfg") + if err != OK: + return err + staring_map = config.get_value("main", "staring_map", "br_lowpoly") + port = int(config.get_value("main", "port", "27015")) + + return OK + + +func start_server() -> Error : + print("Starting") + return OK diff --git a/godot/src/server/server.tscn b/godot/src/server/server.tscn new file mode 100644 index 0000000..fba8172 --- /dev/null +++ b/godot/src/server/server.tscn @@ -0,0 +1,6 @@ +[gd_scene load_steps=2 format=3 uid="uid://dkvbrav2sgs7m"] + +[ext_resource type="Script" path="res://src/server/server.gd" id="1_53ixn"] + +[node name="Server" type="Node"] +script = ExtResource("1_53ixn") diff --git a/godot/src/utils/consts.gd b/godot/src/utils/consts.gd new file mode 100644 index 0000000..943c999 --- /dev/null +++ b/godot/src/utils/consts.gd @@ -0,0 +1,4 @@ +extends Node + +const DEFAULT_JUMP_VELOCITY: float = 5 +const DEFAULT_CHARACTER_SPEED: float = 7.0 diff --git a/godot/src/utils/functions.gd b/godot/src/utils/functions.gd new file mode 100644 index 0000000..894da36 --- /dev/null +++ b/godot/src/utils/functions.gd @@ -0,0 +1,54 @@ +class_name Functions extends Node + + +func get_root_node() -> EntryPoint: + return get_tree().get_root().find_child("Entrypoint", true, false) + + +#func get_server_node() -> ServerData: + #return get_tree().get_root().find_child("ServerData", true, false) +# +# +#func get_map_node() -> MapController: + #return get_tree().get_root().find_child("Map", true, false) + + +#func player_data_into_dict(player_data: PlayerData) -> Dictionary: + #var result: Dictionary = { + #"id": player_data.id, + #"username": player_data.id, + #"score": player_data.score, + #"damage": player_data.damage, + #"headshots": player_data.headshots, + #"active": player_data.active + #} + #var side: String + #match player_data.side: + #-1: + #side = PlayerData.underfined + #0: + #side = PlayerData.blue + #1: + #side = PlayerData.red + #result["side"] = side + #return result +# +# +#func player_data_from_dict(player_data: Dictionary) -> PlayerData: + #var result := PlayerData.new() + #result.id = player_data.get("id") + #result.username = player_data.get("username") + #result.active = player_data.get("active") + #result.damage = player_data.get("damage") + #result.headshots = player_data.get("headshots") + #result.score = player_data.get("score") + #var side: int + #match player_data.side: + #"undefined": + #side = -1 + #"attack": + #side = 0 + #"defend": + #side = 1 + #result.side = side + #return result diff --git a/godot/src/utils/logger.gd b/godot/src/utils/logger.gd new file mode 100644 index 0000000..9789618 --- /dev/null +++ b/godot/src/utils/logger.gd @@ -0,0 +1,54 @@ +extends Node +class_name Logger + +var thread: Thread = Thread.new() +var mutex: Mutex = Mutex.new() +var log_queue: Array[String] = [] +var running: bool = true + + +func _ready(): + thread.start(log_writer) + + +func info(msg: Variant): + mutex.lock() + log_queue.append("[color=white][b]INFO:[/b] [/color]" + msg) + mutex.unlock() + + +func debug(msg: Variant): + mutex.lock() + log_queue.append("[color=cyan][b]DEBUG:[/b] [/color]" + msg) + mutex.unlock() + + +func warning(msg: Variant): + mutex.lock() + log_queue.append("[color=yellow][b]WARN:[/b] [/color]" + msg) + push_warning(msg) + mutex.unlock() + + +func error(msg: Variant): + mutex.lock() + log_queue.append("[color=red][b]ERROR:[/b] [/color]" + msg) + push_error(msg) + mutex.unlock() + + +func log_writer(): + while running: + mutex.lock() + while log_queue.size() > 0: + var log_message = log_queue.pop_front() + print_rich(log_message) + mutex.unlock() + + # Prevent high CPU usage + await get_tree().create_timer(0.1).timeout + + +func _exit_tree(): + running = false + thread.wait_to_finish() diff --git a/rust/src/entrypoint.rs b/rust/src/entrypoint.rs index 0df8f49..6321901 100644 --- a/rust/src/entrypoint.rs +++ b/rust/src/entrypoint.rs @@ -6,12 +6,12 @@ use std; #[class(base=Node)] // EntryPoint should decide whether the game should be launched in the // server or the playing mode and load corresponding resources -struct EntryPoint { +struct EntryPointRs { base: Base, } #[godot_api] -impl INode for EntryPoint { +impl INode for EntryPointRs { fn init(base: Base) -> Self { Self { base } } @@ -20,4 +20,4 @@ impl INode for EntryPoint { } #[godot_api] -impl EntryPoint {} +impl EntryPointRs {}