diff --git a/client/Global.gd b/client/Global.gd index 90fd911..5f48619 100644 --- a/client/Global.gd +++ b/client/Global.gd @@ -2,6 +2,7 @@ extends Node var camera: Camera3D var player: Player +var players: PlayerManager var instance_id := OS.get_process_id() % 4 var username := "user%d" % instance_id var account_id: String diff --git a/client/network/PlayerAdd.gd b/client/network/PlayerAdd.gd index 02c5655..8da8e79 100644 --- a/client/network/PlayerAdd.gd +++ b/client/network/PlayerAdd.gd @@ -21,9 +21,9 @@ func handle_packet(data: PackedByteArray, _peer: PacketPeer): print("[%s] Add player: %s %s @ %v" % [Global.username, player_id, player_name, server_position]) - var player := spawn_player() + var player := spawn_player(player_id) player.position = server_position - player.set_character_name(player_id) + player.set_character_name(player_name) if player_id == Global.account_id: Global.player = player @@ -35,7 +35,8 @@ func handle_packet(data: PackedByteArray, _peer: PacketPeer): player.controller.character = player player.add_child(player.controller) -func spawn_player() -> Player: +func spawn_player(id: String) -> Player: var player = player_scene.instantiate() - %Players.add_child(player) + player.id = id + %Players.add(player) return player diff --git a/client/network/PlayerJump.gd b/client/network/PlayerJump.gd index b5bb247..c1448f7 100644 --- a/client/network/PlayerJump.gd +++ b/client/network/PlayerJump.gd @@ -13,5 +13,5 @@ func on_jump(): func handle_packet(data: PackedByteArray, _peer: PacketPeer): var player_id := data.get_string_from_ascii() - var player := %Players.get_node(player_id) + var player := Global.players.get_player(player_id) player.jump() diff --git a/client/network/PlayerMove.gd b/client/network/PlayerMove.gd index 2975c74..b404041 100644 --- a/client/network/PlayerMove.gd +++ b/client/network/PlayerMove.gd @@ -15,7 +15,7 @@ func handle_packet(data: PackedByteArray, _peer: PacketPeer): # var y := buffer.get_float() var z := buffer.get_float() - var player := %Players.get_node(player_id) + var player := Global.players.get_player(player_id) var controller := player.controller as ProxyController controller.server_position.x = x controller.server_position.z = z diff --git a/client/network/PlayerRemove.gd b/client/network/PlayerRemove.gd index de08f3f..ddd2915 100644 --- a/client/network/PlayerRemove.gd +++ b/client/network/PlayerRemove.gd @@ -3,10 +3,4 @@ extends PacketHandler func handle_packet(data: PackedByteArray, _peer: PacketPeer): var player_id := data.get_string_from_ascii() print("[%s] Remove player: %s" % [Global.username, player_id]) - - var player := %Players.get_node(player_id) - - if is_instance_valid(player): - player.queue_free() - else: - push_warning("PlayerRemove: Player with ID %s doesn't exist") \ No newline at end of file + %Players.remove(player_id) \ No newline at end of file diff --git a/client/player/Player.gd b/client/player/Player.gd index 629bfb7..70711ec 100644 --- a/client/player/Player.gd +++ b/client/player/Player.gd @@ -1,6 +1,8 @@ class_name Player extends Character +var id: String + func set_character_name(new_name: String): name = new_name get_node("Label").text = name \ No newline at end of file diff --git a/client/world/Main.tscn b/client/world/Main.tscn index b6d44ca..469c8bf 100644 --- a/client/world/Main.tscn +++ b/client/world/Main.tscn @@ -1,4 +1,4 @@ -[gd_scene load_steps=20 format=3 uid="uid://b40y7iuskv1ar"] +[gd_scene load_steps=21 format=3 uid="uid://b40y7iuskv1ar"] [ext_resource type="Script" path="res://Game.gd" id="1_uxop2"] [ext_resource type="Script" path="res://network/Client.gd" id="2_00qe8"] @@ -17,6 +17,7 @@ [ext_resource type="PackedScene" uid="uid://cb2t7bvvf3gwh" path="res://enemy/slime/Slime.tscn" id="13_hdbbd"] [ext_resource type="PackedScene" uid="uid://dagn5bf7ou3sd" path="res://ui/UI.tscn" id="14_ofk4r"] [ext_resource type="Script" path="res://camera/Camera.gd" id="15_ym3qc"] +[ext_resource type="Script" path="res://world/PlayerManager.gd" id="16_sjldq"] [ext_resource type="Material" uid="uid://ddy5gkw0k16dq" path="res://shader/OutlineMaterial.tres" id="16_srvfy"] [sub_resource type="QuadMesh" id="QuadMesh_7yiqd"] @@ -60,6 +61,7 @@ packet_type = 11 [node name="PlayerMove" type="Node" parent="Client"] script = ExtResource("7_6xfox") +delay = null packet_type = 12 [node name="PlayerJump" type="Node" parent="Client"] @@ -104,6 +106,7 @@ transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 8.53558, 1.28057, -0.306177) [node name="Players" type="Node3D" parent="."] unique_name_in_owner = true +script = ExtResource("16_sjldq") [node name="UI" parent="." instance=ExtResource("14_ofk4r")] diff --git a/client/world/PlayerManager.gd b/client/world/PlayerManager.gd new file mode 100644 index 0000000..c21ebf1 --- /dev/null +++ b/client/world/PlayerManager.gd @@ -0,0 +1,18 @@ +class_name PlayerManager +extends Node3D + +var players = {} + +func _ready(): + Global.players = self + +func add(player: Player): + add_child(player) + players[player.id] = player + +func get_player(id: String) -> Player: + return players[id] as Player + +func remove(id: String): + players[id].queue_free() + players.erase(id)