169 lines
5.2 KiB
C#
169 lines
5.2 KiB
C#
using Godot;
|
||
using System;
|
||
using System.Collections.Generic;
|
||
using System.Text.Json;
|
||
|
||
public partial class ResourceCategoryManager : Node
|
||
{
|
||
// 资源分类数据结构
|
||
public class ResourceCategory
|
||
{
|
||
public string CategoryName { get; set; }
|
||
public List<string> ItemIds { get; set; }
|
||
}
|
||
|
||
// 单例实例
|
||
private static ResourceCategoryManager instance;
|
||
public static ResourceCategoryManager Instance
|
||
{
|
||
get
|
||
{
|
||
return instance;
|
||
}
|
||
}
|
||
|
||
// 资源分类列表
|
||
private List<ResourceCategory> categories = new List<ResourceCategory>();
|
||
|
||
public override void _Ready()
|
||
{
|
||
GD.Print("ResourceCategoryManager _Ready 开始");
|
||
instance = this; // 设置单例实例
|
||
LoadResourceCategories();
|
||
}
|
||
|
||
// 从JSON文件加载资源分类配置
|
||
private void LoadResourceCategories()
|
||
{
|
||
try
|
||
{
|
||
string jsonPath = "res://data/config/resource_categories.json";
|
||
GD.Print($"尝试加载配置文件: {jsonPath}");
|
||
|
||
if (!Godot.FileAccess.FileExists(jsonPath))
|
||
{
|
||
GD.PrintErr("资源分类配置文件不存在: " + jsonPath);
|
||
return;
|
||
}
|
||
|
||
var file = Godot.FileAccess.Open(jsonPath, Godot.FileAccess.ModeFlags.Read);
|
||
if (file == null)
|
||
{
|
||
GD.PrintErr("无法打开资源分类配置文件: " + jsonPath);
|
||
return;
|
||
}
|
||
|
||
string jsonContent = file.GetAsText();
|
||
file.Close();
|
||
|
||
// GD.Print($"JSON文件内容长度: {jsonContent.Length}");
|
||
// GD.Print($"JSON文件内容: {jsonContent}");
|
||
|
||
// 解析JSON
|
||
var options = new JsonSerializerOptions
|
||
{
|
||
PropertyNameCaseInsensitive = true
|
||
};
|
||
|
||
categories = JsonSerializer.Deserialize<List<ResourceCategory>>(jsonContent, options);
|
||
|
||
if (categories == null)
|
||
{
|
||
GD.PrintErr("JSON反序列化结果为null");
|
||
categories = new List<ResourceCategory>();
|
||
return;
|
||
}
|
||
|
||
GD.Print($"成功加载资源分类配置,共 {categories.Count} 个分类");
|
||
|
||
// 打印每个分类的详细信息
|
||
foreach (var category in categories)
|
||
{
|
||
GD.Print($"分类: {category.CategoryName}, 物品数量: {category.ItemIds?.Count ?? 0}");
|
||
if (category.ItemIds != null)
|
||
{
|
||
foreach (var itemId in category.ItemIds)
|
||
{
|
||
GD.Print($" - {itemId}");
|
||
}
|
||
}
|
||
}
|
||
}
|
||
catch (Exception e)
|
||
{
|
||
GD.PrintErr("加载资源分类配置时出错: " + e.Message);
|
||
GD.PrintErr("堆栈跟踪: " + e.StackTrace);
|
||
}
|
||
}
|
||
|
||
// 获取所有分类
|
||
public List<ResourceCategory> GetAllCategories()
|
||
{
|
||
GD.Print($"GetAllCategories 被调用,返回 {categories.Count} 个分类");
|
||
return categories;
|
||
}
|
||
|
||
// 根据分类名称获取分类
|
||
public ResourceCategory GetCategoryByName(string categoryName)
|
||
{
|
||
return categories.Find(c => c.CategoryName == categoryName);
|
||
}
|
||
|
||
// 根据物品ID获取所属分类
|
||
public ResourceCategory GetCategoryByItemId(string itemId)
|
||
{
|
||
return categories.Find(c => c.ItemIds.Contains(itemId));
|
||
}
|
||
|
||
// 获取指定分类的所有物品数据
|
||
public Dictionary<string, GameData.ItemData> GetItemsByCategory(string categoryName)
|
||
{
|
||
GD.Print($"GetItemsByCategory 被调用,分类名称: {categoryName}");
|
||
|
||
var category = GetCategoryByName(categoryName);
|
||
if (category == null)
|
||
{
|
||
GD.PrintErr($"未找到分类: {categoryName}");
|
||
return new Dictionary<string, GameData.ItemData>();
|
||
}
|
||
|
||
GD.Print($"找到分类 {categoryName},包含 {category.ItemIds?.Count ?? 0} 个物品ID");
|
||
|
||
// 检查GameData.Instance
|
||
if (GameData.Instance == null)
|
||
{
|
||
GD.PrintErr("GameData.Instance 为 null!");
|
||
return new Dictionary<string, GameData.ItemData>();
|
||
}
|
||
|
||
var result = new Dictionary<string, GameData.ItemData>();
|
||
foreach (string itemId in category.ItemIds)
|
||
{
|
||
// GD.Print($"尝试获取物品: {itemId}");
|
||
var item = GameData.Instance.GetItem(itemId);
|
||
if (item != null)
|
||
{
|
||
GD.Print($"成功获取物品: {itemId} - {item.Name}");
|
||
result[itemId] = item;
|
||
}
|
||
else
|
||
{
|
||
GD.PrintErr($"未找到物品: {itemId}");
|
||
}
|
||
}
|
||
|
||
GD.Print($"GetItemsByCategory 返回 {result.Count} 个物品");
|
||
return result;
|
||
}
|
||
|
||
// 获取分类名称列表
|
||
public List<string> GetCategoryNames()
|
||
{
|
||
var names = new List<string>();
|
||
foreach (var category in categories)
|
||
{
|
||
names.Add(category.CategoryName);
|
||
}
|
||
return names;
|
||
}
|
||
} |