bazar_alea/client/scenes/main.gd
2023-10-27 16:49:55 +02:00

95 lines
2.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")
func _ready():
_on_connexion_updated(Multi.Connexion.NONE)
Multi.set_player_position(self.get_node("CharacterBody3D"))
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)
Stream.set_server_ip("127.0.0.1")
Stream.server_ip = "127.0.0.1"
print(Stream.get_server_ip())
func _on_connexion_updated(new_state):
if new_state == Multi.Connexion.NONE:
self.get_node("CharacterBody3D").set_enable_event(false)
$Panel/State.set_text("Not Connected")
$Panel.show()
if new_state == Multi.Connexion.ACCOUNT_REFUSED:
self.get_node("CharacterBody3D").set_enable_event(false)
$Panel/State.set_text("Account Refused")
$Panel.show()
$Window.show()
elif new_state == Multi.Connexion.CONNECTING:
self.get_node("CharacterBody3D").set_enable_event(false)
$Panel/State.set_text("Connecting")
$Panel.show()
else:
$Panel.hide()
func _on_update_me(pos:Vector3):
self.set_player_position(pos)
self.get_node("CharacterBody3D").set_enable_event(true)
func _on_update_player(id:int, pos:Vector3):
var child = $Players.find_child(str(id), false, false)
if child == null:
print("Add player : ", id)
var scene = preload("res://scenes/player.tscn")
var instance = scene.instantiate()
instance.set_name(str(id))
$Players.add_child(instance)
#maxplayer += 1
var child2 = $Players.find_child(str(id), false, false)
child2.set_global_position(pos)
else:
child.set_global_position(pos)
func _on_remove_player(id:int):
#print("Remove player : ", mid)
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 set_player_position(pos: Vector3):
self.get_node("CharacterBody3D").set_global_position(pos)