43 lines
1.1 KiB
GDScript3
Raw Normal View History

2024-01-24 19:57:31 +00:00
extends PacketHandler
2024-01-26 21:45:26 +00:00
signal main_player_spawned(player: Player)
2024-01-24 19:57:31 +00:00
@export var player_scene: PackedScene
func _ready():
assert(player_scene)
func handle_packet(data: PackedByteArray, _peer: PacketPeer):
2024-01-26 11:50:46 +00:00
var buffer := StreamPeerBuffer.new()
buffer.data_array = data
var player_id := buffer.get_string()
var player_name := buffer.get_string()
2024-01-24 19:57:31 +00:00
var server_position := Vector3()
2024-01-26 11:50:46 +00:00
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])
2024-01-24 19:57:31 +00:00
2024-01-28 18:18:16 +00:00
var player := spawn_player(player_id)
2024-01-24 19:57:31 +00:00
player.position = server_position
2024-01-28 18:18:16 +00:00
player.set_character_name(player_name)
2024-01-25 23:29:05 +00:00
2024-01-28 21:08:08 +00:00
func spawn_player(id: String) -> Player:
var player = player_scene.instantiate()
player.id = id
if id == Global.account_id:
2024-01-25 23:29:05 +00:00
Global.player = player
2024-01-26 16:14:20 +00:00
player.controller = PlayerController.new()
2024-01-26 21:45:26 +00:00
main_player_spawned.emit(player)
2024-01-26 16:14:20 +00:00
else:
player.controller = ProxyController.new()
player.controller.character = player
player.add_child(player.controller)
2024-01-28 18:18:16 +00:00
%Players.add(player)
2024-01-24 19:57:31 +00:00
return player