67 lines
1.9 KiB
C#
67 lines
1.9 KiB
C#
using Godot;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
namespace Cosmobox
|
|
{
|
|
public static class ItemResourceManager
|
|
{
|
|
private static Dictionary<string, ItemResource> _items;
|
|
private static bool _initialized = false;
|
|
|
|
// 初始化物品数据
|
|
private static void Initialize()
|
|
{
|
|
if (_initialized) return;
|
|
|
|
_items = new Dictionary<string, ItemResource>();
|
|
|
|
// 加载 res://Resource/Item/ 目录下的所有 ItemResource
|
|
var dir = DirAccess.Open("res://Resource/Item/");
|
|
if (dir != null)
|
|
{
|
|
dir.ListDirBegin();
|
|
string fileName = dir.GetNext();
|
|
while (fileName != "")
|
|
{
|
|
if (!dir.CurrentIsDir() && fileName.EndsWith(".tres"))
|
|
{
|
|
var resourcePath = $"res://Resource/Item/{fileName}";
|
|
var item = GD.Load<ItemResource>(resourcePath);
|
|
if (item != null)
|
|
{
|
|
_items[item.ItemName] = item;
|
|
}
|
|
}
|
|
fileName = dir.GetNext();
|
|
}
|
|
}
|
|
|
|
_initialized = true;
|
|
}
|
|
|
|
// 获取所有物品
|
|
public static Dictionary<string, ItemResource> GetAllItems()
|
|
{
|
|
Initialize();
|
|
return _items;
|
|
}
|
|
|
|
// 通过名称获取物品
|
|
public static ItemResource GetItem(string itemName)
|
|
{
|
|
Initialize();
|
|
if (_items.TryGetValue(itemName, out var item))
|
|
{
|
|
return item;
|
|
}
|
|
return null;
|
|
}
|
|
|
|
// 获取物品数量
|
|
public static int GetItemCount()
|
|
{
|
|
Initialize();
|
|
return _items.Count;
|
|
}
|
|
}
|
|
} |