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

@@ -66,6 +66,7 @@ theme_override_font_sizes/font_size = 36
text = "???" text = "???"
[node name="panel_container" type="PanelContainer" parent="box"] [node name="panel_container" type="PanelContainer" parent="box"]
visible = false
layout_mode = 1 layout_mode = 1
anchors_preset = 3 anchors_preset = 3
anchor_left = 1.0 anchor_left = 1.0

View File

@@ -8,4 +8,4 @@
[node name="first_world" parent="." instance=ExtResource("2_x8nyb")] [node name="first_world" parent="." instance=ExtResource("2_x8nyb")]
[node name="player" parent="." instance=ExtResource("1_p5d84")] [node name="player" parent="." instance=ExtResource("1_p5d84")]
playerName = "[rainbow]Yannik[/rainbow]" playerName = "Yannik"

View File

@@ -5,81 +5,79 @@ 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 ArrayList dlgLines = new ArrayList();
public int dlgPointer = 0; public int dlgPointer = 0;
public RichTextLabel richText; public RichTextLabel richText;
public int dialogOptionsLength; public int dialogOptionsLength = 1;
public override void _Ready() public override void _Ready()
{ {
richText = GetNode<RichTextLabel>("box/rich_text_label"); richText = GetNode<RichTextLabel>("box/rich_text_label");
} }
public void GetDialog(string file, string title, Variant actor, string playerName) public void GetDialog(string file, string title, Variant actor, string playerName)
{ {
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); 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(); WelcomeDialog();
Visible = true; Visible = true;
} }
public void WelcomeDialog() public void WelcomeDialog()
{ {
string[] welcomeText = parsedDlg.AsGodotDictionary()["welcome"].AsStringArray(); string[] welcomeText = parsedDlg.AsGodotDictionary()["welcome"].AsStringArray();
Godot.Collections.Dictionary playerbeginoptions = parsedDlg.AsGodotDictionary()["playerbeginoptions"].AsGodotDictionary(); Godot.Collections.Dictionary playerbeginoptions = parsedDlg.AsGodotDictionary()["playerbeginoptions"].AsGodotDictionary();
GD.Randomize(); GD.Randomize();
dlgLines.Add(welcomeText[GD.Randi() % welcomeText.Length]); 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] }); 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(); dlgLines.Clear();
richText.VisibleCharacters = -1; richText.VisibleCharacters = -1;
GetNode<Label>("box/name_label").Text = "???"; GetNode<Label>("box/name_label").Text = "???";
richText.Text = ""; richText.Text = "";
if (GetParent().Name == "player") GetParent<player>().allowMovement = true; if (GetParent().Name == "player") GetParent<player>().allowMovement = true;
} }
public override void _Process(double delta) public override void _Process(double delta)
{ {
if (richText.VisibleCharacters < Regex.Replace(richText.Text, @"\[[^]]+\]", "").Length && GetNode<Timer>("typewriter_timer").IsStopped()) if (richText.VisibleCharacters < Regex.Replace(richText.Text, @"\[[^]]+\]", "").Length && GetNode<Timer>("typewriter_timer").IsStopped())
{ {
richText.VisibleCharacters++; richText.VisibleCharacters++;
GetNode<Timer>("typewriter_timer").Start(); GetNode<Timer>("typewriter_timer").Start();
} }
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") && 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.Count && dlgLines[dlgPointer] is string)
{ {
richText.Text = dlgLines[dlgPointer].ToString(); richText.Text = dlgLines[dlgPointer].ToString();
richText.VisibleCharacters = 0; richText.VisibleCharacters = 0;
GetNode<Timer>("typewriter_timer").Start(); GetNode<Timer>("typewriter_timer").Start();
} }
dlgPointer++; dlgPointer++;
} }
if (dlgPointer > dlgLines.Count) if (dlgPointer > dlgLines.Count)
CloseDialog(); CloseDialog();
//AnswerBox wait for typewrite effect to finish (garbage code) //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; 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) 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); 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()); 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;
//parent.GetChild<Button>(0).focus }
dialogOptionsLength = dialogOptions.Length;
}
} }

View File

@@ -1,60 +1,65 @@
using Godot; using Godot;
using System; using System;
using System.Text.RegularExpressions;
public partial class player : CharacterBody2D public partial class player : CharacterBody2D
{ {
[Export] public string playerName; [Export] public string playerName;
[Export] public int speed = 200; [Export] public int speed = 200;
public bool allowMovement = true; public bool allowMovement = true;
public Vector2 movement; public Vector2 movement;
public AnimatedSprite2D animatedSprite; public AnimatedSprite2D animatedSprite;
public Marker2D rotCenter; public Marker2D rotCenter;
public RayCast2D dialogRayCast; public RayCast2D dialogRayCast;
public override void _Ready() public override void _Ready()
{ {
animatedSprite = GetNode<AnimatedSprite2D>("animated_sprite_2d"); playerName = Regex.Replace(playerName, @"\[[^]]+\]", "");
rotCenter = GetNode<Marker2D>("rotation_center"); if (playerName.Length > 12)
dialogRayCast = GetNode<RayCast2D>("rotation_center/ray_cast_2d"); playerName = playerName.Substring(0, 12);
}
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) animatedSprite = GetNode<AnimatedSprite2D>("animated_sprite_2d");
if (allowMovement == false) rotCenter = GetNode<Marker2D>("rotation_center");
{ dialogRayCast = GetNode<RayCast2D>("rotation_center/ray_cast_2d");
animatedSprite.Stop(); }
animatedSprite.Frame = 0; public override void _PhysicsProcess(double delta)
} {
if (movement.Length() != 0) if (allowMovement) movement = Input.GetVector("move_left", "move_right", "move_up", "move_down");
animatedSprite.Play(); else movement = Vector2.Zero;
else 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);
animatedSprite.Frame = 0; }
animatedSprite.Stop(); public override void _Process(double delta)
} {
if (Math.Round(movement.X, 0) != 0) 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.Animation = "move_side";
animatedSprite.FlipH = movement.X < 0.5; //animation system (with controller support wich cant get normalized vector)
animatedSprite.SpeedScale = Math.Abs(movement.X * speed / 150); if (allowMovement == false)
} {
else if (Math.Round(movement.Y, 0) != 0) animatedSprite.Stop();
{ animatedSprite.Frame = 0;
if (movement.Y > 0.05) animatedSprite.Animation = "move_down"; }
if (movement.Y < 0.05) animatedSprite.Animation = "move_up"; if (movement.Length() != 0)
animatedSprite.FlipH = false; animatedSprite.Play();
animatedSprite.SpeedScale = Math.Abs(movement.Y * speed / 150); 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);
}
}
} }