From 08ae1a42c694ec409921225daa59ab42fac75d58 Mon Sep 17 00:00:00 2001 From: Jan Tuomi Date: Sun, 9 Aug 2020 16:29:26 +0300 Subject: Allow options in dialogue --- data/dialogue/dwarf_1.json | 8 +++++--- data/dialogue/dwarf_2.json | 5 +++-- project.godot | 6 ++++++ scenes/DialogueOptionButton.tscn | 33 ++++++++++++++++++++++++++++++++ scenes/GUI.tscn | 41 ++++++++++++++++------------------------ scripts/DialogueBox.gd | 30 +++++++++++++++++++++++++++++ scripts/DwarfNPC.gd | 2 +- scripts/GUI.gd | 10 +++++----- scripts/Game.gd | 18 +++++++++++------- 9 files changed, 110 insertions(+), 43 deletions(-) create mode 100644 scenes/DialogueOptionButton.tscn create mode 100644 scripts/DialogueBox.gd diff --git a/data/dialogue/dwarf_1.json b/data/dialogue/dwarf_1.json index 91c7a5e..d42ec8c 100644 --- a/data/dialogue/dwarf_1.json +++ b/data/dialogue/dwarf_1.json @@ -1,3 +1,5 @@ -[ - { "name": "Dwarf", "body": "Top of the morning to ye." } -] +{ + "1": { "name": "Dwarf", "body": "What are you doing in this dungeon?", "options": [["Just chilling", "2"], ["Nothing", "3"]] }, + "2": { "name": "Dwarf", "body": "Stop chilling and go back to the surface." }, + "3": { "name": "Dwarf", "body": "Right." } +} diff --git a/data/dialogue/dwarf_2.json b/data/dialogue/dwarf_2.json index 89d039f..98f0221 100644 --- a/data/dialogue/dwarf_2.json +++ b/data/dialogue/dwarf_2.json @@ -1,4 +1,5 @@ [ - { "name": "Dwarf", "body": "What are you doing in this dungeon?" }, - { "name": "Dwarf", "body": "Go back to the surface." } + { "id": 1, "name": "Dwarf", "body": "What are you doing in this dungeon?", "options": [["Just chilling", 2], ["Nothing", 3]] }, + { "id": 2, "name": "Dwarf", "body": "Stop chilling and go back to the surface." }, + { "id": 3, "name": "Dwarf", "body": "Right." } ] diff --git a/project.godot b/project.godot index 45854be..3a48bce 100644 --- a/project.godot +++ b/project.godot @@ -9,6 +9,11 @@ config_version=4 _global_script_classes=[ { +"base": "ColorRect", +"class": "DialogueBox", +"language": "GDScript", +"path": "res://scripts/DialogueBox.gd" +}, { "base": "KinematicBody2D", "class": "DwarfNPC", "language": "GDScript", @@ -30,6 +35,7 @@ _global_script_classes=[ { "path": "res://scripts/PlayerCharacter.gd" } ] _global_script_class_icons={ +"DialogueBox": "", "DwarfNPC": "", "GUI": "", "Game": "", diff --git a/scenes/DialogueOptionButton.tscn b/scenes/DialogueOptionButton.tscn new file mode 100644 index 0000000..033bbba --- /dev/null +++ b/scenes/DialogueOptionButton.tscn @@ -0,0 +1,33 @@ +[gd_scene load_steps=4 format=2] + +[sub_resource type="StyleBoxFlat" id=3] +bg_color = Color( 0.368627, 0.0352941, 0.313726, 1 ) +border_width_left = 6 +border_width_right = 6 + +[sub_resource type="StyleBoxFlat" id=2] +bg_color = Color( 0.484375, 0.110941, 0.422868, 1 ) +border_width_left = 6 +border_width_right = 6 +border_color = Color( 0.486275, 0.109804, 0.423529, 1 ) + +[sub_resource type="StyleBoxFlat" id=1] +bg_color = Color( 0.368627, 0.0352941, 0.313726, 1 ) +border_width_left = 6 +border_width_right = 6 +border_color = Color( 0.368627, 0.0352941, 0.313726, 1 ) + +[node name="DialogueOptionButton" type="Button"] +margin_left = 88.0 +margin_right = 128.0 +margin_bottom = 20.0 +rect_min_size = Vector2( 40, 20 ) +size_flags_horizontal = 3 +custom_styles/hover = SubResource( 3 ) +custom_styles/pressed = SubResource( 2 ) +custom_styles/focus = SubResource( 3 ) +custom_styles/normal = SubResource( 1 ) +text = "Long text" +__meta__ = { +"_edit_use_anchors_": false +} diff --git a/scenes/GUI.tscn b/scenes/GUI.tscn index 46ad35e..01bf2ef 100644 --- a/scenes/GUI.tscn +++ b/scenes/GUI.tscn @@ -1,14 +1,8 @@ [gd_scene load_steps=4 format=2] [ext_resource path="res://scripts/GUI.gd" type="Script" id=1] - -[sub_resource type="Gradient" id=1] -offsets = PoolRealArray( 0 ) -colors = PoolColorArray( 0.458824, 0.0470588, 0.501961, 1 ) - -[sub_resource type="GradientTexture" id=2] -gradient = SubResource( 1 ) -width = 40 +[ext_resource path="res://scripts/DialogueBox.gd" type="Script" id=2] +[ext_resource path="res://scenes/DialogueOptionButton.tscn" type="PackedScene" id=3] [node name="GUI" type="MarginContainer"] anchor_right = 1.0 @@ -42,10 +36,11 @@ margin_right = 984.0 margin_bottom = 560.0 rect_min_size = Vector2( 0, 40 ) color = Color( 0.403922, 0.360784, 0.54902, 1 ) +script = ExtResource( 2 ) [node name="Name" type="Label" parent="BottomRow/DialogueBox"] -anchor_top = -0.00774612 -anchor_bottom = -0.00774612 +anchor_left = 0.02 +anchor_right = 0.02 margin_left = 18.9 margin_top = 14.0836 margin_right = 84.9 @@ -56,9 +51,7 @@ __meta__ = { "_edit_use_anchors_": false } -[node name="Text" type="Label" parent="BottomRow/DialogueBox"] -anchor_top = -0.00774612 -anchor_bottom = -0.00774612 +[node name="Body" type="Label" parent="BottomRow/DialogueBox"] margin_left = 94.1902 margin_top = 14.0836 margin_right = 160.19 @@ -69,21 +62,19 @@ __meta__ = { "_edit_use_anchors_": false } -[node name="NextButton" type="TextureButton" parent="BottomRow/DialogueBox"] -anchor_left = 0.95 -anchor_right = 0.95 +[node name="HBoxContainer" type="HBoxContainer" parent="BottomRow/DialogueBox"] +anchor_left = 0.94 +anchor_right = 0.98 margin_top = 10.0 margin_bottom = 10.0 -rect_min_size = Vector2( 40, 20 ) -texture_normal = SubResource( 2 ) -expand = true +grow_horizontal = 0 +alignment = 2 __meta__ = { "_edit_use_anchors_": false } -[node name="Label" type="Label" parent="BottomRow/DialogueBox/NextButton"] -margin_left = 12.0 -margin_top = 3.0 -text = ">>" -valign = 1 -[connection signal="pressed" from="BottomRow/DialogueBox/NextButton" to="." method="_on_NextButton_pressed"] +[node name="DialogueOptionButton" parent="BottomRow/DialogueBox/HBoxContainer" instance=ExtResource( 3 )] +margin_left = 0.0 +margin_right = 71.0 +[connection signal="option_pressed" from="BottomRow/DialogueBox" to="." method="_on_DialogueBox_option_pressed"] +[connection signal="pressed" from="BottomRow/DialogueBox/HBoxContainer/DialogueOptionButton" to="BottomRow/DialogueBox" method="_on_DialogueOptionButton_pressed"] diff --git a/scripts/DialogueBox.gd b/scripts/DialogueBox.gd new file mode 100644 index 0000000..eb88ada --- /dev/null +++ b/scripts/DialogueBox.gd @@ -0,0 +1,30 @@ +extends ColorRect +class_name DialogueBox + +signal option_pressed(text, target) + +var btn_res = preload("res://scenes/DialogueOptionButton.tscn") + +func set_dialogue(name: String, body: String, options: Array): + $Name.set_text(name) + $Body.set_text(body) + + for child in $HBoxContainer.get_children(): + $HBoxContainer.remove_child(child) + + for option in options: + var opt_txt = option[0] + var opt_target = option[1] + + var btn: Button = btn_res.instance() + btn.set_text(opt_txt) + var err = btn.connect("pressed", self, "_on_DialogueOptionButton_pressed", [opt_txt, opt_target]) + if err: + return push_error(err) + $HBoxContainer.add_child(btn) + +func free_buttons(): + $HBoxContainer.call_deferred("queue_free") + +func _on_DialogueOptionButton_pressed(text: String, target): + emit_signal("option_pressed", text, target) diff --git a/scripts/DwarfNPC.gd b/scripts/DwarfNPC.gd index b6b8848..52565ba 100644 --- a/scripts/DwarfNPC.gd +++ b/scripts/DwarfNPC.gd @@ -4,7 +4,7 @@ class_name DwarfNPC export (NodePath) var target export (String, FILE, "*.json") var dialogue_file onready var target_node = get_node(target) -var dialogue_data = [] +var dialogue_data = {} func load_text_file(path): var f = File.new() diff --git a/scripts/GUI.gd b/scripts/GUI.gd index f885b10..0c8bf56 100644 --- a/scripts/GUI.gd +++ b/scripts/GUI.gd @@ -3,7 +3,7 @@ class_name GUI export (bool) var show_debug = true onready var debug_label = $TopRow/DebugLabel -signal dialogue_next() +signal dialogue_next(target) # Called when the node enters the scene tree for the first time. func _ready(): @@ -17,11 +17,11 @@ func _unhandled_input(event: InputEvent): if event is InputEventKey: if event.pressed and event.scancode == KEY_ESCAPE: get_tree().quit() - if event.pressed and event.is_action_pressed("ui_accept"): - emit_signal("dialogue_next") +# if event.pressed and event.is_action_pressed("ui_accept"): +# emit_signal("dialogue_next") func set_debug_text(text: String): debug_label.set_text(text) -func _on_NextButton_pressed(): - emit_signal("dialogue_next") +func _on_DialogueBox_option_pressed(_text, target): + emit_signal("dialogue_next", target) diff --git a/scripts/Game.gd b/scripts/Game.gd index 25edde0..d5f3bb5 100644 --- a/scripts/Game.gd +++ b/scripts/Game.gd @@ -52,20 +52,24 @@ func change_level(num: int): func _on_PlayerCharacter_initiate_dialogue(other: Node): in_dialogue_with = other - var dialogues = other.get_dialogue() + var dialogues: Dictionary = other.get_dialogue() $CanvasLayer/GUI/BottomRow/DialogueBox.set_visible(true) emit_signal("dialogue_begin") - for i in range(dialogues.size()): - var d = dialogues[i] + var id = "1" + while id != null: + var d: Dictionary = dialogues[String(id)] var name = d["name"] var body = d["body"] - $CanvasLayer/GUI/BottomRow/DialogueBox/Name.set_text(name) - $CanvasLayer/GUI/BottomRow/DialogueBox/Text.set_text(body) - print("[%s] %s" % [name, body]) - yield($CanvasLayer/GUI, "dialogue_next") + var options = d.get("options", [[">>", null]]) + $CanvasLayer/GUI/BottomRow/DialogueBox.set_dialogue(name, body, options) + print("[%s] \"%s\"" % [name, body]) + var next_target = yield($CanvasLayer/GUI, "dialogue_next") + id = next_target emit_signal("dialogue_next") in_dialogue_with = null $CanvasLayer/GUI/BottomRow/DialogueBox.set_visible(false) + $CanvasLayer/GUI/BottomRow/DialogueBox.free_buttons() emit_signal("dialogue_end") + -- cgit v1.3