using System; using System.Collections.Generic; using YachtDice.Dice; namespace YachtDice.Player { public class DiceCollection { private readonly List ownedDice = new(); public event Action OnChanged; public IReadOnlyList OwnedDice => ownedDice; public void Add(DiceDefinitionSO definition) { if (definition == null) return; if (OwnsById(definition.Id)) return; ownedDice.Add(definition); OnChanged?.Invoke(); } public void Remove(DiceDefinitionSO definition) { if (ownedDice.Remove(definition)) OnChanged?.Invoke(); } public bool OwnsById(string id) { for (int i = 0; i < ownedDice.Count; i++) { if (ownedDice[i] != null && ownedDice[i].Id == id) return true; } return false; } public List GetSaveData() { var ids = new List(); for (int i = 0; i < ownedDice.Count; i++) ids.Add(ownedDice[i].Id); return ids; } public void LoadSaveData(List diceIds, DiceCatalog catalog) { ownedDice.Clear(); if (diceIds == null) { OnChanged?.Invoke(); return; } for (int i = 0; i < diceIds.Count; i++) { var def = catalog.FindById(diceIds[i]); if (def != null) ownedDice.Add(def); } OnChanged?.Invoke(); } public void Clear() { ownedDice.Clear(); OnChanged?.Invoke(); } } }