necra-prototypes/dialog/dialog_map.gd
Antonio Dell'Annunziata 2cb41beef2
All checks were successful
/ test (push) Successful in 2s
Add choices to dialogs
2025-03-28 07:34:57 +01:00

109 lines
4.1 KiB
GDScript

class_name DialogMap
extends Node2D
signal dialog_finished(choice_selected: String)
const SPEED_SLOW = 10.0
const SPEED_NORMAL = 15.0
const SPEED_FAST = 20.0
@export var map_point_number = 0
var text_box_label: RichTextLabel
var choice_1_button: Button
var choice_2_button: Button
var dialog_text: Array[Dictionary]
var current_line = 0
var is_choice_needed = false
func _ready():
_setup_text()
text_box_label.visible = true
text_box_label.text = ""
choice_1_button.visible = false
choice_2_button.visible = false
next_line()
func _close_dialog(choice_selected: String = ""):
dialog_finished.emit(choice_selected)
text_box_label.text = ""
text_box_label.hide()
choice_1_button.hide()
choice_2_button.hide()
is_choice_needed = false
func _setup_text():
match map_point_number:
0:
dialog_text = [
{speaker = "Necra", text = "A small path leads away from the crypt."},
{speaker = "Necra", text = "I follow it slowly into the dark forest."},
{speaker = "Necra", text = "After some time on the path I see a strange tree in full blossom."},
{speaker = "Necra", text = "Strange, everything else seems dead and decaying."},
{speaker = "Necra", text = "But this tree with it's blood red foliage defies the rest."},
{speaker = "Necra", text = "I notice a body sitting on the ground, laying against the massive stem amongs the tentacle like roots."},
{speaker = "Necra", text = "What should I do?", choices = ["Approach body", "Go back"]},
]
1:
dialog_text = [
{speaker = "Necra", text = "The path continues leading me away from the tree."},
{speaker = "Necra", text = "It gets less muddy and eventually turns into a poorly maintained brick road."},
{speaker = "Necra", text = "The road ends at a tower."},
{speaker = "Necra", text = "Something is dangling from the roof, it's hard to make out through the tree tops."},
{speaker = "Necra", text = "I can either go up to the rooftop, or continue deeper into the woods."},
]
2:
dialog_text = [
{speaker = "Necra", text = "I enter the tower ruins."},
{speaker = "Necra", text = "A stone staircase leads up the side of the small room."},
{speaker = "Necra", text = "I have to be careful climbing up, some stairs are missing."},
{speaker = "Necra", text = "On the roof I see an old wooden construct, a rope attached to it."},
{speaker = "Necra", text = "The rope holds a body at the neck over the steep descend."},
{speaker = "Necra", text = "Branches of dead trees seem to stretch toward it, trying to reach it desperately."},
]
3:
dialog_text = [
{speaker = "Necra", text = "The forest is way harder to pass now, without a discernible path."},
{speaker = "Necra", text = "I need all my strength to fight through the undergrowth."},
{speaker = "Necra", text = "Suddenly it clears into a small glade."},
{speaker = "Necra", text = "A headless body lies in the middle."},
]
func _unhandled_input(event):
if event is InputEventKey and not is_choice_needed:
if event.key_label == KEY_SPACE and event.is_released():
next_line()
var tween: Tween
func next_line():
if not text_box_label.visible: return
if current_line >= dialog_text.size():
_close_dialog()
return
var next = dialog_text[current_line]
text_box_label.text = "%s: %s" % [next["speaker"], next["text"]]
text_box_label.visible_characters = next["speaker"].length() + 2
var remaining_characters = text_box_label.text.length() - text_box_label.visible_characters
var tween_duration = remaining_characters / (next["speed"] if next.has("speed") else SPEED_NORMAL)
if tween:
tween.kill()
tween = create_tween()
tween.tween_property(text_box_label, "visible_characters", text_box_label.text.length(), tween_duration)
if "choices" in next:
is_choice_needed = true
tween.tween_callback(_show_choices.bind(next["choices"]))
current_line += 1
func _show_choices(choices: Array):
choice_1_button.text = choices[0]
choice_1_button.visible = true
choice_1_button.pressed.connect(_close_dialog.bind(choices[0]))
choice_2_button.text = choices[1]
choice_2_button.visible = true
choice_2_button.pressed.connect(_close_dialog.bind(choices[1]))
is_choice_needed = true