You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

50 lines
1.4 KiB
GDScript

extends Node
# Lemonland Game Window
onready var ui = $UI
export var websocket_url = "ws://127.0.0.1:8080"
var _client = WebSocketClient.new()
func _ready():
TranslationServer.set_locale("en")
for text in get_tree().get_nodes_in_group("translate"):
text.translate()
_client.connect("connection_closed", self, "_closed")
_client.connect("connection_error", self, "_error")
_client.connect("connection_established", self, "_connected")
_client.connect("data_received", self, "_on_data_received")
var err = _client.connect_to_url(websocket_url)
if err != OK:
print_debug("Unable to connect")
set_process(false)
func _error(was_clean = false):
print_debug("Error, clean: ", was_clean)
set_process(false)
func _closed(was_clean = false):
print_debug("Closed, clean: ", was_clean)
set_process(false)
func _connected(protocol = ""):
print_debug("Connected with protocol: ", protocol)
_client.get_peer(1).put_packet(JSON.print({'type': 'test', 'message': "Game test packet"}).to_utf8())
func _on_data_received():
var json = JSON.parse(_client.get_peer(1).get_packet().get_string_from_utf8())
var packet = json.result
if typeof(packet) != 18:
push_error("%s is not a dictionary" % [packet])
get_tree().quit()
print_debug("Got data from server: ", packet)
if packet['type'] == "view":
ui.change_view(packet)
elif packet['type'] == "portrait":
ui.change_portrait(packet)
func _process(_delta):
_client.poll()