using singleton for playername now

This commit is contained in:
2023-02-22 21:20:54 +01:00
parent ca1957d618
commit a7932ce12b
5 changed files with 23 additions and 20 deletions

16
player_variables.cs Normal file
View File

@@ -0,0 +1,16 @@
using Godot;
using System;
using System.Text.RegularExpressions;
public partial class player_variables : Node
{
public static string PlayerName = "Yannik";
public static void ClearPlayerName() //normal getter setter crashes for some reason
{
PlayerName = Regex.Replace(PlayerName, @"\[[^]]+\]", "");
PlayerName = Regex.Replace(PlayerName, @"<[^>]*>", "");
if (PlayerName.Length > 12)
PlayerName = PlayerName.Substring(0, 12);
}
}

View File

@@ -17,7 +17,8 @@ config/icon="res://assets/textures/debug/dummy-player-normal.png"
[autoload]
Essential="*res://src/essential.cs"
Essential="res://src/essential.cs"
PlayerVariables="*res://player_variables.cs"
[display]

View File

@@ -44,7 +44,6 @@ layer_1/tile_data = PackedInt32Array(-262141, 0, 0, -196605, 0, 0, -196604, 0, 0
metadata/_edit_lock_ = true
[node name="player" parent="." instance=ExtResource("2_0skrb")]
playerName = "Yannik"
[node name="main_cam" parent="player" index="2"]
limit_left = -1791

View File

@@ -28,7 +28,7 @@ public partial class console : PopupPanel
public void OnLineEditTextSubmitted(string command)
{
line.Clear();
if (command.Length != 0) textblock.AddText(GetParent<player>().playerName + " > " + command + "\n");
if (command.Length != 0) textblock.AddText(player_variables.PlayerName + " > " + command + "\n");
Variant args;
if (command.Split(' ').Length == 2 && commandDict.ContainsKey(command.Split(' ')[0].ToLower()))
{
@@ -102,12 +102,9 @@ public partial class console : PopupPanel
}
public void playername(string name)
{
if (GetParent().Name == "player")
{
GetParent<player>().playerName = name;
GetParent<player>().ClearPlayerName();
textblock.AddText("Your new name is now: " + GetParent<player>().playerName + "\n");
}
player_variables.PlayerName = name;
player_variables.ClearPlayerName();
textblock.AddText("Your new name is now: " + player_variables.PlayerName + "\n");
}
public void reload() => GetTree().ReloadCurrentScene();
}

View File

@@ -4,8 +4,6 @@ using System.Text.RegularExpressions;
public partial class player : CharacterBody2D
{
[Export] public string playerName;
[Export] public float speed = 1;
public bool allowMovement = true;
public Vector2 movement;
@@ -15,18 +13,10 @@ public partial class player : CharacterBody2D
public override void _Ready()
{
ClearPlayerName();
animatedSprite = GetNode<AnimatedSprite2D>("animated_sprite_2d");
rotCenter = GetNode<Marker2D>("rotation_center");
dialogRayCast = GetNode<RayCast2D>("rotation_center/ray_cast_2d");
}
public void ClearPlayerName()
{
playerName = Regex.Replace(playerName, @"\[[^]]+\]", "");
playerName = Regex.Replace(playerName, @"<[^>]*>", "");
if (playerName.Length > 12)
playerName = playerName.Substring(0, 12);
}
public override void _PhysicsProcess(double delta)
{
if (allowMovement) movement = Input.GetVector("move_left", "move_right", "move_up", "move_down");
@@ -38,7 +28,7 @@ public partial class player : CharacterBody2D
{
if (Input.IsActionJustPressed("ui_accept") && dialogRayCast.IsColliding() && allowMovement)
GetNode<dialog_bubble>("dialog_bubble").GetDialog(dialogRayCast.GetCollider().Get("file").AsString(), dialogRayCast.GetCollider().Get("title").AsString(),
(Area2D)dialogRayCast.GetCollider(), playerName, dialogRayCast.GetCollider().Get("introducedVillager").AsBool());
(Area2D)dialogRayCast.GetCollider(), player_variables.PlayerName, dialogRayCast.GetCollider().Get("introducedVillager").AsBool());
//animation system (with controller support wich cant get normalized vector)
if (allowMovement == false)