using System.Collections.Generic;
using ColdMint.scripts.utils;
using Godot;
namespace ColdMint.scripts.inventory;
///
/// LootListManager
/// 战利品表管理器
///
public static class LootListManager
{
private static Dictionary? _lootListDictionary;
///
/// Register loot table
/// 注册战利品表
///
///
public static bool RegisterLootList(LootList lootList)
{
var id = lootList.Id;
if (id == null)
{
return false;
}
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
/// 生成战利品对象
///
public static void GenerateLootObjects(Node parentNode, LootData[] lootDataArray, Vector2 position)
{
if (lootDataArray.Length == 0)
{
return;
}
//Cache the loaded PackedScene object.
//缓存已加载的PackedScene对象。
Dictionary packedSceneDictionary = new();
foreach (var lootData in lootDataArray)
{
if (string.IsNullOrEmpty(lootData.ResPath) || lootData.Quantity == 0)
{
continue;
}
if (!packedSceneDictionary.TryGetValue(lootData.ResPath, out var packedScene))
{
packedScene = GD.Load(lootData.ResPath);
packedSceneDictionary.TryAdd(lootData.ResPath, packedScene);
}
for (var i = 0; i < lootData.Quantity; i++)
{
//Generate as many loot instance objects as there are loot.
//有多少个战利品就生成多少个战利品实例对象。
CreateLootInstanceObject(parentNode, packedScene, position);
}
}
}
///
/// Create a loot instance object
/// 创建战利品实例对象
///
private static void CreateLootInstanceObject(Node parent, PackedScene? packedScene, Vector2 position)
{
if (packedScene == null)
{
return;
}
var lootObject = NodeUtils.InstantiatePackedScene(packedScene, parent);
if (lootObject == null)
{
return;
}
lootObject.Position = position;
}
///
/// Remove loot list
/// 移除战利品表
///
///
///
public static bool UnregisterLootList(string id)
{
if (_lootListDictionary == null)
{
return false;
}
return _lootListDictionary.Remove(id);
}
}