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 rows = new(); internal void Get(ref Dictionary result) { if (!rows.Any()) { Debug.LogWarning($"Empty {nameof(DropTableGroup)}!"); 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 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() }; } } }