60 lines
1.7 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System;
using System.Collections.Generic;
using UnityEngine;
[Serializable]
public class ScoreModel
{
public int Score;
public List<DestroyedShipsCounter> ShipsCounters;
public event Action<ScoreModel> OnUpdate;
public event Action<int> OnNewScore;
public ScoreModel(List<EnemyShip> enemyShips)
{
Score = 0;
ShipsCounters = new();
//Собираем сет используемых типов кораблей
HashSet<EnemyShip.ShipType> 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;
}
}
}