diff --git a/Assets/star.png b/Assets/star.png new file mode 100644 index 0000000..7d7ffd4 Binary files /dev/null and b/Assets/star.png differ diff --git a/Assets/star.png.import b/Assets/star.png.import new file mode 100644 index 0000000..f64ce82 --- /dev/null +++ b/Assets/star.png.import @@ -0,0 +1,35 @@ +[remap] + +importer="texture" +type="StreamTexture" +path="res://.import/star.png-59a17f0ff6ffca9363606061d3940469.stex" +metadata={ +"vram_texture": false +} + +[deps] + +source_file="res://Assets/star.png" +dest_files=[ "res://.import/star.png-59a17f0ff6ffca9363606061d3940469.stex" ] + +[params] + +compress/mode=0 +compress/lossy_quality=0.7 +compress/hdr_mode=0 +compress/bptc_ldr=0 +compress/normal_map=0 +flags/repeat=0 +flags/filter=true +flags/mipmaps=false +flags/anisotropic=false +flags/srgb=2 +process/fix_alpha_border=true +process/premult_alpha=false +process/HDR_as_SRGB=false +process/invert_color=false +process/normal_map_invert_y=false +stream=false +size_limit=0 +detect_3d=true +svg/scale=1.0 diff --git a/Scenes/Lobby.tscn b/Scenes/Lobby.tscn index f2d59cf..b56a28c 100644 --- a/Scenes/Lobby.tscn +++ b/Scenes/Lobby.tscn @@ -10,13 +10,43 @@ __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 +[node name="Chat" type="Control" parent="."] +anchor_left = 0.80957 +anchor_top = 0.265 +anchor_right = 0.80957 +anchor_bottom = 0.265 +margin_right = 40.0 +margin_bottom = 40.0 __meta__ = { "_edit_use_anchors_": true } + +[node name="TextEdit" type="TextEdit" parent="Chat"] +margin_left = -59.0 +margin_top = -98.0 +margin_right = 143.0 +margin_bottom = 280.0 +readonly = true +__meta__ = { +"_edit_use_anchors_": false +} + +[node name="LineEdit" type="LineEdit" parent="Chat"] +margin_left = -59.0 +margin_top = 287.0 +margin_right = 142.0 +margin_bottom = 316.0 +__meta__ = { +"_edit_use_anchors_": false +} + +[node name="CopyBtn" type="Button" parent="."] +margin_left = 48.0 +margin_top = 33.0 +margin_right = 168.0 +margin_bottom = 53.0 +disabled = true +text = "Copy Room Code" + +[connection signal="text_entered" from="Chat/LineEdit" to="." method="_on_LineEdit_text_entered"] +[connection signal="pressed" from="CopyBtn" to="." method="_on_CopyBtn_pressed"] diff --git a/Scripts/Lobby.gd b/Scripts/Lobby.gd index d62d449..38a7951 100644 --- a/Scripts/Lobby.gd +++ b/Scripts/Lobby.gd @@ -1,14 +1,36 @@ extends Control +onready var location = JavaScript.get_interface("location") + func _init(): - WebsocketController.connect("hostjoin", self, "_on_hostjoin") + WebsocketController.connect("connected", self, "_on_connected") + WebsocketController.connect("join_ok", self, "_on_join_ok") + WebsocketController.connect("room_code", self, "_on_room_code") + WebsocketController.connect("chat", self, "_on_chat") func _ready(): WebsocketController.connect_websocket() -func _on_hostjoin(): - var location = JavaScript.get_interface("location") +func _on_connected(): if location and location.hash != "": - WebsocketController.join_game(location.hash) + WebsocketController.join_game(location.hash.trim_prefix("#")) else: WebsocketController.host_game() + +func _on_room_code(code): + location.hash = code + $CopyBtn.disabled = false + +func _on_join_ok(): + $CopyBtn.disabled = false + +func _on_chat(msg): + $Chat/TextEdit.text += msg + $Chat/TextEdit.text += "\n" + +func _on_CopyBtn_pressed(): + OS.clipboard = location.href + +func _on_LineEdit_text_entered(text): + WebsocketController.chat(text) + $Chat/LineEdit.clear() diff --git a/Scripts/Message.gd b/Scripts/Message.gd new file mode 100644 index 0000000..cec7e74 --- /dev/null +++ b/Scripts/Message.gd @@ -0,0 +1,29 @@ +extends Object + +class_name Message + +var command: String +var args: Array + +func _init(text: String = ""): + if text == "": + command = "" + args = [] + return + var match_re = RegEx.new(); + # r"((\\,|[^,])+)" + match_re.compile("(\\\\,|[^,])+") + + var radix = text.find(':') + command = text.substr(0, radix) + var args_raw = text.substr(radix+1) + args = [] + for match_ in match_re.search_all(args_raw): + var arg = match_.strings[0].strip_edges().replace("\\,", ",") + args.push_back(arg) + +func _to_string(): + var text = "%s:" % self.command + for arg in self.args: + text += arg.replace(",", "\\,") + return text diff --git a/Scripts/WebsocketController.gd b/Scripts/WebsocketController.gd index 857e0b9..e46834a 100644 --- a/Scripts/WebsocketController.gd +++ b/Scripts/WebsocketController.gd @@ -1,38 +1,56 @@ extends Node var client = WebSocketClient.new() -var peer +var ws -signal hostjoin +signal connected +signal chat +signal room_code +signal join_ok 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) + ws = client.get_peer(1) + ws.set_write_mode(WebSocketPeer.WRITE_MODE_TEXT) + emit_signal("connected") 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") + var msg = Message.new(ws.get_packet().get_string_from_utf8()) + match msg.command: + "CHAT": + emit_signal("chat", msg.args[0]) + "ROOM_CODE": + emit_signal("room_code", msg.args[0]) + "JOIN_OK": + emit_signal("join_ok") + _: + print("Unknown command => ", msg) func _process(_delta): client.poll() func send_message(message): - peer.put_packet(message.to_utf8()) + ws.put_packet(str(message).to_utf8()) func connect_websocket(): client.connect_to_url(ProjectSettings["global/server_url"]); func host_game(): - send_message("HOST:") + var msg = Message.new() + msg.command = "HOST" + send_message(msg) func join_game(hash_): - send_message("JOIN: %s" % hash_) + var msg = Message.new() + msg.command = "JOIN" + msg.args.push_back(hash_) + send_message(msg) + +func chat(text): + var msg = Message.new() + msg.command = "CHAT" + msg.args.push_back(text) + send_message(msg) diff --git a/project.godot b/project.godot index 88406f4..5e64016 100644 --- a/project.godot +++ b/project.godot @@ -23,11 +23,17 @@ _global_script_classes=[ { "class": "Hex", "language": "GDScript", "path": "res://Scripts/Hex.gd" +}, { +"base": "Object", +"class": "Message", +"language": "GDScript", +"path": "res://Scripts/Message.gd" } ] _global_script_class_icons={ "Board": "", "Chit": "", -"Hex": "" +"Hex": "", +"Message": "" } [application]