bazar_alea/client/scenes/main.gd

232 lines
6.6 KiB
GDScript

extends Node3D
# The player name.
var player_name: String
var connected:bool = false
var enet:ENetConnection
var dataEnet:ENetPacketPeer
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")
var dtls := PacketPeerDTLS.new()
var udp := PacketPeerUDP.new()
func create_server_enet():
print("------ create_server_enet")
errorEnet = enet.create_host(10)
if errorEnet != OK:
print("ERROR ENET.create_host: ", errorEnet)
return
func connect_to_host():
print("Connect to : " + listen_ip + " : " + str(listen_port))
dataEnet = enet.connect_to_host(listen_ip, listen_port, 10)
print(dataEnet)
if not dataEnet:
print("ERROR dataEnet == null: ")
return
elif not dataEnet.is_active():
print("ERROR enet.connect_to_host: ", dataEnet.is_active())
return
func _ready():
enet = ENetConnection.new()
create_server_enet()
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 send_account():
var data:PackedByteArray = PackedByteArray()
data.append(1)
data.append(len(player_name))
var packed_array = player_name.to_ascii_buffer()
data += packed_array
print(player_name, " -> size:", data.size(), " / " , len(player_name) )
errorEnet = dataEnet.send(1, data, 1)
if errorEnet != OK:
print("ERROR ENET: ", errorEnet)
return
func set_player_position(pos: Vector3):
self.get_node("CharacterBody3D").set_global_position(pos)
func get_player_position():
if not account_confirmed:
return
#print("perso: ", id, " ", self.get_node("CharacterBody3D").get_position())
var pos:PackedFloat64Array = PackedFloat64Array()
var posRaw:Vector3 = self.get_node("CharacterBody3D").get_position()
#posRaw.x = 123456789182729270e15
#posRaw.y = 2.0
#posRaw.z = 3.0
pos.append(posRaw.x)
#print("x:", pos.to_byte_array())
pos.append(posRaw.y)
#print("x+y:", pos.to_byte_array())
pos.append(posRaw.z)
#print("x+y+z:", pos.to_byte_array())
var data:PackedByteArray = PackedByteArray()
data += pos.to_byte_array()
#data.append(len(player_name))
#var packed_array = player_name.to_ascii_buffer()
#data += packed_array
#print(player_name, " -> size:", data.size(), " / " , len(player_name) )
errorEnet = dataEnet.send(2, data, 1)
if errorEnet == ERR_UNCONFIGURED:
connect_to_host()
elif errorEnet != OK:
print("ERROR ENET: ", errorEnet)
return
func bad_acocunt():
player_name = ""
self.get_node("Window").open_windows()
func get_event_received():
var data = dataEnet.get_packet()
#print("get: ", data)
if data[0] == 1: # Return connexion
if data[1] == 0: # OK
id = data.decode_u64(2)
var x = data.decode_double(2+8)
var y = data.decode_double(2+16)
var z = data.decode_double(2+24)
# print("id:", id, " x:", x, " y:", y, " z:", z)
self.set_player_position(Vector3(x, y, z))
account_confirmed = true
self.get_node("CharacterBody3D").set_enable_event(true)
else: # KO
bad_acocunt()
elif data[0] == 3: # Get position
if id == 0:
return
var nb = data[1]
var pos = 2
for i in nb:
var mid = data.decode_u64(pos)
var mx = data.decode_double(pos+8)
var my = data.decode_double(pos+16)
var mz = data.decode_double(pos+24)
pos += 32
if mid == id:
#print("Me id:", mid, " x:", mx, " y:", my, " z:", mz)
continue
#print("-- id:", mid, " x:", mx, " y:", my, " z:", mz)
var child = $Players.find_child(str(mid), false, false)
if child == null:
print("Add player : ", mid)
if maxplayer > 3:
continue
var scene = preload("res://scenes/player.tscn")
var instance = scene.instantiate()
#var current_scene = scene.instance()
#var GrabbedInstance = PLAYER.instance()
instance.set_name(str(mid))
$Players.add_child(instance)
#$Players.add_child(scene)
maxplayer += 1
var child2 = $Players.find_child(str(mid), false, false)
child2.set_global_position(Vector3(mx, my, mz))
else:
child.set_global_position(Vector3(mx, my, mz))
#print("Found:", $Players.get_child_count())
var nbuserremove = data[pos]
pos += 1
for i in nbuserremove:
var mid = data.decode_u64(pos)
pos += 8
if mid == id:
print("Try to remove me :", mid)
continue
#print("Remove player : ", mid)
var child = $Players.find_child(str(mid), false, false)
if child != null:
print("Remove player : ", mid)
$Players.get_node(str(mid)).queue_free()
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(_delta):
if not dataEnet:
return
#print("state:", dataEnet.get_state())
get_player_position()
if dataEnet.get_state() == ENetPacketPeer.STATE_CONNECTED:
var event = enet.service()
if event[0] == ENetConnection.EVENT_RECEIVE:
#print("get: ", dataEnet.get_packet())
get_event_received()
if player_name != "" and not account_confirmed:
send_account()
elif dataEnet.get_state() == ENetPacketPeer.STATE_CONNECTING:
var event = enet.service()
if event[0] == ENetConnection.EVENT_RECEIVE:
pass
elif dataEnet.get_state() == ENetPacketPeer.STATE_DISCONNECTED:
var _event = enet.service()
print("STATE_DISCONNECTED")
elif dataEnet.get_state() == ENetPacketPeer.STATE_ACKNOWLEDGING_CONNECT:
var _event = enet.service()
print("STATE_ACKNOWLEDGING_CONNECT")
elif dataEnet.get_state() == ENetPacketPeer.STATE_CONNECTION_PENDING:
var _event = enet.service()
print("STATE_CONNECTION_PENDING")
elif dataEnet.get_state() == ENetPacketPeer.STATE_CONNECTION_SUCCEEDED:
var _event = enet.service()
print("STATE_CONNECTION_SUCCEEDED")
elif dataEnet.get_state() == ENetPacketPeer.STATE_DISCONNECT_LATER:
var _event = enet.service()
print("STATE_DISCONNECT_LATER")
elif dataEnet.get_state() == ENetPacketPeer.STATE_DISCONNECTING:
var _event = enet.service()
print("STATE_DISCONNECTING")
elif dataEnet.get_state() == ENetPacketPeer.STATE_ACKNOWLEDGING_DISCONNECT:
var _event = enet.service()
print("STATE_ACKNOWLEDGING_DISCONNECT")
elif dataEnet.get_state() == ENetPacketPeer.STATE_ZOMBIE:
var _event = enet.service()
print("STATE_ZOMBIE")
else:
var _event = enet.service()
func connect_enet(_name:String, _listen_ip:String, _listen_port:int):
player_name = _name
listen_ip = _listen_ip
listen_port = _listen_port
connect_to_host()
print("My name:" + player_name)