Improved player manager
This commit is contained in:
parent
0b988f58cf
commit
75801e21fd
@ -2,6 +2,7 @@ extends Node
|
|||||||
|
|
||||||
var camera: Camera3D
|
var camera: Camera3D
|
||||||
var player: Player
|
var player: Player
|
||||||
|
var players: PlayerManager
|
||||||
var instance_id := OS.get_process_id() % 4
|
var instance_id := OS.get_process_id() % 4
|
||||||
var username := "user%d" % instance_id
|
var username := "user%d" % instance_id
|
||||||
var account_id: String
|
var account_id: String
|
||||||
|
@ -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])
|
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.position = server_position
|
||||||
player.set_character_name(player_id)
|
player.set_character_name(player_name)
|
||||||
|
|
||||||
if player_id == Global.account_id:
|
if player_id == Global.account_id:
|
||||||
Global.player = player
|
Global.player = player
|
||||||
@ -35,7 +35,8 @@ func handle_packet(data: PackedByteArray, _peer: PacketPeer):
|
|||||||
player.controller.character = player
|
player.controller.character = player
|
||||||
player.add_child(player.controller)
|
player.add_child(player.controller)
|
||||||
|
|
||||||
func spawn_player() -> Player:
|
func spawn_player(id: String) -> Player:
|
||||||
var player = player_scene.instantiate()
|
var player = player_scene.instantiate()
|
||||||
%Players.add_child(player)
|
player.id = id
|
||||||
|
%Players.add(player)
|
||||||
return player
|
return player
|
||||||
|
@ -13,5 +13,5 @@ func on_jump():
|
|||||||
|
|
||||||
func handle_packet(data: PackedByteArray, _peer: PacketPeer):
|
func handle_packet(data: PackedByteArray, _peer: PacketPeer):
|
||||||
var player_id := data.get_string_from_ascii()
|
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()
|
player.jump()
|
||||||
|
@ -15,7 +15,7 @@ func handle_packet(data: PackedByteArray, _peer: PacketPeer):
|
|||||||
# var y := buffer.get_float()
|
# var y := buffer.get_float()
|
||||||
var z := 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
|
var controller := player.controller as ProxyController
|
||||||
controller.server_position.x = x
|
controller.server_position.x = x
|
||||||
controller.server_position.z = z
|
controller.server_position.z = z
|
||||||
|
@ -3,10 +3,4 @@ extends PacketHandler
|
|||||||
func handle_packet(data: PackedByteArray, _peer: PacketPeer):
|
func handle_packet(data: PackedByteArray, _peer: PacketPeer):
|
||||||
var player_id := data.get_string_from_ascii()
|
var player_id := data.get_string_from_ascii()
|
||||||
print("[%s] Remove player: %s" % [Global.username, player_id])
|
print("[%s] Remove player: %s" % [Global.username, player_id])
|
||||||
|
%Players.remove(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")
|
|
@ -1,6 +1,8 @@
|
|||||||
class_name Player
|
class_name Player
|
||||||
extends Character
|
extends Character
|
||||||
|
|
||||||
|
var id: String
|
||||||
|
|
||||||
func set_character_name(new_name: String):
|
func set_character_name(new_name: String):
|
||||||
name = new_name
|
name = new_name
|
||||||
get_node("Label").text = name
|
get_node("Label").text = name
|
@ -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://Game.gd" id="1_uxop2"]
|
||||||
[ext_resource type="Script" path="res://network/Client.gd" id="2_00qe8"]
|
[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://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="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://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"]
|
[ext_resource type="Material" uid="uid://ddy5gkw0k16dq" path="res://shader/OutlineMaterial.tres" id="16_srvfy"]
|
||||||
|
|
||||||
[sub_resource type="QuadMesh" id="QuadMesh_7yiqd"]
|
[sub_resource type="QuadMesh" id="QuadMesh_7yiqd"]
|
||||||
@ -60,6 +61,7 @@ packet_type = 11
|
|||||||
|
|
||||||
[node name="PlayerMove" type="Node" parent="Client"]
|
[node name="PlayerMove" type="Node" parent="Client"]
|
||||||
script = ExtResource("7_6xfox")
|
script = ExtResource("7_6xfox")
|
||||||
|
delay = null
|
||||||
packet_type = 12
|
packet_type = 12
|
||||||
|
|
||||||
[node name="PlayerJump" type="Node" parent="Client"]
|
[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="."]
|
[node name="Players" type="Node3D" parent="."]
|
||||||
unique_name_in_owner = true
|
unique_name_in_owner = true
|
||||||
|
script = ExtResource("16_sjldq")
|
||||||
|
|
||||||
[node name="UI" parent="." instance=ExtResource("14_ofk4r")]
|
[node name="UI" parent="." instance=ExtResource("14_ofk4r")]
|
||||||
|
|
||||||
|
18
client/world/PlayerManager.gd
Normal file
18
client/world/PlayerManager.gd
Normal file
@ -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)
|
Loading…
Reference in New Issue
Block a user