Commit
This commit is contained in:
105
godot_poc/Main.gd
Normal file
105
godot_poc/Main.gd
Normal file
@@ -0,0 +1,105 @@
|
||||
extends Control
|
||||
|
||||
@onready var token_input = $VBoxContainer/HBoxContainer/TokenInput
|
||||
@onready var status_label = $VBoxContainer/ConnectionStatusLabel
|
||||
@onready var location_name_label = $VBoxContainer/LocationNameLabel
|
||||
@onready var location_image = $VBoxContainer/LocationImage
|
||||
@onready var location_desc_label = $VBoxContainer/LocationDescriptionLabel
|
||||
@onready var log_label = $VBoxContainer/LogLabel
|
||||
|
||||
var socket = WebSocketPeer.new()
|
||||
var http_request : HTTPRequest
|
||||
var is_connected_to_host = false
|
||||
|
||||
func _ready():
|
||||
log_message("Godot PoC Started")
|
||||
http_request = HTTPRequest.new()
|
||||
add_child(http_request)
|
||||
http_request.request_completed.connect(_on_image_request_completed)
|
||||
|
||||
func _process(delta):
|
||||
socket.poll()
|
||||
var state = socket.get_ready_state()
|
||||
|
||||
if state == WebSocketPeer.STATE_OPEN:
|
||||
if not is_connected_to_host:
|
||||
is_connected_to_host = true
|
||||
status_label.text = "Status: Connected"
|
||||
log_message("WebSocket Connected!")
|
||||
|
||||
while socket.get_available_packet_count():
|
||||
var packet = socket.get_packet()
|
||||
var data = packet.get_string_from_utf8()
|
||||
var json = JSON.new()
|
||||
var error = json.parse(data)
|
||||
if error == OK:
|
||||
handle_message(json.get_data())
|
||||
else:
|
||||
log_message("Error parsing JSON: " + data)
|
||||
|
||||
elif state == WebSocketPeer.STATE_CLOSED:
|
||||
if is_connected_to_host:
|
||||
is_connected_to_host = false
|
||||
status_label.text = "Status: Disconnected"
|
||||
log_message("WebSocket Disconnected")
|
||||
|
||||
func _on_connect_button_pressed():
|
||||
var token = token_input.text.strip_edges()
|
||||
if token == "":
|
||||
log_message("Please enter a token.")
|
||||
return
|
||||
|
||||
var url = "wss://api-staging.echoesoftheash.com/ws/game/" + token
|
||||
log_message("Connecting to: " + url)
|
||||
var err = socket.connect_to_url(url)
|
||||
if err != OK:
|
||||
log_message("Error connecting to URL: " + str(err))
|
||||
else:
|
||||
status_label.text = "Status: Connecting..."
|
||||
|
||||
func handle_message(msg):
|
||||
# log_message("Received: " + str(msg.get("type")))
|
||||
|
||||
if msg.get("type") == "location_update":
|
||||
var data = msg.get("data", {})
|
||||
var location = data.get("location", {})
|
||||
|
||||
if location:
|
||||
update_location_ui(location)
|
||||
|
||||
func update_location_ui(location):
|
||||
location_name_label.text = location.get("name", "Unknown Location")
|
||||
location_desc_label.text = location.get("description", "")
|
||||
|
||||
var image_url = location.get("image_url", "")
|
||||
if image_url != "":
|
||||
fetch_image(image_url)
|
||||
|
||||
func fetch_image(url):
|
||||
if url.begins_with("/"):
|
||||
url = "https://api-staging.echoesoftheash.com" + url
|
||||
|
||||
log_message("Fetching image: " + url)
|
||||
http_request.cancel_request()
|
||||
http_request.request(url)
|
||||
|
||||
func _on_image_request_completed(result, response_code, headers, body):
|
||||
if result == HTTPRequest.RESULT_SUCCESS:
|
||||
var image = Image.new()
|
||||
var error = image.load_png_from_buffer(body)
|
||||
if error != OK:
|
||||
error = image.load_jpg_from_buffer(body)
|
||||
if error != OK:
|
||||
error = image.load_webp_from_buffer(body)
|
||||
|
||||
if error == OK:
|
||||
var texture = ImageTexture.create_from_image(image)
|
||||
location_image.texture = texture
|
||||
else:
|
||||
log_message("Failed to load image texture")
|
||||
else:
|
||||
log_message("Failed to fetch image. Code: " + str(response_code))
|
||||
|
||||
func log_message(text):
|
||||
print(text)
|
||||
log_label.text += text + "\n"
|
||||
69
godot_poc/Main.tscn
Normal file
69
godot_poc/Main.tscn
Normal file
@@ -0,0 +1,69 @@
|
||||
[gd_scene load_steps=2 format=3 uid="uid://c8q7y6x5z4w3"]
|
||||
|
||||
[ext_resource type="Script" path="res://Main.gd" id="1_m4i3n"]
|
||||
|
||||
[node name="Main" type="Control"]
|
||||
layout_mode = 3
|
||||
anchors_preset = 15
|
||||
anchor_right = 1.0
|
||||
anchor_bottom = 1.0
|
||||
grow_horizontal = 2
|
||||
grow_vertical = 2
|
||||
script = ExtResource("1_m4i3n")
|
||||
|
||||
[node name="VBoxContainer" type="VBoxContainer" parent="."]
|
||||
layout_mode = 1
|
||||
anchors_preset = 15
|
||||
anchor_right = 1.0
|
||||
anchor_bottom = 1.0
|
||||
grow_horizontal = 2
|
||||
grow_vertical = 2
|
||||
|
||||
[node name="HeaderLabel" type="Label" parent="VBoxContainer"]
|
||||
layout_mode = 2
|
||||
text = "Echoes of the Ashes - Godot PoC"
|
||||
horizontal_alignment = 1
|
||||
|
||||
[node name="HBoxContainer" type="HBoxContainer" parent="VBoxContainer"]
|
||||
layout_mode = 2
|
||||
|
||||
[node name="TokenInput" type="LineEdit" parent="VBoxContainer/HBoxContainer"]
|
||||
layout_mode = 2
|
||||
size_flags_horizontal = 3
|
||||
placeholder_text = "Enter Auth Token"
|
||||
|
||||
[node name="ConnectButton" type="Button" parent="VBoxContainer/HBoxContainer"]
|
||||
layout_mode = 2
|
||||
text = "Connect via WebSocket"
|
||||
|
||||
[node name="ConnectionStatusLabel" type="Label" parent="VBoxContainer"]
|
||||
layout_mode = 2
|
||||
text = "Status: Disconnected"
|
||||
|
||||
[node name="HSeparator" type="HSeparator" parent="VBoxContainer"]
|
||||
layout_mode = 2
|
||||
|
||||
[node name="LocationNameLabel" type="Label" parent="VBoxContainer"]
|
||||
layout_mode = 2
|
||||
horizontal_alignment = 1
|
||||
|
||||
[node name="LocationImage" type="TextureRect" parent="VBoxContainer"]
|
||||
custom_minimum_size = Vector2(0, 300)
|
||||
layout_mode = 2
|
||||
expand_mode = 1
|
||||
stretch_mode = 5
|
||||
|
||||
[node name="LocationDescriptionLabel" type="RichTextLabel" parent="VBoxContainer"]
|
||||
layout_mode = 2
|
||||
fit_content = true
|
||||
|
||||
[node name="HSeparator2" type="HSeparator" parent="VBoxContainer"]
|
||||
layout_mode = 2
|
||||
|
||||
[node name="LogLabel" type="RichTextLabel" parent="VBoxContainer"]
|
||||
layout_mode = 2
|
||||
size_flags_vertical = 3
|
||||
text = "Logs will appear here...
|
||||
"
|
||||
|
||||
[connection signal="pressed" from="VBoxContainer/HBoxContainer/ConnectButton" to="." method="_on_connect_button_pressed"]
|
||||
1
godot_poc/icon.svg
Normal file
1
godot_poc/icon.svg
Normal file
@@ -0,0 +1 @@
|
||||
<svg height="128" width="128" xmlns="http://www.w3.org/2000/svg"><rect fill="#364966" height="128" width="128" rx="20" ry="20"/><path d="M64 16 L16 112 L112 112 Z" fill="#ffffff"/></svg>
|
||||
|
After Width: | Height: | Size: 187 B |
13
godot_poc/project.godot
Normal file
13
godot_poc/project.godot
Normal file
@@ -0,0 +1,13 @@
|
||||
config_version=5
|
||||
|
||||
[application]
|
||||
|
||||
config/name="Echoes of the Ashes PoC"
|
||||
config/features=PackedStringArray("4.5", "Forward Plus")
|
||||
run/main_scene="res://Main.tscn"
|
||||
config/icon="res://icon.svg"
|
||||
|
||||
[display]
|
||||
|
||||
window/size/viewport_width=1280
|
||||
window/size/viewport_height=720
|
||||
Reference in New Issue
Block a user