Skip to content
Snippets Groups Projects
Commit 834f808d authored by Canopteks's avatar Canopteks
Browse files

test environment for server-clients connection

parent d12e358d
Branches
Tags
No related merge requests found
...@@ -16,7 +16,7 @@ _global_script_class_icons={ ...@@ -16,7 +16,7 @@ _global_script_class_icons={
[application] [application]
config/name="client" config/name="client"
run/main_scene="res://scenes/levels/Game.tscn" run/main_scene="res://scenes/levels/lobby/lobby.tscn"
config/icon="res://icon.png" config/icon="res://icon.png"
[rendering] [rendering]
......
...@@ -32,13 +32,13 @@ func process_input(delta): ...@@ -32,13 +32,13 @@ func process_input(delta):
dir = Vector3() dir = Vector3()
var input_movement:int = 0 var input_movement:int = 0
if Input.is_action_pressed("movement_left"): if Input.is_action_pressed("ui_left"):
input_movement = -1 input_movement = -1
if Input.is_action_pressed("movement_right"): if Input.is_action_pressed("ui_right"):
input_movement = 1 input_movement = 1
if player.is_on_floor() || player.is_on_wall(): if player.is_on_floor() || player.is_on_wall():
if Input.is_action_just_pressed("movement_jump"): if Input.is_action_just_pressed("ui_up"):
vel.y = JUMP_SPEED vel.y = JUMP_SPEED
if Input.is_action_pressed("movement_sprint"): if Input.is_action_pressed("movement_sprint"):
......
[gd_scene load_steps=4 format=2] [gd_scene load_steps=3 format=2]
[ext_resource path="res://scenes/entities/player/Player.tscn" type="PackedScene" id=1] [ext_resource path="res://scenes/entities/player/Player.tscn" type="PackedScene" id=1]
[ext_resource path="res://Camera.gd" type="Script" id=2] [ext_resource path="res://scenes/entities/bloc/Bloc.tscn" type="PackedScene" id=2]
[ext_resource path="res://scenes/entities/bloc/Bloc.tscn" type="PackedScene" id=3]
[node name="Spatial" type="Spatial"] [node name="Spatial" type="Spatial"]
[node name="Player" parent="." instance=ExtResource( 1 )] [node name="Player" parent="." instance=ExtResource( 1 )]
transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1.80767, 0 ) transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, -0.0417032, 1.4664, 0 )
[node name="Camera" type="Camera" parent="."] [node name="Camera" type="Camera" parent="."]
transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1.08, 18.986 ) transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1.08, 18.986 )
fov = 30.0 fov = 30.0
script = ExtResource( 2 )
[node name="Structure" type="Spatial" parent="."] [node name="Structure" type="Spatial" parent="."]
[node name="Bloc" parent="Structure" instance=ExtResource( 3 )] [node name="Bloc" parent="Structure" instance=ExtResource( 2 )]
transform = Transform( 10, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0 ) transform = Transform( 10, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0 )
[node name="Bloc2" parent="Structure" instance=ExtResource( 3 )] [node name="Bloc2" parent="Structure" instance=ExtResource( 2 )]
transform = Transform( 5, 0, 0, 0, 1, 0, 0, 0, 1, 0, 4.15191, 0 ) transform = Transform( 5, 0, 0, 0, 1, 0, 0, 0, 1, 0, 4.15191, 0 )
extends Control
var players = {}
func _ready():
get_tree().connect("connected_to_server", self, "_player_connected")
get_tree().connect("connection_failed", self, "_connection_failed")
get_tree().connect("server_disconnected", self, "_player_disconnected")
func _on_joinButton_pressed():
var ip = get_node("menu/ipLineEdit").text
if not ip.is_valid_ip_address():
get_node("menu/errorLabel").text = "Invalid IPv4 address!"
return
var port = get_node("menu/portLineEdit").text
if port == "":
get_node("menu/errorLabel").text = "Invalid port!"
return
get_node("menu/errorLabel").text=""
get_node("menu/joinButton").disabled = true
var peer_join = NetworkedMultiplayerENet.new()
peer_join.create_client(ip, int(port))
get_tree().set_network_peer(peer_join)
#checks:
print("Trying to join the server ", ip, ":", port)
remote func register_player(id):
print("A new player joined the game ..", id) #adding the new id to anyones array!
#if !(id in players):
players[id] = ""
#We can list all the users
# Checking whether the tree is initialized as a server or client:
# Server sends the info of existing players back to the new player
if get_tree().is_network_server():
# Sending the list of existing players to the new player from ther server's personal list
for peer_id in players:
rpc_id(id, "register_player", peer_id)
remote func get_message(mess):
if mess == "quit":
print("Received order to quit")
get_tree().set_network_peer(null)
get_tree().quit()
else:
print(mess)
func _player_connected():
print("You succesfuly joined the server with the id " + str(get_tree().get_network_unique_id()))
func _connection_failed():
print("Connection failure")
func _player_disconnected():
print("You disconnected the server")
\ No newline at end of file
[gd_scene load_steps=2 format=2]
[ext_resource path="res://scenes/levels/lobby/lobby.gd" type="Script" id=1]
[node name="lobby" type="Control"]
anchor_right = 1.0
anchor_bottom = 1.0
script = ExtResource( 1 )
[node name="menu" type="Panel" parent="."]
margin_right = 506.0
margin_bottom = 302.0
[node name="ipLabel" type="Label" parent="menu"]
margin_left = 60.0
margin_top = 32.0
margin_right = 140.0
margin_bottom = 48.0
text = "IP:"
[node name="ipLineEdit" type="LineEdit" parent="menu"]
margin_left = 62.0
margin_top = 64.0
margin_right = 176.0
margin_bottom = 100.0
rect_pivot_offset = Vector2( 55, 16 )
hint_tooltip = "91.121.81.74 : lucas
127.0.0.1 : local"
text = "91.121.81.74"
[node name="portLabel" type="Label" parent="menu"]
margin_left = 60.0
margin_top = 131.0
margin_right = 140.0
margin_bottom = 147.0
text = "Port:"
[node name="portLineEdit" type="LineEdit" parent="menu"]
margin_left = 66.0
margin_top = 173.0
margin_right = 180.0
margin_bottom = 209.0
rect_pivot_offset = Vector2( 55, 16 )
hint_tooltip = "défini en dur: 10001"
text = "10001"
[node name="joinButton" type="Button" parent="menu"]
margin_left = 319.0
margin_top = 133.0
margin_right = 398.0
margin_bottom = 181.0
text = "Join"
[node name="errorLabel" type="Label" parent="menu"]
margin_left = 55.0
margin_top = 244.0
margin_right = 455.0
margin_bottom = 277.0
[connection signal="pressed" from="menu/joinButton" to="." method="_on_joinButton_pressed"]
[gd_scene load_steps=2 format=2] [gd_scene load_steps=2 format=2]
[ext_resource path="res://Node.gd" type="Script" id=1] [ext_resource path="res://scenes/levels/testnetwork/Node.gd" type="Script" id=1]
[node name="Node" type="Node"] [node name="Node" type="Node"]
script = ExtResource( 1 ) script = ExtResource( 1 )
File mode changed from 100755 to 100644
#server
extends Node extends Node
var peer = NetworkedMultiplayerENet.new() const PORT = 10001
export var PORT = 10001 const MAX_CLIENTS = 2
var players = {}
func _ready(): func _ready():
peer.create_server(PORT,2) get_tree().connect("network_peer_connected", self, "_player_connected")
get_tree().connect("network_peer_disconnected", self, "_player_disconnected")
#get_tree().connect("connection_failed", self, "_player_disconnected")
var peer = NetworkedMultiplayerENet.new()
peer.create_server(PORT, MAX_CLIENTS)
get_tree().set_network_peer(peer) get_tree().set_network_peer(peer)
get_tree().connect("network_peer_connected", self, "_player_connected") #Quand un client se connecte print("Server now hosting")
get_tree().connect("network_peer_disconnected", self, "_player_disconnected") #Quand un client se déconnecte
remote func register_player(id):
print("A new player joined the game ..", id) #adding the new id to anyones array!
#if !(id in players):
players[id] = ""
#We can list all the users
# Checking whether the tree is initialized as a server or client:
# Server sends the info of existing players back to the new player
if get_tree().is_network_server():
# Sending the list of existing players to the new player from ther server's personal list
for peer_id in players:
rpc_id(id, "register_player", peer_id)
func game_setup():
print("game started with players ", players.values())
#Quand un client se connecte #Quand un client se connecte
func _player_connected(id): func _player_connected(id):
print("Player ", id, " connected") if (players.size()<MAX_CLIENTS):
set_network_master(id) print("Hello players. I have just joined the game and I will beat u!: ", id)
register_player(id)
else:
game_setup()
#Quand un client se déconnecte #Quand un client se déconnecte
func _player_disconnected(id): func _player_disconnected(id):
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment