dialogue system changes

This commit is contained in:
2023-02-07 14:13:30 +01:00
parent 72421f4a32
commit 0190b6bdb2
4 changed files with 133 additions and 132 deletions

View File

@@ -13,9 +13,9 @@
]
},
"welcome": [
"Oh hello, [color=blue]{player}[/color]... You look great! What do you want from me?",
"Hey, [color=blue]{player}[/color]! Hope you have fun today! So what do you want?",
"Hi! What's up [color=blue]{player}[/color]?"
"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}?"
],
"else": [
"Do you need something else?",
@@ -23,7 +23,7 @@
],
"goodbye": [
"Was nice talking to you goodbye!",
"Have a great day, [color=blue]{player}[/color]!"
"Have a great day, {player}!"
],
"tipp": [
[

View File

@@ -11,7 +11,7 @@ script = ExtResource("1_xtj1q")
metadata/_edit_use_anchors_ = true
[node name="typewriter_timer" type="Timer" parent="."]
wait_time = 0.05
wait_time = 0.04
one_shot = true
metadata/charsAtOnce = 60

View File

@@ -5,79 +5,80 @@ 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 = 1;
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") && 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();
public override void _Ready()
{
richText = GetNode<RichTextLabel>("box/rich_text_label");
}
public void GetDialog(string file, string title, Variant actor, string playerName)
{
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();
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];
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;
}
}

View File

@@ -5,61 +5,61 @@ using System.Text.RegularExpressions;
public partial class player : CharacterBody2D
{
[Export] public string playerName;
[Export] public int speed = 200;
public bool allowMovement = true;
public Vector2 movement;
public AnimatedSprite2D animatedSprite;
public Marker2D rotCenter;
public RayCast2D dialogRayCast;
[Export] public string playerName;
[Export] public int speed = 200;
public bool allowMovement = true;
public Vector2 movement;
public AnimatedSprite2D animatedSprite;
public Marker2D rotCenter;
public RayCast2D dialogRayCast;
public override void _Ready()
{
playerName = Regex.Replace(playerName, @"\[[^]]+\]", "");
if (playerName.Length > 12)
playerName = playerName.Substring(0, 12);
public override void _Ready()
{
playerName = Regex.Replace(playerName, @"\[[^]]+\]", "");
if (playerName.Length > 12)
playerName = playerName.Substring(0, 12);
animatedSprite = GetNode<AnimatedSprite2D>("animated_sprite_2d");
rotCenter = GetNode<Marker2D>("rotation_center");
dialogRayCast = GetNode<RayCast2D>("rotation_center/ray_cast_2d");
}
public override void _PhysicsProcess(double delta)
{
if (allowMovement) movement = Input.GetVector("move_left", "move_right", "move_up", "move_down");
else movement = Vector2.Zero;
if (movement.Length() != 0) rotCenter.Rotation = new Vector2((float)Math.Round(movement.X, 0), (float)Math.Round(movement.Y, 0)).Angle();
MoveAndCollide(movement * speed * (float)delta);
}
public override void _Process(double delta)
{
if (Input.IsActionJustPressed("ui_accept") && dialogRayCast.IsColliding() && allowMovement)
GetNode<dialog_bubble>("dialog_bubble").GetDialog(dialogRayCast.GetCollider().Get("file").AsString(), dialogRayCast.GetCollider().Get("title").AsString(), dialogRayCast.GetCollider().Get("actor"), playerName);
animatedSprite = GetNode<AnimatedSprite2D>("animated_sprite_2d");
rotCenter = GetNode<Marker2D>("rotation_center");
dialogRayCast = GetNode<RayCast2D>("rotation_center/ray_cast_2d");
}
public override void _PhysicsProcess(double delta)
{
if (allowMovement) movement = Input.GetVector("move_left", "move_right", "move_up", "move_down");
else movement = Vector2.Zero;
if (movement.Length() != 0) rotCenter.Rotation = new Vector2((float)Math.Round(movement.X, 0), (float)Math.Round(movement.Y, 0)).Angle();
MoveAndCollide(movement * speed * (float)delta);
}
public override void _Process(double delta)
{
if (Input.IsActionJustPressed("ui_accept") && dialogRayCast.IsColliding() && allowMovement)
GetNode<dialog_bubble>("dialog_bubble").GetDialog(dialogRayCast.GetCollider().Get("file").AsString(), dialogRayCast.GetCollider().Get("title").AsString(), dialogRayCast.GetCollider().Get("actor"), playerName);
//animation system (with controller support wich cant get normalized vector)
if (allowMovement == false)
{
animatedSprite.Stop();
animatedSprite.Frame = 0;
}
if (movement.Length() != 0)
animatedSprite.Play();
else
{
animatedSprite.Frame = 0;
animatedSprite.Stop();
}
if (Math.Round(movement.X, 0) != 0)
{
animatedSprite.Animation = "move_side";
animatedSprite.FlipH = movement.X < 0.5;
animatedSprite.SpeedScale = Math.Abs(movement.X * speed / 150);
}
else if (Math.Round(movement.Y, 0) != 0)
{
if (movement.Y > 0.05) animatedSprite.Animation = "move_down";
if (movement.Y < 0.05) animatedSprite.Animation = "move_up";
animatedSprite.FlipH = false;
animatedSprite.SpeedScale = Math.Abs(movement.Y * speed / 150);
}
}
//animation system (with controller support wich cant get normalized vector)
if (allowMovement == false)
{
animatedSprite.Stop();
animatedSprite.Frame = 0;
}
if (movement.Length() != 0)
animatedSprite.Play();
else
{
animatedSprite.Frame = 0;
animatedSprite.Stop();
}
if (Math.Round(movement.X, 0) != 0)
{
animatedSprite.Animation = "move_side";
animatedSprite.FlipH = movement.X < 0.5;
animatedSprite.SpeedScale = Math.Abs(movement.X * speed / 150);
}
else if (Math.Round(movement.Y, 0) != 0)
{
if (movement.Y > 0.05) animatedSprite.Animation = "move_down";
if (movement.Y < 0.05) animatedSprite.Animation = "move_up";
animatedSprite.FlipH = false;
animatedSprite.SpeedScale = Math.Abs(movement.Y * speed / 150);
}
}
}