split network and main

This commit is contained in:
AleaJactaEst 2023-10-26 23:19:36 +02:00
parent efbdb34abd
commit 93fc0f44b2
5 changed files with 330 additions and 214 deletions

View file

@ -15,6 +15,10 @@ run/main_scene="res://scenes/main.tscn"
config/features=PackedStringArray("4.1", "Forward Plus")
config/icon="res://icon.svg"
[autoload]
Multi="*res://scenes/multi.gd"
[input]
ui_left={

View file

@ -20,7 +20,9 @@ func _on_button_pressed():
if $VBoxContainer/TextEdit.get_text() != "":
self.hide()
#get_parent().get_node("CharacterBody3D").set_enable_event(true)
get_parent().connect_enet($VBoxContainer/TextEdit.get_text(), listen_ip, listen_port)
#get_parent().connect_enet($VBoxContainer/TextEdit.get_text(), listen_ip, listen_port)
Multi.set_username($VBoxContainer/TextEdit.get_text())
#Multi.connect_server()
func check_testedit():
@ -36,8 +38,10 @@ func check_testedit():
$VBoxContainer/Button.set_disabled(true)
return
$VBoxContainer/Button.set_disabled(false)
Multi.set_ip(listen_ip)
Multi.set_port(listen_port)
func _on_text_edit_text_changed(value:String):
func _on_text_edit_text_changed(_value:String):
check_testedit()

View file

@ -2,231 +2,91 @@ 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
#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
#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()
_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)
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 _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 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
func _on_update_me(pos:Vector3):
self.set_player_position(pos)
self.get_node("CharacterBody3D").set_enable_event(true)
print(player_name, " -> size:", data.size(), " / " , len(player_name) )
errorEnet = dataEnet.send(1, data, 1)
if errorEnet != OK:
print("ERROR ENET: ", errorEnet)
return
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)
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)

View file

@ -93,6 +93,25 @@ layout_mode = 2
disabled = true
text = "OK"
[node name="Panel" type="Panel" parent="."]
offset_right = 199.0
offset_bottom = 40.0
size_flags_horizontal = 3
size_flags_vertical = 3
[node name="State" type="Label" parent="Panel"]
layout_mode = 1
anchors_preset = -1
anchor_right = 0.994
anchor_bottom = 0.75
offset_right = -0.0279999
offset_bottom = 10.0
grow_horizontal = 2
grow_vertical = 2
text = "Not connected"
horizontal_alignment = 1
vertical_alignment = 1
[connection signal="focus_entered" from="Window" to="Window" method="_on_focus_entered"]
[connection signal="focus_exited" from="Window" to="Window" method="_on_focus_exited"]
[connection signal="mouse_entered" from="Window" to="Window" method="_on_mouse_entered"]

229
client/scenes/multi.gd Normal file
View file

@ -0,0 +1,229 @@
extends Node
@export var listen_ip:String = "127.0.0.1":
set = set_ip
func set_ip(value):
listen_ip = value
disconnect_server()
@export var listen_port:int = 33333:
set = set_port
func set_port(value:int):
listen_port = value
disconnect_server()
@export var username:String = "":
set = set_username
func set_username(value):
username = value
disconnect_server()
@export var player_position:Node = null :
set = set_player_position
func set_player_position(value):
player_position = value
enum Connexion{
NONE,
ACCOUNT_REFUSED,
CONNECTING,
CONNECTED,
}
@export var state_connexion:Connexion
@export var id:int = 0
signal connexion_updated(new_state)
signal update_my_position(pos)
signal update_player_position(id, pos)
signal remove_player(id)
var enet:ENetConnection
var dataEnet:ENetPacketPeer
var errorEnet:Error
func update_state(value):
state_connexion = value
connexion_updated.emit(state_connexion)
func disconnect_server():
update_state(Connexion.NONE)
func create_server_enet():
print("------ create_server_enet")
enet = ENetConnection.new()
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
update_state(Connexion.CONNECTING)
func connect_server():
if username == "":
return
if player_position == null:
return
create_server_enet()
connect_to_host()
func _ready():
print("------ Multi initliazed")
update_state(Connexion.NONE)
func bad_account():
set_username("")
update_state(Connexion.ACCOUNT_REFUSED)
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)
update_my_position.emit(Vector3(x, y, z))
#self.set_player_position(Vector3(x, y, z))
update_state(Connexion.CONNECTED)
else: # KO
bad_account()
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
update_player_position.emit(mid, Vector3(mx, my, mz))
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
remove_player.emit(mid)
func send_account():
var data:PackedByteArray = PackedByteArray()
data.append(1)
data.append(len(username))
var packed_array = username.to_ascii_buffer()
data += packed_array
print(username, " -> size:", data.size(), " / " , len(username) )
errorEnet = dataEnet.send(1, data, 1)
if errorEnet != OK:
print("ERROR ENET: ", errorEnet)
return
func get_player_position():
if state_connexion != Connexion.CONNECTED:
return
var pos:PackedFloat64Array = PackedFloat64Array()
var posRaw:Vector3 = player_position.get_position()
pos.append(posRaw.x)
pos.append(posRaw.y)
pos.append(posRaw.z)
var data:PackedByteArray = PackedByteArray()
data += pos.to_byte_array()
errorEnet = dataEnet.send(2, data, 1)
if errorEnet == ERR_UNCONFIGURED:
update_state(Connexion.NONE)
elif errorEnet != OK:
print("ERROR ENET: ", errorEnet)
return
func _process(_delta):
if state_connexion == Connexion.ACCOUNT_REFUSED:
return
elif state_connexion == Connexion.NONE:
connect_server()
return
get_player_position()
if state_connexion == Connexion.CONNECTING:
send_account()
if dataEnet.get_state() == ENetPacketPeer.STATE_CONNECTED:
var event = enet.service()
if event[0] == ENetConnection.EVENT_RECEIVE:
get_event_received()
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()