way better dialogue system code

This commit is contained in:
2023-02-07 15:48:55 +01:00
parent 0190b6bdb2
commit 38bd6d47b9
2 changed files with 68 additions and 45 deletions

View File

@@ -1,29 +1,51 @@
{ {
"dialogType": "villager", "dialogType": "villager",
"playerbeginoptions": {
"talk": [
"I just wanna talk",
"What's up",
"Can we talk?"
],
"goaway": [
"Nevermind!",
"Nothing",
"Have a great day!"
]
},
"welcome": [ "welcome": [
"Oh hello, {player}... You look great! What do you want from me?", [
"Hey, {player}! Hope you have fun today! So what do you want?", "Oh hello, {player}... You look great! What can I do for you?",
"Hi! What's up {player}?" {
"I just wanna talk": true,
"Nevermind!": false
}
],
[
"Hey, {player}! Hope you have fun today! So what do you want?",
{
"What's up": true,
"Nothing": false
}
],
[
"Hi! What's up {player}?",
{
"Can we talk?": true,
"Have a great day!": false
}
]
], ],
"else": [ "else": [
"Do you need something else?", [
"So can i help you with something else?" "Do you need something else?",
{
"Can you tell me something else?": true,
"No, thanks!": false
}
],
[
"So can i help you with something else?",
{
"Tell me a bit more maybe": true,
"I'm good, thank you!": false
}
]
], ],
"goodbye": [ "goodbye": [
"Was nice talking to you goodbye!", [
"Have a great day, {player}!" "Was nice talking to you goodbye!"
],
[
"Have a great day, {player}!"
]
], ],
"tipp": [ "tipp": [
[ [

View File

@@ -1,15 +1,14 @@
using Godot; using Godot;
using Godot.Collections;
using System; using System;
using System.Collections;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
public partial class dialog_bubble : CanvasLayer public partial class dialog_bubble : CanvasLayer
{ {
public Variant parsedDlg; public Variant parsedDlg;
public ArrayList dlgLines = new ArrayList(); public Variant dlgLines;
public int dlgPointer = 0; public int dlgPointer = 0;
public RichTextLabel richText; public RichTextLabel richText;
public int dialogOptionsLength = 1;
public override void _Ready() public override void _Ready()
{ {
@@ -20,25 +19,23 @@ public partial class dialog_bubble : CanvasLayer
playerName = "[color=blue]" + playerName + "[/color]"; playerName = "[color=blue]" + playerName + "[/color]";
parsedDlg = Json.ParseString(FileAccess.Open(file, FileAccess.ModeFlags.Read).GetAsText().Replace("{player}", playerName)); parsedDlg = Json.ParseString(FileAccess.Open(file, FileAccess.ModeFlags.Read).GetAsText().Replace("{player}", playerName));
GetNode<Label>("box/name_label").Text = title; GetNode<Label>("box/name_label").Text = title;
GD.Print("Now talking to: " + actor);
if (GetParent().Name == "player") GetParent<player>().allowMovement = false; if (GetParent().Name == "player") GetParent<player>().allowMovement = false;
if (parsedDlg.AsGodotDictionary()["dialogType"].AsString() == "villager") if (parsedDlg.AsGodotDictionary()["dialogType"].AsString() == "villager")
WelcomeDialog(); GatherDialog("welcome");
Visible = true; Visible = true;
} }
public void WelcomeDialog()
public void GatherDialog(string key)
{ {
string[] welcomeText = parsedDlg.AsGodotDictionary()["welcome"].AsStringArray(); dlgLines = parsedDlg.AsGodotDictionary()[key].AsGodotArray();
Godot.Collections.Dictionary playerbeginoptions = parsedDlg.AsGodotDictionary()["playerbeginoptions"].AsGodotDictionary(); if (dlgLines.VariantType == Variant.Type.Array)
GD.Randomize(); dlgLines = dlgLines.AsGodotArray()[GD.RandRange(0, dlgLines.AsGodotArray().Count)];
dlgLines.Add(welcomeText[GD.Randi() % welcomeText.Length]);
MakeAnswerBox(new string[] { playerbeginoptions["talk"].AsStringArray()[GD.Randi() % playerbeginoptions["talk"].AsStringArray().Length], playerbeginoptions["goaway"].AsStringArray()[GD.Randi() % playerbeginoptions["goaway"].AsStringArray().Length] });
} }
public void CloseDialog() public void CloseDialog()
{ {
Visible = false; Visible = false;
dlgPointer = 0; dlgPointer = 0;
dlgLines.Clear();
richText.VisibleCharacters = -1; richText.VisibleCharacters = -1;
GetNode<Label>("box/name_label").Text = "???"; GetNode<Label>("box/name_label").Text = "???";
richText.Text = ""; richText.Text = "";
@@ -54,31 +51,35 @@ public partial class dialog_bubble : CanvasLayer
if (Input.IsActionJustPressed("ui_cancel") && Visible) richText.VisibleCharacters = richText.Text.Length; if (Input.IsActionJustPressed("ui_cancel") && Visible) richText.VisibleCharacters = richText.Text.Length;
if (Input.IsActionJustPressed("ui_accept") && GetNode<PanelContainer>("box/panel_container").Visible == false && Visible && richText.VisibleCharacters == -1 | Regex.Replace(richText.Text, @"\[[^]]+\]", "").Length <= richText.VisibleCharacters) if (Input.IsActionJustPressed("ui_accept") && GetNode<PanelContainer>("box/panel_container").Visible == false && Visible && richText.VisibleCharacters == -1 | Regex.Replace(richText.Text, @"\[[^]]+\]", "").Length <= richText.VisibleCharacters)
{ {
if (dlgPointer < dlgLines.Count && dlgLines[dlgPointer] is string) if (dlgPointer < dlgLines.AsGodotArray().Count)
{ {
richText.Text = dlgLines[dlgPointer].ToString(); if (dlgLines.AsGodotArray()[dlgPointer].VariantType == Variant.Type.String)
richText.VisibleCharacters = 0; {
GetNode<Timer>("typewriter_timer").Start(); richText.Text = dlgLines.AsGodotArray()[dlgPointer].ToString();
} richText.VisibleCharacters = 0;
dlgPointer++; GetNode<Timer>("typewriter_timer").Start();
} }
if (dlgPointer > dlgLines.Count) else if (dlgLines.AsGodotArray()[dlgPointer].VariantType == Variant.Type.Dictionary)
CloseDialog(); {
MakeAnswerBox(dlgLines.AsGodotArray()[dlgPointer].AsGodotDictionary().Keys.ToString().Trim('[', ']').Split(","));
GetNode<PanelContainer>("box/panel_container").Visible = true;
}
//AnswerBox wait for typewrite effect to finish (garbage code) }
GetNode<PanelContainer>("box/panel_container").Visible = richText.VisibleCharacters == -1 | Regex.Replace(richText.Text, @"\[[^]]+\]", "").Length <= richText.VisibleCharacters && GetNode("box/panel_container/margin_container/v_box_container").GetChildCount() == dialogOptionsLength; if (dlgLines.AsGodotArray()[dlgPointer].VariantType != Variant.Type.Dictionary)
dlgPointer++;
}
if (dlgPointer > dlgLines.AsGodotArray().Count)
CloseDialog();
} }
public void MakeAnswerBox(string[] dialogOptions) public void MakeAnswerBox(string[] dialogOptions)
{ {
var parent = GetNode("box/panel_container/margin_container/v_box_container"); var parent = GetNode("box/panel_container/margin_container/v_box_container");
GD.Print(dialogOptions);
for (int i = 0; parent.GetChildCount() < dialogOptions.Length; i++) for (int i = 0; parent.GetChildCount() < dialogOptions.Length; i++)
{ {
GD.Print(parent.GetChildren());
parent.AddChild(GD.Load<PackedScene>("res://scenes/gui/dlg_answer_button.tscn").Instantiate()); parent.AddChild(GD.Load<PackedScene>("res://scenes/gui/dlg_answer_button.tscn").Instantiate());
} }
for (int i = 0; i < dialogOptions.Length; i++) for (int i = 0; i < dialogOptions.Length; i++)
parent.GetChild<Button>(i).Text = dialogOptions[i]; parent.GetChild<Button>(i).Text = dialogOptions[i];
dialogOptionsLength = dialogOptions.Length;
} }
} }