game registry internal refactor

This commit is contained in:
Rendo 2025-07-25 18:02:47 +05:00
commit d5573db251
11 changed files with 68 additions and 188 deletions

View file

@ -1,69 +0,0 @@
using Godot;
using System.Linq;
using System.Collections.Generic;
public partial class DebugZombieSpawner : PopupPanel
{
private List<string> variants;
[Export] private Label label;
[Export] private LineEdit line;
[Export] private SpinBox spin;
public void OnTextChanged(string text)
{
variants = GameRegistry.GetZombieNames();
variants.Sort(Comparer);
if (variants.Count > 3)
{
label.Text = $"{variants[0]}\n{variants[1]}\n{variants[2]}";
}
else
{
label.Text = "";
for (int i = 0; i < variants.Count; i++)
{
label.Text += variants[i] + "\n";
}
}
}
public void OnTextSubmitted(string text)
{
spin.GrabFocus();
}
public void OnPopupHide()
{
if (variants == null || variants.Count == 0)
{
return;
}
ZombieSequencer.Instance.DebugClearQueue();
for(int i = 0; i < spin.Value; i++)
{
ZombieSequencer.Instance.Add(variants[0]);
}
QueueFree();
}
private int Comparer(string x, string y)
{
return LevensteinDistance(x,line.Text) - LevensteinDistance(y,line.Text);
}
private static int LevensteinDistance(string a, string b)
{
if (b.Length == 0)
{
return a.Length;
}
else if (a.Length == 0)
{
return b.Length;
}
else if (a[0] == b[0])
{
return LevensteinDistance(a[1..], b[1..]);
}
List<int> x = [LevensteinDistance(a[1..], b[1..]), LevensteinDistance(a[1..], b), LevensteinDistance(a, b[1..])];
return 1 + x.Min();
}
}

View file

@ -1 +0,0 @@
uid://dac0bvlhho7u4

View file

@ -72,7 +72,7 @@ public partial class SaveSerializer : Node
foreach (var plantId in parsed.PlayerPlants)
{
playerProgress.PlayerPlants.Add(GameRegistry.GetPlantByName(plantId));
playerProgress.PlayerPlants.Add((PlantResource)GameRegistry.GetEntityByName(plantId));
}
@ -80,7 +80,7 @@ public partial class SaveSerializer : Node
}
private static void InitiateCleanSave()
{
PlayerProgress.Instance.PlayerPlants = new List<PlantResource>([GameRegistry.GetPlantByName("peashooter")]);
PlayerProgress.Instance.PlayerPlants = new List<PlantResource>([(PlantResource)GameRegistry.GetEntityByName("peashooter")]);
PlayerProgress.Instance.Money = 0;
SaveGame();

View file

@ -12,12 +12,6 @@ public partial class Cheats : Node
{
RuntimeLevelData.Instance.AddSun(50);
}
if (@event.IsActionPressed("cheat_zombie_spawn"))
{
var spawner = GD.Load<PackedScene>("res://scenes/debug_zombie_spawner.tscn").Instantiate<DebugZombieSpawner>();
GetTree().CurrentScene.AddChild(spawner);
}
if (@event.IsActionPressed("cheat_unlock_all"))
{
PlayerProgress.Instance.PlayerPlants = GameRegistry.GetPlants();

View file

@ -3,13 +3,11 @@ using Newlon.Components.GUI.Seedpackets;
public partial class AlmanachGrid : GridContainer
{
private PackedScene _plantCard;
private PackedScene _plantCard = ResourceLoader.Load<PackedScene>("res://scenes/gui/seedpacket.tscn");
[Export]
private bool _zombies;
public override void _Ready()
{
_plantCard = ResourceLoader.Load<PackedScene>("res://scenes/gui/seedpacket.tscn");
if (_zombies)
{
var list = GameRegistry.GetZombies();

View file

@ -1,4 +1,5 @@
using Godot;
using Newlon;
using Newlon.Components;
using Newlon.Components.Level;
using Newlon.Components.Plants;
@ -37,7 +38,7 @@ public partial class InitialPackedSceneSpawner : Node
{
PoolContainer.Instance.Plants.AddChild(plant);
node.GlobalPosition = position;
plant.Resource = GameRegistry.GetPlantByName(plant.internal_id);
plant.Resource = GameRegistry.GetEntityByName(plant.internal_id) as PlantResource;
PoolContainer.Instance.EntityField[plant.Resource.Layer].Add(plant.GlobalPosition, plant);
}
else

View file

@ -163,7 +163,7 @@ public partial class SurvivalZombieSpawner : Node
List<ZombieResource> list = [];
foreach (var res in pool)
{
list.Add(GameRegistry.GetZombieByName(res));
list.Add(GameRegistry.GetEntityByName(res) as ZombieResource);
}
return list;
}

View file

@ -58,7 +58,7 @@ public partial class ZombieSequencer : Node2D
private void Spawn(string id, int lane)
{
RuntimeZombieData zombie = GameRegistry.GetZombieByName(id).Scene.Instantiate<RuntimeZombieData>();
RuntimeZombieData zombie = GameRegistry.GetEntityByName(id).Scene.Instantiate<RuntimeZombieData>();
PoolContainer.Instance.Zombies.AddChild(zombie);
zombie.GlobalPosition = new Vector2(GlobalPosition.X, FieldParams.RightFieldBoundary.Y - (lane - 1) * FieldParams.TileHeight);

View file

@ -7,8 +7,7 @@ public partial class GameRegistry : Node
private const string PLANT_RESOURCE_PATH = "res://assets/plants/";
private const string ZOMBIE_RESOURCE_PATH = "res://assets/zombies/";
public static GameRegistry Instance { get; private set; }
public static readonly Dictionary<string, PlantResource> PlantDictionary = [];
public static readonly Dictionary<string, ZombieResource> ZombieDictionary = [];
public static readonly Dictionary<string, DisplayResource> EntityDictionary = [];
public override void _EnterTree()
{
@ -21,10 +20,9 @@ public partial class GameRegistry : Node
{
var plant = ResourceLoader.Load<PlantResource>(PLANT_RESOURCE_PATH + file);
plant.internal_id = file.ToLower().Split('.')[0];
PlantDictionary.Add(file.ToLower().Split('.')[0], plant);
EntityDictionary.Add(file.ToLower().Split('.')[0], plant);
}
}
//Zombie init
string[] zombieFiles = ResourceLoader.ListDirectory(ZOMBIE_RESOURCE_PATH);
@ -34,66 +32,67 @@ public partial class GameRegistry : Node
{
var zombie = ResourceLoader.Load<ZombieResource>(ZOMBIE_RESOURCE_PATH + file);
zombie.internal_id = file.ToLower().Split('.')[0];
ZombieDictionary.Add(file.ToLower().Split('.')[0], zombie);
EntityDictionary.Add(file.ToLower().Split('.')[0], zombie);
}
}
}
public static PlantResource GetPlantByName(string name)
public static DisplayResource GetEntityByName(string name)
{
if (PlantDictionary.ContainsKey(name) == false) return null;
return PlantDictionary[name];
if (EntityDictionary.ContainsKey(name) == false) return null;
return EntityDictionary[name];
}
public static List<string> GetPlantNames()
public static List<string> GetEntityNames()
{
List<string> result = [];
foreach (var key in PlantDictionary.Keys)
foreach (var key in EntityDictionary.Keys)
{
result.Add(key);
}
return result;
}
public static List<DisplayResource> GetEntities()
{
List<DisplayResource> result = [];
foreach (var value in EntityDictionary.Values)
{
result.Add(value);
}
return result;
}
public static List<PlantResource> GetPlants()
{
List<PlantResource> result = [];
foreach (var value in PlantDictionary.Values)
{
result.Add(value);
}
return result;
}
public static int GetPlantCount()
{
return PlantDictionary.Count;
}
var entities = GetEntities();
List<PlantResource> plants = [];
public static ZombieResource GetZombieByName(string name)
{
if (ZombieDictionary.ContainsKey(name) == false) return null;
return ZombieDictionary[name];
}
public static List<string> GetZombieNames()
{
List<string> result = [];
foreach (var key in ZombieDictionary.Keys)
foreach (var entity in entities)
{
result.Add(key);
if (entity is PlantResource plant)
{
plants.Add(plant);
}
}
return result;
return plants;
}
public static List<ZombieResource> GetZombies()
{
List<ZombieResource> result = [];
foreach (var value in ZombieDictionary.Values)
{
result.Add(value);
}
return result;
}
var entities = GetEntities();
List<ZombieResource> zombies = [];
public static int GetZombieCount()
foreach (var entity in entities)
{
if (entity is ZombieResource zombie)
{
zombies.Add(zombie);
}
}
return zombies;
}
public static int GetEntityCount()
{
return ZombieDictionary.Count;
return EntityDictionary.Count;
}
}