54 lines
1.3 KiB
GDScript

extends PacketHandler
signal main_player_spawned(player: Player)
@export var player_scene: PackedScene
func _ready():
assert(player_scene)
func handle_packet(data: PackedByteArray):
var buffer := StreamPeerBuffer.new()
buffer.data_array = data
var player_id := buffer.get_string()
var player_name := buffer.get_string()
var server_position := Vector3()
server_position.x = buffer.get_float()
server_position.y = buffer.get_float()
server_position.z = buffer.get_float()
Log.info("Add player: %s %s @ %v" % [player_id, player_name, server_position])
spawn_player.call_deferred(player_id, player_name, server_position)
func spawn_player(id: String, nick: String, position: Vector3) -> Player:
var player: Player
if Global.players.has(id):
player = Global.players.get_player(id)
if id == Global.account.id:
Global.player = player
return player
player = player_scene.instantiate()
player.id = id
player.position = position
var controller: Controller
if id == Global.account.id:
controller = PlayerController.new(player)
Global.player = player
main_player_spawned.emit(player)
else:
controller = ProxyController.new(player)
player.set_player_name(nick)
player.add_child(controller)
Global.players.add(player)
controller.control(player)
return player