primitive working dialog bubble

This commit is contained in:
2022-12-01 20:25:36 +01:00
parent f093d3c0b5
commit c4500b09f4
7 changed files with 33 additions and 25 deletions

View File

@@ -1,12 +1,12 @@
{ {
"randomWelcomeText":[ "randomWelcomeText":[
"Oh hello, {player}... You look great! What do you want from me?", "Oh hello, {0}... You look great! What do you want from me?",
"Hey, {player}! Hope you have fun today!", "Hey, {0}! Hope you have fun today!",
"Hi! What's up {player}?" "Hi! What's up {0}?"
], ],
"debugAnswer": "This is a debug answer!", "debugAnswer": "This is a debug answer!",
"randomGoodbyeText":[ "randomGoodbyeText":[
"Was nice talking to you goodbye!", "Was nice talking to you goodbye!",
"Have a great day, {player}!" "Have a great day, {0}!"
] ]
} }

View File

@@ -26,16 +26,17 @@ anchor_left = 0.218
anchor_top = 0.812 anchor_top = 0.812
anchor_right = 0.837 anchor_right = 0.837
anchor_bottom = 0.972 anchor_bottom = 0.972
offset_left = 0.319977 offset_left = -15.066
offset_top = -19.64 offset_top = 8.43201
offset_right = 0.380005 offset_right = -16.469
offset_bottom = -4.84003 offset_bottom = -9.80798
grow_horizontal = 2 grow_horizontal = 2
grow_vertical = 0 grow_vertical = 0
theme_override_colors/font_color = Color(0, 0, 0, 1) theme_override_colors/font_color = Color(0, 0, 0, 1)
theme_override_fonts/font = ExtResource("1_vlvkg") theme_override_fonts/font = ExtResource("1_vlvkg")
theme_override_font_sizes/font_size = 32 theme_override_font_sizes/font_size = 32
text = "This is test text" text = "This is test text"
autowrap_mode = 3
[node name="NameLabel" type="Label" parent="."] [node name="NameLabel" type="Label" parent="."]
anchors_preset = -1 anchors_preset = -1
@@ -43,10 +44,10 @@ anchor_left = 0.164
anchor_top = 0.782 anchor_top = 0.782
anchor_right = 0.197 anchor_right = 0.197
anchor_bottom = 0.808 anchor_bottom = 0.808
offset_left = 0.259995 offset_left = 7.93199
offset_top = -0.430054 offset_top = 4.35199
offset_right = -0.39502 offset_right = 7.27698
offset_bottom = 0.0799561 offset_bottom = 4.888
theme_override_colors/font_color = Color(0, 0, 0, 1) theme_override_colors/font_color = Color(0, 0, 0, 1)
theme_override_fonts/font = ExtResource("1_vlvkg") theme_override_fonts/font = ExtResource("1_vlvkg")
theme_override_font_sizes/font_size = 16 theme_override_font_sizes/font_size = 16

View File

@@ -9,5 +9,7 @@
[node name="first_world" parent="." instance=ExtResource("2_x8nyb")] [node name="first_world" parent="." instance=ExtResource("2_x8nyb")]
[node name="player" parent="." instance=ExtResource("1_p5d84")] [node name="player" parent="." instance=ExtResource("1_p5d84")]
playerName = "vaporvee"
[node name="dialog_bubble" parent="." instance=ExtResource("3_ohogx")] [node name="dialog_bubble" parent="." instance=ExtResource("3_ohogx")]
visible = false

View File

@@ -19,6 +19,7 @@ shape = SubResource("RectangleShape2D_pchpi")
[node name="dialog_trigger_area" parent="." instance=ExtResource("2_blpmd")] [node name="dialog_trigger_area" parent="." instance=ExtResource("2_blpmd")]
dialogFile = "res://dialog/npcs/villager_normal.json" dialogFile = "res://dialog/npcs/villager_normal.json"
dialogTitle = "Talking Cupcake"
[node name="collision_shape_2d" parent="dialog_trigger_area" index="0"] [node name="collision_shape_2d" parent="dialog_trigger_area" index="0"]
shape = SubResource("RectangleShape2D_d78tr") shape = SubResource("RectangleShape2D_d78tr")

View File

@@ -6,10 +6,12 @@ public partial class dialog_trigger_area : Area2D
{ {
[Export(PropertyHint.File, "*json")] [Export(PropertyHint.File, "*json")]
string dialogFile; string dialogFile;
[Export]
public string currentKey = "randomWelcomeText"; string dialogTitle;
public void OnInteraction() public string printedDialog;
public void OnInteraction(string playerName)
{ {
string currentKey = "randomWelcomeText";
using var file = FileAccess.Open(dialogFile, FileAccess.ModeFlags.Read); using var file = FileAccess.Open(dialogFile, FileAccess.ModeFlags.Read);
string text = file.GetAsText(); string text = file.GetAsText();
@@ -17,7 +19,10 @@ public partial class dialog_trigger_area : Area2D
Dictionary allDialog = (Dictionary)jsonFile; Dictionary allDialog = (Dictionary)jsonFile;
if (currentKey.BeginsWith("random")) { if (currentKey.BeginsWith("random")) {
string[] dialogPart = allDialog[currentKey].AsStringArray(); string[] dialogPart = allDialog[currentKey].AsStringArray();
GD.Print(dialogPart[GD.Randi() % dialogPart.Length]); printedDialog = dialogPart[GD.Randi() % dialogPart.Length];
} else GD.Print(allDialog[currentKey]); } else printedDialog = allDialog[currentKey].AsString();
printedDialog = String.Format(printedDialog, playerName);
GetNode("/root/main/dialog_bubble").Call("ImportString",dialogTitle, printedDialog);
} }
} }

View File

@@ -3,13 +3,10 @@ using System;
public partial class dialog_bubble : CanvasLayer public partial class dialog_bubble : CanvasLayer
{ {
public override void _Ready() public void ImportString(string dialogTitle, string printedDialog)
{ {
Show(); Visible = !Visible;
ImportString(); GetNode<Label>("TextLabel").Text = printedDialog;
} GetNode<Label>("NameLabel").Text = dialogTitle;
public void ImportString()
{
} }
} }

View File

@@ -4,6 +4,8 @@ public partial class player : CharacterBody2D
{ {
[Export] [Export]
public int speed = 400; public int speed = 400;
[Export]
public string playerName;
public override void _PhysicsProcess(double delta) public override void _PhysicsProcess(double delta)
{ {
@@ -28,7 +30,7 @@ public partial class player : CharacterBody2D
//call event in raycasted object //call event in raycasted object
if (Input.IsActionJustPressed("ui_accept") && GetNode<RayCast2D>("ray_cast_2d").IsColliding()) { if (Input.IsActionJustPressed("ui_accept") && GetNode<RayCast2D>("ray_cast_2d").IsColliding()) {
var raycastedObject = GetNode<RayCast2D>("ray_cast_2d").GetCollider(); var raycastedObject = GetNode<RayCast2D>("ray_cast_2d").GetCollider();
raycastedObject.Call("OnInteraction"); raycastedObject.Call("OnInteraction", playerName);
} }
} }
} }