bazar_alea/client/scenes/main.gd

161 lines
5 KiB
GDScript

extends Node3D
# The player name.
var player_name: String
#var connected:bool = false
#var errorEnet:Error
#var id = 0
#var maxplayer = 0
#var listen_ip:String
#var listen_port:int
# Server confirm our account
#var account_confirmed:bool = false
#const PLAYER = preload("res://scenes/player.tscn")
#const PLAYER = preload("res://player/character.tscn")
const player_path:String = "res://player/character.tscn"
@export var PlayerCharacter = preload(player_path)
@export var debug_window: PackedScene
#@onready var _MainWindow: Window = get_window()
func _ready():
_on_connexion_updated(Multi.Connexion.NONE)
#Multi.set_player_position(self.get_node("CharacterBody3D"))
for child in $PlayerSpawnLocation.get_children():
child.queue_free()
var p = PlayerCharacter.instantiate()
$PlayerSpawnLocation.add_child(p)
$PlayerSpawnLocation.set_visible(false)
p.set_otherplayer(false)
#Multi.set_player_position($PlayerSpawnLocation.get_child(0))
Multi.set_player_position($PlayerSpawnLocation.get_child(0))
Multi.connexion_updated.connect(_on_connexion_updated)
Multi.update_my_position.connect(_on_update_me)
Multi.update_player_position.connect(_on_update_player)
Multi.remove_player.connect(_on_remove_player)
#$Window.world_2d = _MainWindow.world_2d
func create_view_window():
pass
# var new_window: Window = view_window.instantiate()
# # Pass the main window's world to the new window
# # This is what makes it possible to show the same world in multiple windows
# new_window.world_2d = _MainWindow.world_2d
# new_window.world_3d = _MainWindow.world_3d
# # The new window needs to have the same world offset as the player
# new_window.world_offset = world_offset
# # Contrarily to the main window, hide the player and show the world
# new_window.set_canvas_cull_mask_bit(player_visibility_layer, false)
# new_window.set_canvas_cull_mask_bit(world_visibility_layer, true)
# add_child(new_window)
func _on_connexion_updated(new_state:Multi.Connexion):
if new_state == Multi.Connexion.NONE:
#self.get_node("CharacterBody3D").set_enable_event(false)
$CameraStarting.set_current(true)
$Panel/State.set_text("Not Connected")
$Panel.show()
elif new_state == Multi.Connexion.ACCOUNT_REFUSED:
#self.get_node("CharacterBody3D").set_enable_event(false)
$CameraStarting.set_current(true)
$Panel/State.set_text("Account Refused")
$Panel.show()
$Window.show()
elif new_state == Multi.Connexion.CONNECTING:
#self.get_node("CharacterBody3D").set_enable_event(false)
$CameraStarting.set_current(true)
$Panel/State.set_text("Connecting")
$Panel.show()
elif new_state == Multi.Connexion.CONNECTED:
$Panel.hide()
$PlayerSpawnLocation.get_child(0).set_current_camera()
$PlayerSpawnLocation.get_child(0).set_name(str(Multi.get_id()))
$PlayerSpawnLocation.get_child(0).set_id(Multi.get_id())
$PlayerSpawnLocation.get_child(0).set_activate()
$PlayerSpawnLocation.set_visible(true)
$CameraStarting.set_current(false)
else:
$CameraStarting.set_current(true)
$Panel/State.set_text("Unknown")
$Panel.show()
func _on_update_me(tick:int, pos:Vector3, rot:Vector3):
$PlayerSpawnLocation.get_child(0).update_player(tick, pos, rot)
func _on_update_player(id:int, tick:int, pos:Vector3, rot:Vector3):
var child = $Players.find_child(str(id), false, false)
if child == null:
print("Add player : ", id)
var scene = preload(player_path)
var instance = scene.instantiate()
instance.set_name(str(id))
$Players.add_child(instance)
instance.set_visible(true)
instance.set_id(id)
var child2 = $Players.find_child(str(id), false, false)
child2.update_player(tick, pos, rot)
#child2.set_my_position(pos)
#child2.set_my_rotation(rot)
else:
print("Update player : ", id, " ", pos, " ", rot)
#print(pos)
#pos.y += 10.0
child.update_player(tick, pos, rot)
#child.set_my_position(pos)
#child.set_my_rotation(rot)
##child.set_visible(true)
func _on_remove_player(id:int):
var child = $Players.find_child(str(id), false, false)
if child != null:
print("Remove player : ", id)
$Players.get_node(str(id)).queue_free()
#
#func decode_msg(data:PackedByteArray):
# print("==========================")
# var cmd = data.decode_u8(0)
# print("cmd:", cmd)
# var taille = data.decode_u8(1)
# print("taille:", taille)
# var str:String = ""
# var tmp = data.slice(2,taille+2)
# print("tmp:", tmp)
#
# str = tmp.get_string_from_utf8()
#
# print("str:", str)
# print("len str:", len(str))
#
# print("==========================")
#func _process(delta):
## if $PlayerSpawnLocation:
## print(">", $PlayerSpawnLocation.get_global_position())
# pass
func _input(event):
if event is InputEventKey:
if event.is_action_pressed("ui_quit"):
get_tree().quit()
elif event.is_action_pressed("ui_debug"):
var new_window: Window = debug_window.instantiate()
add_child(new_window)
new_window.node_players = $Players
new_window.node_me = $PlayerSpawnLocation
func set_player_position(pos: Vector3):
#$PlayerSpawnLocation.set_global_position(pos)
$PlayerSpawnLocation.get_child(0).set_my_position(pos)
func set_player_rotation(rot: Vector3):
$PlayerSpawnLocation.get_child(0).set_my_rotation(rot)