Files
TheVVaS-Assets/RPGCoreCommon/DropTable/Runtime/DropTableGroup.cs
T
2026-04-25 23:37:10 +02:00

58 lines
1.8 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using RPGCoreCommon.Helpers;
using UnityEngine;
using Object = UnityEngine.Object;
namespace RPGCoreCommon.DropTable
{
[Serializable]
internal class DropTableGroup
{
// Rules for randomizing
[SerializeField] internal ChanceTypeEnum chanceType = ChanceTypeEnum.ByPercent;
// SubGroups and/or items of this group
[SerializeField] internal List<DropTableGroupRow> rows = new();
internal void Get(ref Dictionary<Object, int> result)
{
if (!rows.Any())
{
Debug.LogWarning($"Empty <b>{nameof(DropTableGroup)}</b>!");
return;
}
foreach (var row in GetRows())
{
if (row.item)
{
result.TryAdd(row.item, 0);
result[row.item] += row.count;
}
else if (row.subGroupRef)
{
for (var i = 0; i < row.count; i++)
row.subGroupRef.group.Get(ref result);
}
else if (row.subGroupInline != null)
{
for (var i = 0; i < row.count; i++)
row.subGroupInline.Get(ref result);
}
}
}
internal List<DropTableGroupRow> GetRows()
{
return chanceType switch
{
ChanceTypeEnum.All => rows,
ChanceTypeEnum.ByWeight => RandomHelper.RandomElementsByWeight(rows, row => row.chance, 1),
ChanceTypeEnum.ByPercent => rows.Where(row => RandomHelper.Chance(row.chance/100)).ToList(),
_ => throw new ArgumentOutOfRangeException()
};
}
}
}