more weird dialogue code

This commit is contained in:
2023-02-05 10:56:31 +01:00
parent 271084a94d
commit cec3a13a66
4 changed files with 131 additions and 127 deletions

View File

@@ -5,81 +5,79 @@ using System.Text.RegularExpressions;
public partial class dialog_bubble : CanvasLayer
{
public Variant parsedDlg;
public ArrayList dlgLines = new ArrayList();
public int dlgPointer = 0;
public RichTextLabel richText;
public int dialogOptionsLength;
public Variant parsedDlg;
public ArrayList dlgLines = new ArrayList();
public int dlgPointer = 0;
public RichTextLabel richText;
public int dialogOptionsLength = 1;
public override void _Ready()
{
richText = GetNode<RichTextLabel>("box/rich_text_label");
}
public void GetDialog(string file, string title, Variant actor, string playerName)
{
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();
Visible = true;
}
public void WelcomeDialog()
{
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] });
}
public void CloseDialog()
{
Visible = false;
dlgPointer = 0;
dlgLines.Clear();
richText.VisibleCharacters = -1;
GetNode<Label>("box/name_label").Text = "???";
richText.Text = "";
if (GetParent().Name == "player") GetParent<player>().allowMovement = true;
}
public override void _Process(double delta)
{
if (richText.VisibleCharacters < Regex.Replace(richText.Text, @"\[[^]]+\]", "").Length && GetNode<Timer>("typewriter_timer").IsStopped())
{
richText.VisibleCharacters++;
GetNode<Timer>("typewriter_timer").Start();
}
if (Input.IsActionJustPressed("ui_cancel") && Visible) richText.VisibleCharacters = richText.Text.Length;
if (Input.IsActionJustPressed("ui_accept") && Visible && richText.VisibleCharacters == -1 | Regex.Replace(richText.Text, @"\[[^]]+\]", "").Length <= richText.VisibleCharacters)
{
if (dlgPointer < dlgLines.Count && dlgLines[dlgPointer] is string)
{
richText.Text = dlgLines[dlgPointer].ToString();
richText.VisibleCharacters = 0;
GetNode<Timer>("typewriter_timer").Start();
}
dlgPointer++;
}
if (dlgPointer > dlgLines.Count)
CloseDialog();
public override void _Ready()
{
richText = GetNode<RichTextLabel>("box/rich_text_label");
}
public void GetDialog(string file, string title, Variant actor, string playerName)
{
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();
Visible = true;
}
public void WelcomeDialog()
{
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] });
}
public void CloseDialog()
{
Visible = false;
dlgPointer = 0;
dlgLines.Clear();
richText.VisibleCharacters = -1;
GetNode<Label>("box/name_label").Text = "???";
richText.Text = "";
if (GetParent().Name == "player") GetParent<player>().allowMovement = true;
}
public override void _Process(double delta)
{
if (richText.VisibleCharacters < Regex.Replace(richText.Text, @"\[[^]]+\]", "").Length && GetNode<Timer>("typewriter_timer").IsStopped())
{
richText.VisibleCharacters++;
GetNode<Timer>("typewriter_timer").Start();
}
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)
{
richText.Text = dlgLines[dlgPointer].ToString();
richText.VisibleCharacters = 0;
GetNode<Timer>("typewriter_timer").Start();
}
dlgPointer++;
}
if (dlgPointer > dlgLines.Count)
CloseDialog();
//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;
}
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];
//parent.GetChild<Button>(0).focus
dialogOptionsLength = dialogOptions.Length;
}
//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;
}
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;
}
}