using System.Collections.Generic;
using Godot;
namespace ColdMint.scripts.inventory;
///
/// LootListManager
/// 战利品表管理器
///
public static class LootListManager
{
private static Dictionary? _lootListDictionary;
///
/// Register loot table
/// 注册战利品表
///
///
///
public static bool RegisterLootList(string id, LootList lootList)
{
if (_lootListDictionary != null) return _lootListDictionary.TryAdd(id, lootList);
_lootListDictionary = new Dictionary { { id, lootList } };
return true;
}
///
/// Get Loot List
/// 获取战利品表
///
///
///
public static LootList? GetLootList(string id)
{
return _lootListDictionary?.GetValueOrDefault(id);
}
///
/// Generate loot objects
/// 生成战利品对象
///
///
///lootDataArray
///战利品数组
///
///
///parentNode
///父节点
///
public static void GenerateLootObjects(LootData[] lootDataArray, Node parentNode)
{
if (lootDataArray.Length == 0)
{
return;
}
Dictionary packedSceneDictionary = new();
foreach (var lootData in lootDataArray)
{
if (string.IsNullOrEmpty(lootData.ResPath))
{
continue;
}
if (!packedSceneDictionary.TryGetValue(lootData.ResPath, out var packedScene))
{
packedScene = GD.Load(lootData.ResPath);
packedSceneDictionary.TryAdd(lootData.ResPath, packedScene);
}
CreateLootObject(packedScene, parentNode);
}
}
private static void CreateLootObject(PackedScene? packedScene, Node parent)
{
if (packedScene == null)
{
return;
}
var lootObject = packedScene.Instantiate();
if (lootObject is not Node2D node2D)
{
return;
}
parent.AddChild(node2D);
}
///
/// Remove loot list
/// 移除战利品表
///
///
///
public static bool UnregisterLootList(string id)
{
if (_lootListDictionary == null)
{
return false;
}
return _lootListDictionary.Remove(id);
}
}