diff --git a/Scenes/Lobby.tscn b/Scenes/Lobby.tscn new file mode 100644 index 0000000..f2d59cf --- /dev/null +++ b/Scenes/Lobby.tscn @@ -0,0 +1,22 @@ +[gd_scene load_steps=2 format=2] + +[ext_resource path="res://Scripts/Lobby.gd" type="Script" id=1] + +[node name="Lobby" type="Control"] +anchor_right = 1.0 +anchor_bottom = 1.0 +script = ExtResource( 1 ) +__meta__ = { +"_edit_use_anchors_": false +} + +[node name="ItemList" type="ItemList" parent="."] +anchor_left = 0.0449219 +anchor_top = 0.125 +anchor_right = 0.254477 +anchor_bottom = 0.955 +margin_right = -27.584 +margin_bottom = -62.0 +__meta__ = { +"_edit_use_anchors_": true +} diff --git a/Scripts/Lobby.gd b/Scripts/Lobby.gd new file mode 100644 index 0000000..d62d449 --- /dev/null +++ b/Scripts/Lobby.gd @@ -0,0 +1,14 @@ +extends Control + +func _init(): + WebsocketController.connect("hostjoin", self, "_on_hostjoin") + +func _ready(): + WebsocketController.connect_websocket() + +func _on_hostjoin(): + var location = JavaScript.get_interface("location") + if location and location.hash != "": + WebsocketController.join_game(location.hash) + else: + WebsocketController.host_game() diff --git a/Scripts/MainMenu.gd b/Scripts/MainMenu.gd index fd81aeb..7687a8c 100644 --- a/Scripts/MainMenu.gd +++ b/Scripts/MainMenu.gd @@ -1,7 +1,14 @@ extends Control +onready var Location = JavaScript.get_interface("location") + func _ready(): + if Location and Location.hash != "": + ## TODO + # join_room(location.hash) + pass $Buttons/Host.connect("pressed", self, "host_pressed") func host_pressed(): - get_tree().change_scene("res://Scenes/Main.tscn") + if Location: Location.hash = "#blarg" + get_tree().change_scene("res://Scenes/Lobby.tscn") diff --git a/Scripts/WebsocketController.gd b/Scripts/WebsocketController.gd new file mode 100644 index 0000000..857e0b9 --- /dev/null +++ b/Scripts/WebsocketController.gd @@ -0,0 +1,38 @@ +extends Node + +var client = WebSocketClient.new() +var peer + +signal hostjoin + +func _init(): + client.connect("connection_established", self, "_on_connect") + client.connect("data_received", self, "_on_data") + +func _on_connect(_proto): + peer = client.get_peer(1) + peer.set_write_mode(WebSocketPeer.WRITE_MODE_TEXT) + +func _on_data(): + var msg = peer.get_packet().get_string_from_utf8() + var regex = RegEx.new() + regex.compile("([\\w_]+):") + var res = regex.search(msg) + match res.strings[1]: + "HOSTJOIN": + emit_signal("hostjoin") + +func _process(_delta): + client.poll() + +func send_message(message): + peer.put_packet(message.to_utf8()) + +func connect_websocket(): + client.connect_to_url(ProjectSettings["global/server_url"]); + +func host_game(): + send_message("HOST:") + +func join_game(hash_): + send_message("JOIN: %s" % hash_) diff --git a/project.godot b/project.godot index 049f428..88406f4 100644 --- a/project.godot +++ b/project.godot @@ -33,16 +33,17 @@ _global_script_class_icons={ [application] config/name="Hexland" -run/main_scene="res://Scenes/MainMenu.tscn" +run/main_scene="res://Scenes/Lobby.tscn" config/icon="res://icon.png" [autoload] +WebsocketController="*res://Scripts/WebsocketController.gd" Debug="*res://Scripts/Debug.gd" [global] -main=false +server_url="127.0.0.1:8080" [gui]