nomadro/apps/minigame-godot/scripts/hud.gd
Eric 58a845db32 Add WeChat minigame triple-engine scaffold with Godot Chiang Mai plaza SoT.
Unity and Cocos stay as parallel schemes; apps/minigame ships the Godot 4.7 WeChat pack for DevTools.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-11-06 20:17:14 +08:00

111 lines
3.3 KiB
GDScript

extends CanvasLayer
@onready var _stick_base: Control = $Touch/Joystick
@onready var _stick_knob: Control = $Touch/Joystick/Knob
@onready var _hint: Label = $Hint
@onready var _title: Label = $Title
@onready var _poi: Label = $PoiToast
@onready var _quest: Label = $Quest
@onready var _toast: Label = $Toast
var _dragging := false
var _player: Node
var _poi_tween: Tween
var _toast_tween: Tween
func _ready() -> void:
_player = get_tree().get_first_node_in_group("player")
if _title:
_title.text = "NOMADRÓ"
if _hint:
_hint.text = "stick move · drag right look · walk into gold rings"
if _poi:
_poi.modulate.a = 0.0
if _toast:
_toast.modulate.a = 0.0
_reset_knob()
await get_tree().process_frame
for z in get_tree().get_nodes_in_group("poi"):
if z.has_signal("entered_poi"):
z.entered_poi.connect(_on_poi_enter)
z.exited_poi.connect(_on_poi_exit)
var game := get_tree().get_first_node_in_group("game")
if game:
if game.has_signal("quest_changed"):
game.quest_changed.connect(_on_quest)
if game.has_signal("toast"):
game.toast.connect(show_toast)
if game.has_method("_quest_text"):
_on_quest(game._quest_text())
func _on_quest(text: String) -> void:
if _quest:
_quest.text = text
func show_toast(text: String) -> void:
if not _toast:
return
_toast.text = text
if _toast_tween:
_toast_tween.kill()
_toast.modulate.a = 1.0
_toast_tween = create_tween()
_toast_tween.tween_interval(1.6)
_toast_tween.tween_property(_toast, "modulate:a", 0.0, 0.35)
func show_poi(title: String, blurb: String) -> void:
if not _poi:
return
_poi.text = "%s\n%s" % [title, blurb]
if _poi_tween:
_poi_tween.kill()
_poi_tween = create_tween()
_poi_tween.tween_property(_poi, "modulate:a", 1.0, 0.2)
func hide_poi() -> void:
if not _poi:
return
if _poi_tween:
_poi_tween.kill()
_poi_tween = create_tween()
_poi_tween.tween_property(_poi, "modulate:a", 0.0, 0.25)
func _on_poi_enter(title: String, blurb: String) -> void:
show_poi(title, blurb)
func _on_poi_exit(_title: String) -> void:
hide_poi()
func _process(_delta: float) -> void:
if _player and _player.has_method("set_joystick"):
var v := Vector2.ZERO
if _dragging:
var center := _stick_base.global_position + _stick_base.size * 0.5
var delta := _stick_knob.global_position + _stick_knob.size * 0.5 - center
var max_r := minf(_stick_base.size.x, _stick_base.size.y) * 0.35
v = (delta / max_r).limit_length(1.0)
v = Vector2(v.x, -v.y)
_player.set_joystick(v)
func _input(event: InputEvent) -> void:
if event is InputEventScreenTouch:
var t := event as InputEventScreenTouch
var base_rect := Rect2(_stick_base.global_position, _stick_base.size)
if t.pressed and base_rect.has_point(t.position):
_dragging = true
_move_knob(t.position)
elif not t.pressed:
_dragging = false
_reset_knob()
elif event is InputEventScreenDrag and _dragging:
_move_knob((event as InputEventScreenDrag).position)
func _move_knob(screen_pos: Vector2) -> void:
var center := _stick_base.global_position + _stick_base.size * 0.5
var max_r := minf(_stick_base.size.x, _stick_base.size.y) * 0.35
var delta := (screen_pos - center).limit_length(max_r)
_stick_knob.global_position = center + delta - _stick_knob.size * 0.5
func _reset_knob() -> void:
_stick_knob.position = (_stick_base.size - _stick_knob.size) * 0.5