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