From 9e9f6984a500ec6eab02fa651eaaf3472c281c2a Mon Sep 17 00:00:00 2001 From: Jan Tuomi Date: Thu, 4 Dec 2025 23:40:19 +0200 Subject: Improve dialogue --- dialogue.gd | 27 +++++++++++++++++++++++-- dialogues/test1.gd | 6 ++++-- docs/index.html | 2 +- docs/index.pck | Bin 4604796 -> 4606972 bytes docs/index.service.worker.js | 2 +- text_bubble.gd | 21 ++++++++++++++++---- text_bubble.tscn | 46 +++++++++++++++++++++++++++++-------------- 7 files changed, 79 insertions(+), 25 deletions(-) diff --git a/dialogue.gd b/dialogue.gd index f085ed8..311d5e2 100644 --- a/dialogue.gd +++ b/dialogue.gd @@ -1,11 +1,14 @@ class_name Dialogue extends Node -signal next +signal next(arg: Variant) @onready var parent: Node2D = get_parent() @onready var cam: GameCamera = parent.get_node("GameCamera") +enum Type { SAY, ASK } +var type: Type + func say(who: Object, text: String): var tb: TextBubble = preload("res://text_bubble.tscn").instantiate() tb.text = text @@ -16,9 +19,29 @@ func say(who: Object, text: String): tb.add_to_group("text_bubbles") parent.add_child(tb) + type = Type.SAY await next +func ask(who: Object, text: String, options: Array[String]) -> String: + var tb: TextBubble = preload("res://text_bubble.tscn").instantiate() + tb.text = text + tb.options = options + tb.connect("answered", _on_answered) + if who is Node2D: + tb.global_position = who.global_position + Vector2(0, -20) + else: + tb.global_position = cam.global_position + + tb.add_to_group("text_bubbles") + parent.add_child(tb) + type = Type.ASK + return await next + func _process(_delta: float): - if Input.is_action_just_pressed("action") or Input.is_action_just_pressed("jump"): + if type == Type.SAY and (Input.is_action_just_pressed("action") or Input.is_action_just_pressed("jump")): get_tree().call_group("text_bubbles", "queue_free") next.emit() + +func _on_answered(answer: String): + get_tree().call_group("text_bubbles", "queue_free") + next.emit(answer) diff --git a/dialogues/test1.gd b/dialogues/test1.gd index e7752b3..d411484 100644 --- a/dialogues/test1.gd +++ b/dialogues/test1.gd @@ -5,10 +5,12 @@ extends Dialogue @onready var npc: Node2D = parent.get_node("DialogueTestNPC") func _ready(): - player.request_state_init() + player.request_state_forced("DROP") await say(player, "hello") await say(npc, "well hi") + var answer = await ask(npc, "buy or sell?", ["buy", "sell"]) + await say(npc, "it's %s time" % answer) player.request_state_normal() - print("dialogue done") + print("dialogue done, answer %s" % answer) diff --git a/docs/index.html b/docs/index.html index d691280..f437c23 100644 --- a/docs/index.html +++ b/docs/index.html @@ -112,7 +112,7 @@ body {