improved typewriter and cleaned dialogue code

This commit is contained in:
2023-02-12 10:44:23 +01:00
parent 953210b65f
commit 47c6d08f53
2 changed files with 138 additions and 130 deletions

View File

@@ -11,9 +11,7 @@ script = ExtResource("1_xtj1q")
metadata/_edit_use_anchors_ = true
[node name="typewriter_timer" type="Timer" parent="."]
wait_time = 0.04
one_shot = true
metadata/charsAtOnce = 60
wait_time = 0.05
[node name="box" type="ColorRect" parent="."]
anchors_preset = -1
@@ -102,3 +100,5 @@ theme_override_constants/margin_top = 30
theme_override_constants/margin_right = 60
theme_override_constants/margin_bottom = 30
metadata/_edit_lock_ = true
[connection signal="timeout" from="typewriter_timer" to="." method="OnTypewriterTimerTimeout"]

View File

@@ -1,5 +1,6 @@
using Godot;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
public partial class dialog_bubble : CanvasLayer
{
@@ -7,6 +8,7 @@ public partial class dialog_bubble : CanvasLayer
public Variant dlgLines;
public int dlgPointer = 0;
public RichTextLabel richText;
public Timer typewriterTimer;
public string name;
public Area2D triggerArea;
@@ -18,6 +20,7 @@ public partial class dialog_bubble : CanvasLayer
public override void _Ready()
{
richText = GetNode<RichTextLabel>("box/rich_text_label");
typewriterTimer = GetNode<Timer>("typewriter_timer");
}
public void GetDialog(string file, string title, Area2D actor, string playerName, bool introducedVillager)
{
@@ -52,16 +55,12 @@ public partial class dialog_bubble : CanvasLayer
//TODO:copy a clean default array and remove already used indexes and copy from clean array when its empty
}
public void CloseDialog()
{
Visible = false;
dlgPointer = 0;
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)
{
DialogControlls();
AnswerBoxControlls();
}
public void DialogControlls()
{
if (Input.IsActionJustPressed("ui_cancel") && Visible) richText.VisibleCharacters = richText.Text.Length;
@@ -85,36 +84,20 @@ public partial class dialog_bubble : CanvasLayer
}
dlgPointer++;
}
//Typewrite effect
if (richText.VisibleCharacters < Regex.Replace(richText.Text, @"\[[^]]+\]", "").Length && GetNode<Timer>("typewriter_timer").IsStopped())
{
richText.VisibleCharacters++;
GetNode<Timer>("typewriter_timer").Start();
}
if (dlgPointer > dlgLines.AsGodotArray().Count)
CloseDialog();
//AnswerBox answer "logic"
if (GetNode<PanelContainer>("box/panel_container").Visible == true
&& GetNode("box/panel_container/margin_container").GetChild(0).GetChild<Button>(0).ButtonGroup.GetPressedButton() != null)
{
var answer = dlgLines.AsGodotArray()[dlgPointer - 1].AsGodotDictionary()[GetNode<Button>(GetNode("box/panel_container/margin_container")
.GetChild(0).GetChild<Button>(0).ButtonGroup.GetPressedButton().GetPath()).Text];
GetNode<PanelContainer>("box/panel_container").Visible = false;
if (answer.VariantType == Variant.Type.String && answer.AsString().StartsWith("<goto:>"))
{
GatherDialog(answer.AsString().Replace("<goto:>", ""));
UpdateDialog();
}
dlgPointer++;
}
}
public void UpdateDialog()
{
richText.Text = dlgLines.AsGodotArray()[dlgPointer].ToString();
richText.VisibleCharacters = 0;
GetNode<Timer>("typewriter_timer").Start();
typewriterTimer.Start();
}
public void OnTypewriterTimerTimeout()
{
if (richText.VisibleCharacters < Regex.Replace(richText.Text, @"\[[^]]+\]", "").Length)
richText.VisibleCharacters++;
else typewriterTimer.Stop();
}
public void MakeAnswerBox(string[] dialogOptions)
{
@@ -131,6 +114,22 @@ public partial class dialog_bubble : CanvasLayer
GetNode<PanelContainer>("box/panel_container").Visible = true;
parent.GetChild<Button>(0).GrabFocus();
}
public void AnswerBoxControlls()
{
if (GetNode<PanelContainer>("box/panel_container").Visible == true
&& GetNode("box/panel_container/margin_container").GetChild(0).GetChild<Button>(0).ButtonGroup.GetPressedButton() != null)
{
var answer = dlgLines.AsGodotArray()[dlgPointer - 1].AsGodotDictionary()[GetNode<Button>(GetNode("box/panel_container/margin_container")
.GetChild(0).GetChild<Button>(0).ButtonGroup.GetPressedButton().GetPath()).Text];
GetNode<PanelContainer>("box/panel_container").Visible = false;
if (answer.VariantType == Variant.Type.String && answer.AsString().StartsWith("<goto:>"))
{
GatherDialog(answer.AsString().Replace("<goto:>", ""));
UpdateDialog();
}
dlgPointer++;
}
}
public void InDialogEvents(int eventID) //maybe replaceable with jsonrpc?
{
switch (eventID)
@@ -143,4 +142,13 @@ public partial class dialog_bubble : CanvasLayer
break;
}
}
public void CloseDialog()
{
Visible = false;
dlgPointer = 0;
richText.VisibleCharacters = -1;
GetNode<Label>("box/name_label").Text = "???";
richText.Text = "";
if (GetParent().Name == "player") GetParent<player>().allowMovement = true;
}
}