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",
"playerbeginoptions": {
"talk": [
"I just wanna talk",
"What's up",
"Can we talk?"
],
"goaway": [
"Nevermind!",
"Nothing",
"Have a great day!"
]
},
"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?",
"Hi! What's up {player}?"
[
"Oh hello, {player}... You look great! What can I do for you?",
{
"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": [
"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": [
"Was nice talking to you goodbye!",
"Have a great day, {player}!"
[
"Was nice talking to you goodbye!"
],
[
"Have a great day, {player}!"
]
],
"tipp": [
[

View File

@@ -1,15 +1,14 @@
using Godot;
using Godot.Collections;
using System;
using System.Collections;
using System.Text.RegularExpressions;
public partial class dialog_bubble : CanvasLayer
{
public Variant parsedDlg;
public ArrayList dlgLines = new ArrayList();
public Variant dlgLines;
public int dlgPointer = 0;
public RichTextLabel richText;
public int dialogOptionsLength = 1;
public override void _Ready()
{
@@ -20,25 +19,23 @@ public partial class dialog_bubble : CanvasLayer
playerName = "[color=blue]" + playerName + "[/color]";
parsedDlg = Json.ParseString(FileAccess.Open(file, FileAccess.ModeFlags.Read).GetAsText().Replace("{player}", playerName));
GetNode<Label>("box/name_label").Text = title;
GD.Print("Now talking to: " + actor);
if (GetParent().Name == "player") GetParent<player>().allowMovement = false;
if (parsedDlg.AsGodotDictionary()["dialogType"].AsString() == "villager")
WelcomeDialog();
GatherDialog("welcome");
Visible = true;
}
public void WelcomeDialog()
public void GatherDialog(string key)
{
string[] welcomeText = parsedDlg.AsGodotDictionary()["welcome"].AsStringArray();
Godot.Collections.Dictionary playerbeginoptions = parsedDlg.AsGodotDictionary()["playerbeginoptions"].AsGodotDictionary();
GD.Randomize();
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] });
dlgLines = parsedDlg.AsGodotDictionary()[key].AsGodotArray();
if (dlgLines.VariantType == Variant.Type.Array)
dlgLines = dlgLines.AsGodotArray()[GD.RandRange(0, dlgLines.AsGodotArray().Count)];
}
public void CloseDialog()
{
Visible = false;
dlgPointer = 0;
dlgLines.Clear();
richText.VisibleCharacters = -1;
GetNode<Label>("box/name_label").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_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();
richText.VisibleCharacters = 0;
GetNode<Timer>("typewriter_timer").Start();
}
dlgPointer++;
}
if (dlgPointer > dlgLines.Count)
CloseDialog();
if (dlgLines.AsGodotArray()[dlgPointer].VariantType == Variant.Type.String)
{
richText.Text = dlgLines.AsGodotArray()[dlgPointer].ToString();
richText.VisibleCharacters = 0;
GetNode<Timer>("typewriter_timer").Start();
}
else if (dlgLines.AsGodotArray()[dlgPointer].VariantType == Variant.Type.Dictionary)
{
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)
{
var parent = GetNode("box/panel_container/margin_container/v_box_container");
GD.Print(dialogOptions);
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());
}
for (int i = 0; i < dialogOptions.Length; i++)
parent.GetChild<Button>(i).Text = dialogOptions[i];
dialogOptionsLength = dialogOptions.Length;
}
}