Basic lobby connections

This commit is contained in:
Daniel Flanagan 2021-11-15 16:25:13 -06:00
parent 6de5819595
commit 01fc1b0386
Signed by: lytedev
GPG key ID: 5B2020A0F9921EF4
9 changed files with 146 additions and 16 deletions

View file

@ -1,22 +1,38 @@
extends Node
const MultiplayerClient = preload("multiplayer_client.gd")
# Declare member variables here. Examples:
# var a = 2
# var b = "text"
onready var client = MultiplayerClient.new()
const MULTIPLAYER_URL = "ws://localhost:9080"
# Called when the node enters the scene tree for the first time.
func _ready():
pass # Replace with function body.
# Called every frame. 'delta' is the elapsed time since the previous frame.
#func _process(delta):
# pass
client.connect("lobby_joined", self, "_lobby_joined")
client.connect("connected", self, "_connected")
add_child(client)
func start_singleplayer_game():
print("Starting singleplayer game...")
get_tree().change_scene("res://game.tscn")
func join_lobby():
get_tree().change_scene("res://join_lobby.tscn")
func create_lobby():
join_lobby_with_code("")
func _lobby_joined(_lobby):
print("Joined!")
goto_lobby()
func goto_lobby():
print("Going to lobby...")
get_tree().change_scene("res://lobby.tscn")
func leave_game():
client.stop()
print("Leaving game...")
get_tree().change_scene("res://main.tscn")
func join_lobby_with_code(code):
client.start(MULTIPLAYER_URL, code)

21
join_lobby.gd Normal file
View file

@ -0,0 +1,21 @@
extends Control
# Declare member variables here. Examples:
# var a = 2
# var b = "text"
# Called when the node enters the scene tree for the first time.
func _ready():
pass # Replace with function body.
# Called every frame. 'delta' is the elapsed time since the previous frame.
#func _process(delta):
# pass
func _on_Button_pressed():
print("Joining with ", $TextEdit.text)
Global.join_lobby_with_code($TextEdit.text)

36
join_lobby.tscn Normal file
View file

@ -0,0 +1,36 @@
[gd_scene load_steps=2 format=2]
[ext_resource path="res://join_lobby.gd" type="Script" id=1]
[node name="Control" type="Control"]
anchor_right = 1.0
anchor_bottom = 1.0
script = ExtResource( 1 )
__meta__ = {
"_edit_use_anchors_": false
}
[node name="Label" type="Label" parent="."]
margin_left = 235.0
margin_top = 74.0
margin_right = 275.0
margin_bottom = 88.0
text = "Lobby Code"
[node name="TextEdit" type="TextEdit" parent="."]
margin_left = 239.0
margin_top = 99.0
margin_right = 544.0
margin_bottom = 139.0
__meta__ = {
"_edit_use_anchors_": false
}
[node name="Button" type="Button" parent="."]
margin_left = 320.0
margin_top = 149.0
margin_right = 451.0
margin_bottom = 169.0
text = "Join"
[connection signal="pressed" from="Button" to="." method="_on_Button_pressed"]

15
lobby.gd Normal file
View file

@ -0,0 +1,15 @@
extends Node2D
func _ready():
$MarginContainer/Label.text = Global.client.lobby
pass
func _draw():
pass
func _process(_delta):
pass
func _on_Button_pressed():
Global.leave_game()

26
lobby.tscn Normal file
View file

@ -0,0 +1,26 @@
[gd_scene load_steps=2 format=2]
[ext_resource path="res://lobby.gd" type="Script" id=1]
[node name="Node2D" type="Node2D"]
script = ExtResource( 1 )
[node name="MarginContainer" type="VBoxContainer" parent="."]
margin_right = 669.0
margin_bottom = 366.0
[node name="Label" type="Label" parent="MarginContainer"]
margin_right = 669.0
margin_bottom = 14.0
autowrap = true
__meta__ = {
"_edit_use_anchors_": false
}
[node name="Button" type="Button" parent="MarginContainer"]
margin_top = 18.0
margin_right = 669.0
margin_bottom = 38.0
text = "Close Lobby"
[connection signal="pressed" from="MarginContainer/Button" to="." method="_on_Button_pressed"]

10
main.gd
View file

@ -1,4 +1,12 @@
extends Node
func _on_Singleplayer_pressed():
Global.start_singleplayer_game()
func _on_CreateLobbyButton_pressed():
Global.create_lobby()
func _on_JoinLobbyButton_pressed():
Global.join_lobby()

View file

@ -48,3 +48,5 @@ __meta__ = {
[node name="Server" type="Node" parent="."]
[connection signal="pressed" from="VBoxContainer/Singleplayer" to="." method="_on_Singleplayer_pressed"]
[connection signal="pressed" from="VBoxContainer/CreateLobbyButton" to="." method="_on_CreateLobbyButton_pressed"]
[connection signal="pressed" from="VBoxContainer/JoinLobbyButton" to="." method="_on_JoinLobbyButton_pressed"]

View file

@ -14,7 +14,7 @@ const STR_ONLY_HOST_CAN_SEAL = "Only host can seal the lobby";
const STR_SEAL_COMPLETE = "Seal complete";
const STR_TOO_MANY_LOBBIES = "Too many lobbies open, disconnecting";
const STR_ALREADY_IN_LOBBY = "Already in a lobby";
const STR_LOBBY_DOES_NOT_EXISTS = "Lobby does not exists";
const STR_LOBBY_DOES_NOT_EXIST = "Lobby does not exist";
const STR_LOBBY_IS_SEALED = "Lobby is sealed";
const STR_INVALID_FORMAT = "Invalid message format";
const STR_NEED_LOBBY = "Invalid message when not in a lobby";
@ -70,7 +70,7 @@ class Peer {
console.log(`Open lobbies: ${lobbies.size}`);
}
const lobby = lobbies.get(lobbyName);
if (!lobby) throw new ProtoError(4000, STR_LOBBY_DOES_NOT_EXISTS);
if (!lobby) throw new ProtoError(4000, STR_LOBBY_DOES_NOT_EXIST);
if (lobby.sealed) throw new ProtoError(4000, STR_LOBBY_IS_SEALED);
this.lobby = lobbyName;
console.log(

View file

@ -57,6 +57,7 @@ func _parse_msg():
return
if type.begins_with("J: "):
print("Emitting lobby_joined")
emit_signal("lobby_joined", type.substr(3, type.length() - 3))
return
elif type.begins_with("S: "):
@ -70,18 +71,22 @@ func _parse_msg():
var src_id: int = int(src_str)
if type.begins_with("I: "):
print("Emitting connected")
emit_signal("connected", src_id)
elif type.begins_with("N: "):
# Client connected
print("Emitting peer_connected")
emit_signal("peer_connected", src_id)
elif type.begins_with("D: "):
# Client connected
emit_signal("peer_disconnected", src_id)
elif type.begins_with("O: "):
# Offer received
print("Emitting offer_received")
emit_signal("offer_received", src_id, req[1])
elif type.begins_with("A: "):
# Answer received
print("Emitting answer_received")
emit_signal("answer_received", src_id, req[1])
elif type.begins_with("C: "):
# Candidate received
@ -90,10 +95,11 @@ func _parse_msg():
return
if not candidate[1].is_valid_integer():
return
print("Emitting candidate_received")
emit_signal("candidate_received", src_id, candidate[0], int(candidate[1]), candidate[2])
func join_lobby(lobby):
return client.get_peer(1).put_packet(("J: %s\n" % lobby).to_utf8())
func join_lobby(joined_lobby):
return client.get_peer(1).put_packet(("J: %s\n" % joined_lobby).to_utf8())
func seal_lobby():
return client.get_peer(1).put_packet("S: \n".to_utf8())
@ -110,7 +116,7 @@ func send_answer(id, answer) -> int:
func _send_msg(type, id, data) -> int:
return client.get_peer(1).put_packet(("%s: %d\n%s" % [type, id, data]).to_utf8())
func _process(delta):
func _process(_delta):
var status: int = client.get_connection_status()
if status == WebSocketClient.CONNECTION_CONNECTING or status == WebSocketClient.CONNECTION_CONNECTED:
client.poll()