42 lines
1.1 KiB
GDScript
42 lines
1.1 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, _peer: PacketPeer):
|
|
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()
|
|
|
|
print("[%s] Add player: %s %s @ %v" % [Global.username, player_id, player_name, server_position])
|
|
|
|
var player := spawn_player()
|
|
player.position = server_position
|
|
player.set_character_name(player_id)
|
|
|
|
if player_id == Global.account_id:
|
|
Global.player = player
|
|
player.controller = PlayerController.new()
|
|
main_player_spawned.emit(player)
|
|
else:
|
|
player.controller = ProxyController.new()
|
|
|
|
player.controller.character = player
|
|
player.add_child(player.controller)
|
|
|
|
func spawn_player() -> Player:
|
|
var player = player_scene.instantiate()
|
|
%Players.add_child(player)
|
|
return player
|