diff options
| author | Jan Tuomi <jan.tuomi@valuemotive.com> | 2020-08-08 21:56:41 +0300 |
|---|---|---|
| committer | Jan Tuomi <jan.tuomi@valuemotive.com> | 2020-08-08 21:56:41 +0300 |
| commit | 5718149342cd9f81f37bd9077aa8fff272c61e9b (patch) | |
| tree | a6871cf6801ab3786d775438bccc4b46e8571ddc | |
| parent | bc72e9a407db4fcc830450c112de90f5d1c251a9 (diff) | |
Implement dialogue files
| -rw-r--r-- | data/dialogue/dwarf_1.json | 3 | ||||
| -rw-r--r-- | data/dialogue/dwarf_2.json | 4 | ||||
| -rw-r--r-- | scenes/Level1.tscn | 1 | ||||
| -rw-r--r-- | scenes/Level2.tscn | 1 | ||||
| -rw-r--r-- | scripts/DwarfNPC.gd | 25 | ||||
| -rw-r--r-- | scripts/PlayerCharacter.gd | 2 |
6 files changed, 27 insertions, 9 deletions
diff --git a/data/dialogue/dwarf_1.json b/data/dialogue/dwarf_1.json new file mode 100644 index 0000000..91c7a5e --- /dev/null +++ b/data/dialogue/dwarf_1.json @@ -0,0 +1,3 @@ +[ + { "name": "Dwarf", "body": "Top of the morning to ye." } +] diff --git a/data/dialogue/dwarf_2.json b/data/dialogue/dwarf_2.json new file mode 100644 index 0000000..89d039f --- /dev/null +++ b/data/dialogue/dwarf_2.json @@ -0,0 +1,4 @@ +[ + { "name": "Dwarf", "body": "What are you doing in this dungeon?" }, + { "name": "Dwarf", "body": "Go back to the surface." } +] diff --git a/scenes/Level1.tscn b/scenes/Level1.tscn index 36517cf..d0d3d7b 100644 --- a/scenes/Level1.tscn +++ b/scenes/Level1.tscn @@ -357,6 +357,7 @@ shadow_filter = 1 ] instance=ExtResource( 3 )] position = Vector2( 51.6174, 19.3117 ) target = NodePath("../PlayerCharacter") +dialogue_file = "res://data/dialogue/dwarf_1.json" [node name="Ladder" parent="YSort" instance=ExtResource( 6 )] target_scene_num = 2 diff --git a/scenes/Level2.tscn b/scenes/Level2.tscn index b24d11b..e1253ce 100644 --- a/scenes/Level2.tscn +++ b/scenes/Level2.tscn @@ -358,6 +358,7 @@ shadow_filter = 1 ] instance=ExtResource( 2 )] position = Vector2( 17.3501, 55.8153 ) target = NodePath("../../../Scene2/YSort/PlayerCharacter") +dialogue_file = "res://data/dialogue/dwarf_2.json" [node name="Ladder" parent="YSort" instance=ExtResource( 3 )] position = Vector2( 88, 48 ) diff --git a/scripts/DwarfNPC.gd b/scripts/DwarfNPC.gd index 8fa7b24..b6b8848 100644 --- a/scripts/DwarfNPC.gd +++ b/scripts/DwarfNPC.gd @@ -2,17 +2,27 @@ extends KinematicBody2D class_name DwarfNPC export (NodePath) var target +export (String, FILE, "*.json") var dialogue_file onready var target_node = get_node(target) +var dialogue_data = [] -# Declare member variables here. Examples: -# var a = 2 -# var b = "text" - +func load_text_file(path): + var f = File.new() + var err = f.open(path, File.READ) + if err != OK: + printerr("Could not open file, error code ", err) + return "" + var text = f.get_as_text() + f.close() + return text # Called when the node enters the scene tree for the first time. func _ready(): - pass # Replace with function body. - + var dialogue_txt = load_text_file(dialogue_file) + var dialogue_result = JSON.parse(dialogue_txt) + if dialogue_result.error != OK: + return push_error(dialogue_result.error_string) + dialogue_data = dialogue_result.result # Called every frame. 'delta' is the elapsed time since the previous frame. func _process(_delta): @@ -23,5 +33,4 @@ func _process(_delta): $Sprite.set_scale(Vector2(dir, 1)) func get_dialogue(): - return [{ "name": "Dwarf", "body": "Hello, I'm a dwarf." }, - { "name": "Dwarf", "body": "How's it going?" }] + return dialogue_data diff --git a/scripts/PlayerCharacter.gd b/scripts/PlayerCharacter.gd index 15b3421..8e2b83a 100644 --- a/scripts/PlayerCharacter.gd +++ b/scripts/PlayerCharacter.gd @@ -40,7 +40,7 @@ func update_input_and_movement(): func _process(_delta): # Update walking wobble - var is_moving = velocity.length_squared() > .0 + var is_moving = velocity.length_squared() > .0 and dialogue_state != DialogueState.ACTIVE if is_moving: var time = OS.get_system_time_msecs() / 1000.0 var rot = 0.2 * sin(walk_wobble_coeff * time) |
