2023-10-22 18:42:13 +00:00
|
|
|
extends Node3D
|
|
|
|
|
|
|
|
# The player name.
|
|
|
|
var player_name: String
|
2023-10-26 21:19:36 +00:00
|
|
|
#var connected:bool = false
|
|
|
|
#var errorEnet:Error
|
|
|
|
#var id = 0
|
|
|
|
#var maxplayer = 0
|
|
|
|
#var listen_ip:String
|
|
|
|
#var listen_port:int
|
2023-10-22 18:42:13 +00:00
|
|
|
|
|
|
|
# Server confirm our account
|
2023-10-26 21:19:36 +00:00
|
|
|
#var account_confirmed:bool = false
|
2023-10-22 18:42:13 +00:00
|
|
|
|
2023-11-14 21:41:08 +00:00
|
|
|
#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)
|
2023-11-19 19:41:38 +00:00
|
|
|
@export var debug_window: PackedScene
|
2023-10-22 18:42:13 +00:00
|
|
|
|
2023-11-19 19:41:38 +00:00
|
|
|
#@onready var _MainWindow: Window = get_window()
|
2023-10-22 18:42:13 +00:00
|
|
|
|
|
|
|
func _ready():
|
2023-10-26 21:19:36 +00:00
|
|
|
_on_connexion_updated(Multi.Connexion.NONE)
|
2023-11-14 21:41:08 +00:00
|
|
|
#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)
|
2023-11-19 19:41:38 +00:00
|
|
|
#Multi.set_player_position($PlayerSpawnLocation.get_child(0))
|
2023-11-22 19:55:36 +00:00
|
|
|
Multi.set_player_position($PlayerSpawnLocation.get_child(0))
|
2023-10-26 21:19:36 +00:00
|
|
|
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)
|
2023-11-19 19:41:38 +00:00
|
|
|
#$Window.world_2d = _MainWindow.world_2d
|
2023-10-26 21:19:36 +00:00
|
|
|
|
2023-11-14 21:41:08 +00:00
|
|
|
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):
|
2023-10-26 21:19:36 +00:00
|
|
|
if new_state == Multi.Connexion.NONE:
|
2023-11-14 21:41:08 +00:00
|
|
|
#self.get_node("CharacterBody3D").set_enable_event(false)
|
|
|
|
$CameraStarting.set_current(true)
|
2023-10-26 21:19:36 +00:00
|
|
|
$Panel/State.set_text("Not Connected")
|
|
|
|
$Panel.show()
|
2023-11-14 21:41:08 +00:00
|
|
|
elif new_state == Multi.Connexion.ACCOUNT_REFUSED:
|
|
|
|
#self.get_node("CharacterBody3D").set_enable_event(false)
|
|
|
|
$CameraStarting.set_current(true)
|
2023-10-26 21:19:36 +00:00
|
|
|
$Panel/State.set_text("Account Refused")
|
|
|
|
$Panel.show()
|
|
|
|
$Window.show()
|
|
|
|
elif new_state == Multi.Connexion.CONNECTING:
|
2023-11-14 21:41:08 +00:00
|
|
|
#self.get_node("CharacterBody3D").set_enable_event(false)
|
|
|
|
$CameraStarting.set_current(true)
|
2023-10-26 21:19:36 +00:00
|
|
|
$Panel/State.set_text("Connecting")
|
|
|
|
$Panel.show()
|
2023-11-14 21:41:08 +00:00
|
|
|
elif new_state == Multi.Connexion.CONNECTED:
|
2023-10-26 21:19:36 +00:00
|
|
|
$Panel.hide()
|
2023-11-14 21:41:08 +00:00
|
|
|
$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())
|
2023-11-20 22:18:02 +00:00
|
|
|
$PlayerSpawnLocation.get_child(0).set_activate()
|
2023-11-19 19:41:38 +00:00
|
|
|
$PlayerSpawnLocation.set_visible(true)
|
2023-11-14 21:41:08 +00:00
|
|
|
$CameraStarting.set_current(false)
|
|
|
|
else:
|
|
|
|
$CameraStarting.set_current(true)
|
|
|
|
$Panel/State.set_text("Unknown")
|
|
|
|
$Panel.show()
|
2023-10-22 18:42:13 +00:00
|
|
|
|
|
|
|
|
2023-11-26 12:06:37 +00:00
|
|
|
func _on_update_me(tick:int, pos:Vector3, rot:Vector3):
|
2023-12-04 22:05:35 +00:00
|
|
|
$PlayerSpawnLocation.get_child(0).set_first_position_player(tick, pos, rot)
|
2023-10-24 21:51:30 +00:00
|
|
|
|
2023-10-22 18:42:13 +00:00
|
|
|
|
2023-12-04 21:14:32 +00:00
|
|
|
func _on_update_player(id:int, tick:int, pos:Vector3, rot:Vector3, extra:int, tickjump:int, vel:Vector3):
|
2023-10-26 21:19:36 +00:00
|
|
|
var child = $Players.find_child(str(id), false, false)
|
|
|
|
if child == null:
|
|
|
|
print("Add player : ", id)
|
2023-11-14 21:41:08 +00:00
|
|
|
var scene = preload(player_path)
|
2023-10-26 21:19:36 +00:00
|
|
|
var instance = scene.instantiate()
|
|
|
|
instance.set_name(str(id))
|
|
|
|
$Players.add_child(instance)
|
2023-11-14 21:41:08 +00:00
|
|
|
instance.set_visible(true)
|
|
|
|
instance.set_id(id)
|
2023-12-04 21:14:32 +00:00
|
|
|
child = $Players.find_child(str(id), false, false)
|
2023-10-26 21:19:36 +00:00
|
|
|
else:
|
2023-12-04 21:14:32 +00:00
|
|
|
print("Update player : ", id, " ", pos, " ", rot, " ", extra, " ",tickjump, " ", vel)
|
|
|
|
child.update_player(tick, pos, rot, extra, tickjump, vel)
|
|
|
|
|
2023-10-26 21:19:36 +00:00
|
|
|
|
|
|
|
|
|
|
|
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("==========================")
|
2023-10-22 18:42:13 +00:00
|
|
|
|
|
|
|
|
2023-11-19 19:41:38 +00:00
|
|
|
#func _process(delta):
|
|
|
|
## if $PlayerSpawnLocation:
|
|
|
|
## print(">", $PlayerSpawnLocation.get_global_position())
|
|
|
|
# pass
|
2023-11-14 21:41:08 +00:00
|
|
|
|
|
|
|
|
|
|
|
func _input(event):
|
|
|
|
if event is InputEventKey:
|
|
|
|
if event.is_action_pressed("ui_quit"):
|
|
|
|
get_tree().quit()
|
2023-11-19 19:41:38 +00:00
|
|
|
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
|
2023-11-14 21:41:08 +00:00
|
|
|
|
|
|
|
|
2023-10-22 18:42:13 +00:00
|
|
|
func set_player_position(pos: Vector3):
|
2023-11-22 23:05:33 +00:00
|
|
|
#$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)
|