using System; using System.Collections.Generic; using UnityEngine; [Serializable] public class ScoreModel { public int Score; public List ShipsCounters; public event Action OnUpdate; public event Action OnNewScore; public ScoreModel(List enemyShips) { Score = 0; ShipsCounters = new(); //Собираем сет используемых типов кораблей HashSet shipTypes = new(); foreach (EnemyShip enemy in enemyShips) shipTypes.Add(enemy.Type); //Заполняем модель счетчиками foreach (var shipType in shipTypes) ShipsCounters.Add(new DestroyedShipsCounter(shipType)); EnemyShip.ShipDefeated += ShipDestroyedHandler; } public void ShipDestroyedHandler(EnemyShip destroyedShip) { Score += destroyedShip.Score; OnNewScore?.Invoke(Score); var counter = ShipsCounters.Find(counter => counter.ShipType == destroyedShip.Type); if (counter != null) { counter.Count++; OnUpdate?.Invoke(this); } else Debug.LogWarning($"Не найден счетчик для типа корабля \"{destroyedShip.Type}\""); } public DestroyedShipsCounter GetCounterByType(EnemyShip.ShipType shiptype) => ShipsCounters.Find(s => s.ShipType == shiptype); [Serializable] public class DestroyedShipsCounter { public EnemyShip.ShipType ShipType; public int Count; public DestroyedShipsCounter(EnemyShip.ShipType shipType) { this.ShipType = shipType; Count = 0; } } }