60 lines
1.7 KiB
C#
60 lines
1.7 KiB
C#
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;
|
||
}
|
||
}
|
||
} |