From 46353fc8f6133eac1d4f4b79e83826cee7d0358d Mon Sep 17 00:00:00 2001 From: leca Date: Mon, 12 Feb 2024 00:11:50 +0300 Subject: [PATCH] Embeeded configs of the game to server, made client ask it --- project.godot | 2 ++ scripts/Anticheat.gd | 1 + scripts/ConnectButton.gd | 2 +- scripts/GameData.gd | 24 ++++++++++++++++++++++-- scripts/Networking.gd | 15 +++++++++++++-- scripts/Player.gd | 32 +++++++++++++++++++++++--------- settings/server-settings.json | 8 ++++++++ 7 files changed, 70 insertions(+), 14 deletions(-) create mode 100644 scripts/Anticheat.gd diff --git a/project.godot b/project.godot index ff08086..43eb3d1 100644 --- a/project.godot +++ b/project.godot @@ -13,12 +13,14 @@ config_version=5 config/name="Cs-os" run/main_scene="res://scenes/menu.tscn" config/features=PackedStringArray("4.2", "Forward Plus") +boot_splash/bg_color=Color(0.282353, 0.00784314, 0.0745098, 1) config/icon="res://icon.svg" [autoload] Networking="*res://scripts/Networking.gd" GameData="*res://scripts/GameData.gd" +Anticheat="*res://scripts/Anticheat.gd" [input] diff --git a/scripts/Anticheat.gd b/scripts/Anticheat.gd new file mode 100644 index 0000000..61510e1 --- /dev/null +++ b/scripts/Anticheat.gd @@ -0,0 +1 @@ +extends Node diff --git a/scripts/ConnectButton.gd b/scripts/ConnectButton.gd index d5dee4b..214e370 100644 --- a/scripts/ConnectButton.gd +++ b/scripts/ConnectButton.gd @@ -5,7 +5,7 @@ extends Button @onready var port_input: TextEdit = $"../Port" func _ready(): - button.text = "Click me" + button.text = "Connect" button.pressed.connect(self._button_pressed) func _button_pressed(): diff --git a/scripts/GameData.gd b/scripts/GameData.gd index 21a8f33..fc83e77 100644 --- a/scripts/GameData.gd +++ b/scripts/GameData.gd @@ -4,8 +4,28 @@ var client_settings var server_settings func _ready(): - var client_settings_file = FileAccess.open("res://settings/client-settings.json", FileAccess.READ) + var user_dir = DirAccess.open(".") + if(!user_dir.dir_exists("./settings")): + user_dir.make_dir("settings") + var client_settings_file + + if(!FileAccess.file_exists("./settings/client-settings.json")): + print("Client-settings doesn't exist, creating one") + var example = FileAccess.open("res://settings/client-settings.json", FileAccess.READ) + client_settings_file = FileAccess.open("./settings/client-settings.json", FileAccess.WRITE_READ) + client_settings_file.store_buffer(example.get_as_text()) + else: + client_settings_file = FileAccess.open("./settings/client-settings.json", FileAccess.READ) client_settings = JSON.parse_string(client_settings_file.get_as_text()) - var server_settings_file = FileAccess.open("res://settings/server-settings.json", FileAccess.READ) + + + var server_settings_file + if(!FileAccess.file_exists("./settings/server-settings.json")): + print("Server-settings doesn't exist, creating one") + var example = FileAccess.open("res://settings/server-settings.json", FileAccess.READ) + server_settings_file = FileAccess.open("./settings/server-settings.json", FileAccess.WRITE_READ) + server_settings_file.store_string(example.get_as_text()) + else: + server_settings_file = FileAccess.open("./settings/server-settings.json", FileAccess.READ) server_settings = JSON.parse_string(server_settings_file.get_as_text()) diff --git a/scripts/Networking.gd b/scripts/Networking.gd index e928d31..f3a5391 100644 --- a/scripts/Networking.gd +++ b/scripts/Networking.gd @@ -1,6 +1,7 @@ extends Node -const player_script := preload("res://scripts/Player.gd") +#var player_script := load("res://scripts/Player.gd") +var player_script := preload("res://scripts/Player.gd") var player_model = preload("res://models/player.tscn") var peer = ENetMultiplayerPeer.new() @@ -76,6 +77,9 @@ func move_client(client_id, position): var internal_id = client["internal_id"] client["position"] = position send_everyone_except(client_id, [get_node("player" + str(internal_id)).find_child("CharacterBody3D").move_puppet, position, internal_id]) + #send_everyone_except(client_id, [get_node("player" + str(internal_id)).find_child("CharacterBody3D").move_puppet, position, internal_id]) + #send_everyone_except(client_id, ["move_puppet", position, internal_id]) + #send_everyone_except(client_id, [player_script.move_puppet, position, internal_id]) @rpc ("any_peer", "call_remote", "reliable") func get_client_list(client_id): @@ -83,6 +87,11 @@ func get_client_list(client_id): if (current_client_id == str(client_id)): continue rpc_id(client_id, "spawn_puppet", clients[str(current_client_id)]["internal_id"]) +@rpc ("any_peer", "call_remote", "reliable") +func get_server_settings(client_id): + var client = clients[str(client_id)] + var internal_id = client["internal_id"] + get_node("player" + str(internal_id)).find_child("CharacterBody3D").set_game_settings.rpc_id(int(client_id), settings["game"]) ##########################################CLIENT####################### var player @@ -124,9 +133,11 @@ func _Connection_Succseeded(): rpc_id(1, "get_character_properties", multiplayer.get_unique_id()) rpc_id(1, "get_client_list", multiplayer.get_unique_id()) - await get_tree().create_timer(0.2).timeout # костыль get_tree().change_scene_to_file("res://scenes/test_map.tscn") + await get_tree().create_timer(0.2).timeout # костыль + + rpc_id(1, "get_server_settings", multiplayer.get_unique_id()) var player_node = player_model.instantiate() player_node.find_child("CharacterBody3D").set_is_playable(true) diff --git a/scripts/Player.gd b/scripts/Player.gd index 4f03a46..4a9b67e 100644 --- a/scripts/Player.gd +++ b/scripts/Player.gd @@ -1,8 +1,5 @@ extends CharacterBody3D -const WALK_VELOCITY = 12.0 -const RUN_VELOCITY = 24.0 -const JUMP_VELOCITY = 12.0 const SENSETIVITY = 0.009; enum Class_type { @@ -28,8 +25,14 @@ var properties = { internal_id = 0 } -var speed = WALK_VELOCITY -var gravity = 9.8 +var speed = 0 +var walk = 0 +var run = 0 +var jump = 0 +var gravity = 0 +var game_settings +var client_settings + @onready var head = $Head @onready var camera = $Head/Camera @onready var playerCharacterBody = $"." @@ -59,7 +62,8 @@ func _unhandled_input(event): camera.rotation.x = clamp(camera.rotation.x, deg_to_rad(-50), deg_to_rad(60)) func _physics_process(delta): - if (!properties["is_playable"]): return + if (!properties["is_playable"]): return + if (game_settings == null): return if not is_on_floor(): velocity.y -= 4 * gravity * delta @@ -68,11 +72,11 @@ func _physics_process(delta): if is_on_floor(): if Input.is_action_just_pressed("jump"): - velocity.y = JUMP_VELOCITY + velocity.y = jump if Input.is_action_pressed("run"): - speed = RUN_VELOCITY + speed = run else: - speed = WALK_VELOCITY + speed = walk if direction: velocity.x = direction.x * speed velocity.z = direction.z * speed @@ -91,3 +95,13 @@ func _physics_process(delta): func move_puppet(p: Vector3, i_id): if (i_id == properties["internal_id"]): playerCharacterBody.position = p + +@rpc ("authority", "call_remote", "reliable") +func set_game_settings(s): + if(!properties["is_playable"]): return + print("Got settings from server: " + str(s)) + game_settings = s + gravity = game_settings["moving"]["gravity"] + walk = game_settings["moving"]["walk"] + run = game_settings["moving"]["run"] + jump = game_settings["moving"]["jump"] diff --git a/settings/server-settings.json b/settings/server-settings.json index f94d757..49fb74d 100644 --- a/settings/server-settings.json +++ b/settings/server-settings.json @@ -2,4 +2,12 @@ "port": 2390, "maxclients": 10, "enableAntiCheat": false, + "game": { + "moving": { + "walk": 12, + "run": 24, + "gravity": 2, + "jump": 12 + } + } }