54 lines
1.3 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)
2024-01-29 22:16:06 +00:00
func handle_packet(data: PackedByteArray):
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()
2024-02-27 22:21:44 +00:00
Log.info("Add player: %s %s @ %v" % [player_id, player_name, server_position])
2024-01-24 19:57:31 +00:00
2024-02-28 12:23:17 +00:00
spawn_player.call_deferred(player_id, player_name, server_position)
2024-01-25 23:29:05 +00:00
2024-02-24 14:30:41 +00:00
func spawn_player(id: String, nick: String, position: Vector3) -> Player:
2024-01-29 22:16:06 +00:00
var player: Player
if Global.players.has(id):
player = Global.players.get_player(id)
2024-02-16 19:04:50 +00:00
if id == Global.account.id:
2024-01-29 22:16:06 +00:00
Global.player = player
2024-02-24 14:30:41 +00:00
2024-01-29 22:16:06 +00:00
return player
2024-02-24 14:30:41 +00:00
2024-01-29 22:16:06 +00:00
player = player_scene.instantiate()
2024-01-28 21:08:08 +00:00
player.id = id
2024-02-23 19:13:46 +00:00
player.position = position
2024-02-24 14:30:41 +00:00
var controller: Controller
2024-01-28 21:08:08 +00:00
2024-02-16 19:04:50 +00:00
if id == Global.account.id:
2024-02-24 14:30:41 +00:00
controller = PlayerController.new(player)
2024-01-29 22:16:06 +00:00
Global.player = player
2024-01-26 21:45:26 +00:00
main_player_spawned.emit(player)
2024-01-26 16:14:20 +00:00
else:
2024-02-24 14:30:41 +00:00
controller = ProxyController.new(player)
player.set_player_name(nick)
player.add_child(controller)
2024-02-12 08:48:58 +00:00
Global.players.add(player)
2024-02-24 14:30:41 +00:00
controller.control(player)
2024-01-24 19:57:31 +00:00
return player