198 lines
5.4 KiB
C#
198 lines
5.4 KiB
C#
using Godot;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
|
|
public partial class GameData : Node
|
|
{
|
|
// 物品分类枚举
|
|
public enum ItemCategory
|
|
{
|
|
RawMaterial, // 原材料
|
|
ProcessedMaterial, // 冶炼成品
|
|
Building, // 建筑
|
|
ProductionDevice, // 生产设备
|
|
Component, // 组件
|
|
Product // 产品
|
|
}
|
|
|
|
// 物品数据结构
|
|
public class ItemData
|
|
{
|
|
public string Id { get; set; } // 物品ID
|
|
public string Name { get; set; } // 物品名称
|
|
public ItemCategory Category { get; set; } // 物品分类
|
|
public string Description { get; set; } // 物品描述
|
|
public Dictionary<string, int> Recipe { get; set; } // 合成配方
|
|
public float CraftTime { get; set; } // 合成时间
|
|
public float PowerConsumption { get; set; } // 耗电量
|
|
public string IconPath { get; set; } // 图标路径
|
|
}
|
|
|
|
// 单例实例
|
|
private static GameData instance;
|
|
public static GameData Instance
|
|
{
|
|
get
|
|
{
|
|
return instance;
|
|
}
|
|
}
|
|
|
|
// 物品数据字典
|
|
private Dictionary<string, ItemData> items = new Dictionary<string, ItemData>();
|
|
|
|
public override void _Ready()
|
|
{
|
|
instance = this; // 设置单例实例
|
|
LoadItemsFromCSV();
|
|
}
|
|
|
|
// 从CSV文件加载物品数据
|
|
private void LoadItemsFromCSV()
|
|
{
|
|
try
|
|
{
|
|
// 读取CSV文件
|
|
string csvPath = "res://data/config/items.csv";
|
|
if (!Godot.FileAccess.FileExists(csvPath))
|
|
{
|
|
GD.PrintErr("物品数据文件不存在: " + csvPath);
|
|
return;
|
|
}
|
|
|
|
var file = Godot.FileAccess.Open(csvPath, Godot.FileAccess.ModeFlags.Read);
|
|
if (file == null)
|
|
{
|
|
GD.PrintErr("无法打开物品数据文件: " + csvPath);
|
|
return;
|
|
}
|
|
|
|
// 读取标题行
|
|
string header = file.GetLine();
|
|
string[] headers = header.Split(',');
|
|
|
|
// 读取数据行
|
|
while (!file.EofReached())
|
|
{
|
|
string line = file.GetLine();
|
|
if (string.IsNullOrEmpty(line)) continue;
|
|
|
|
string[] values = line.Split(',');
|
|
if (values.Length != headers.Length) continue;
|
|
|
|
// 创建物品数据
|
|
var item = new ItemData
|
|
{
|
|
Id = values[0],
|
|
Name = values[1],
|
|
Category = (ItemCategory)Enum.Parse(typeof(ItemCategory), values[2]),
|
|
Description = values[3],
|
|
Recipe = ParseRecipe(values[4]),
|
|
CraftTime = float.Parse(values[5]),
|
|
PowerConsumption = float.Parse(values[6]),
|
|
IconPath = values[7]
|
|
};
|
|
|
|
AddItem(item);
|
|
}
|
|
|
|
file.Close();
|
|
GD.Print("成功加载物品数据");
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
GD.PrintErr("加载物品数据时出错: " + e.Message);
|
|
}
|
|
}
|
|
|
|
// 解析配方字符串
|
|
private Dictionary<string, int> ParseRecipe(string recipeStr)
|
|
{
|
|
if (string.IsNullOrEmpty(recipeStr) || recipeStr == "null")
|
|
return null;
|
|
|
|
var recipe = new Dictionary<string, int>();
|
|
string[] pairs = recipeStr.Split(';');
|
|
|
|
foreach (string pair in pairs)
|
|
{
|
|
string[] parts = pair.Split(':');
|
|
if (parts.Length == 2)
|
|
{
|
|
string itemId = parts[0];
|
|
int amount = int.Parse(parts[1]);
|
|
recipe[itemId] = amount;
|
|
}
|
|
}
|
|
|
|
return recipe;
|
|
}
|
|
|
|
// 添加物品
|
|
private void AddItem(ItemData item)
|
|
{
|
|
items[item.Id] = item;
|
|
}
|
|
|
|
// 获取物品数据
|
|
public ItemData GetItem(string id)
|
|
{
|
|
if (items.ContainsKey(id))
|
|
{
|
|
return items[id];
|
|
}
|
|
return null;
|
|
}
|
|
|
|
// 获取所有物品
|
|
public Dictionary<string, ItemData> GetAllItems()
|
|
{
|
|
return items;
|
|
}
|
|
|
|
// 获取指定分类的所有物品
|
|
public Dictionary<string, ItemData> GetItemsByCategory(ItemCategory category)
|
|
{
|
|
var result = new Dictionary<string, ItemData>();
|
|
foreach (var item in items)
|
|
{
|
|
if (item.Value.Category == category)
|
|
{
|
|
result[item.Key] = item.Value;
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
|
|
// 检查是否有足够的材料进行合成
|
|
public bool HasEnoughMaterials(string itemId, Dictionary<string, int> inventory)
|
|
{
|
|
var item = GetItem(itemId);
|
|
if (item == null || item.Recipe == null) return false;
|
|
|
|
foreach (var requirement in item.Recipe)
|
|
{
|
|
if (!inventory.ContainsKey(requirement.Key) ||
|
|
inventory[requirement.Key] < requirement.Value)
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
|
|
// 计算合成所需时间
|
|
public float GetCraftTime(string itemId)
|
|
{
|
|
var item = GetItem(itemId);
|
|
return item?.CraftTime ?? 0f;
|
|
}
|
|
|
|
// 计算合成所需电力
|
|
public float GetPowerConsumption(string itemId)
|
|
{
|
|
var item = GetItem(itemId);
|
|
return item?.PowerConsumption ?? 0f;
|
|
}
|
|
} |