Init
This commit is contained in:
121
scripts/inventory/InventoryCategoryManager.cs
Normal file
121
scripts/inventory/InventoryCategoryManager.cs
Normal file
@ -0,0 +1,121 @@
|
||||
using Godot;
|
||||
using System.Collections.Generic;
|
||||
using System.Text.Json;
|
||||
|
||||
public partial class InventoryCategoryManager : Node
|
||||
{
|
||||
public static InventoryCategoryManager Instance { get; private set; }
|
||||
|
||||
public class InventoryCategory
|
||||
{
|
||||
public string CategoryName { get; set; }
|
||||
public List<string> Items { get; set; } = new List<string>();
|
||||
}
|
||||
|
||||
public class InventoryCategoryData
|
||||
{
|
||||
public List<InventoryCategory> Categories { get; set; } = new List<InventoryCategory>();
|
||||
}
|
||||
|
||||
private InventoryCategoryData categoryData;
|
||||
private Dictionary<string, InventoryCategory> categoryMap = new Dictionary<string, InventoryCategory>();
|
||||
|
||||
public override void _Ready()
|
||||
{
|
||||
if (Instance == null)
|
||||
{
|
||||
Instance = this;
|
||||
LoadInventoryCategories();
|
||||
}
|
||||
else
|
||||
{
|
||||
QueueFree();
|
||||
}
|
||||
}
|
||||
|
||||
private void LoadInventoryCategories()
|
||||
{
|
||||
string configPath = "res://data/config/inventory_categories.json";
|
||||
|
||||
if (!FileAccess.FileExists(configPath))
|
||||
{
|
||||
GD.PrintErr($"库存分类配置文件不存在: {configPath}");
|
||||
return;
|
||||
}
|
||||
|
||||
using var file = FileAccess.Open(configPath, FileAccess.ModeFlags.Read);
|
||||
if (file == null)
|
||||
{
|
||||
GD.PrintErr($"无法打开库存分类配置文件: {configPath}");
|
||||
return;
|
||||
}
|
||||
|
||||
string jsonContent = file.GetAsText();
|
||||
|
||||
try
|
||||
{
|
||||
var options = new JsonSerializerOptions
|
||||
{
|
||||
PropertyNameCaseInsensitive = true
|
||||
};
|
||||
|
||||
categoryData = JsonSerializer.Deserialize<InventoryCategoryData>(jsonContent, options);
|
||||
|
||||
if (categoryData?.Categories != null)
|
||||
{
|
||||
// 构建分类映射
|
||||
foreach (var category in categoryData.Categories)
|
||||
{
|
||||
categoryMap[category.CategoryName] = category;
|
||||
GD.Print($"加载库存分类: {category.CategoryName}, 包含 {category.Items.Count} 个物品");
|
||||
}
|
||||
|
||||
GD.Print($"成功加载 {categoryData.Categories.Count} 个库存分类");
|
||||
}
|
||||
}
|
||||
catch (JsonException e)
|
||||
{
|
||||
GD.PrintErr($"解析库存分类配置文件失败: {e.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
public List<InventoryCategory> GetAllCategories()
|
||||
{
|
||||
return categoryData?.Categories ?? new List<InventoryCategory>();
|
||||
}
|
||||
|
||||
public InventoryCategory GetCategory(string categoryName)
|
||||
{
|
||||
return categoryMap.GetValueOrDefault(categoryName);
|
||||
}
|
||||
|
||||
public Dictionary<string, GameData.ItemData> GetItemsByCategory(string categoryName)
|
||||
{
|
||||
var result = new Dictionary<string, GameData.ItemData>();
|
||||
var category = GetCategory(categoryName);
|
||||
|
||||
if (category == null || GameData.Instance == null)
|
||||
{
|
||||
return result;
|
||||
}
|
||||
|
||||
foreach (var itemId in category.Items)
|
||||
{
|
||||
var itemData = GameData.Instance.GetItem(itemId);
|
||||
if (itemData != null)
|
||||
{
|
||||
result[itemId] = itemData;
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public override void _ExitTree()
|
||||
{
|
||||
if (Instance == this)
|
||||
{
|
||||
Instance = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user