2024-01-11 21:00:48 -05:00
|
|
|
using System.Linq;
|
|
|
|
using Godot;
|
|
|
|
using Godot.Collections;
|
|
|
|
|
|
|
|
namespace GodotGridInventory.Code;
|
|
|
|
|
|
|
|
public class ItemDatabase
|
|
|
|
{
|
|
|
|
public static ItemDatabase Instance { get; } = new();
|
2024-01-11 23:52:01 -05:00
|
|
|
private readonly Dictionary<string, Item> _itemDatabase = new();
|
2024-01-11 21:00:48 -05:00
|
|
|
|
2024-01-11 23:52:01 -05:00
|
|
|
public ItemDatabase()
|
|
|
|
{
|
|
|
|
InitializeItemDatabase();
|
|
|
|
}
|
2024-01-11 21:00:48 -05:00
|
|
|
|
2024-01-11 23:52:01 -05:00
|
|
|
public Item GetItemConfiguration(string id)
|
2024-01-11 21:00:48 -05:00
|
|
|
{
|
|
|
|
return _itemDatabase[id];
|
|
|
|
}
|
|
|
|
|
|
|
|
private void InitializeItemDatabase()
|
|
|
|
{
|
|
|
|
GD.Print("Initializing the item database.");
|
|
|
|
var items = DirAccess.GetFilesAt(ItemDatabaseConstants.ITEMS_PATH).Where(filePath => filePath.EndsWith(ItemDatabaseConstants.ITEM_EXTENSION));
|
|
|
|
foreach (var item in items)
|
|
|
|
{
|
2024-01-11 23:52:01 -05:00
|
|
|
var itemResource = GD.Load<Item>(item);
|
2024-01-11 21:00:48 -05:00
|
|
|
if (itemResource != null)
|
|
|
|
{
|
|
|
|
_itemDatabase.Add(itemResource.Id, itemResource);
|
|
|
|
GD.Print($"Loaded item {itemResource.Name} with id {itemResource.Id} into the item database.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|