mirror of
https://github.com/ryzij/Cube-Tower-Defense.git
synced 2026-07-14 01:56:57 +00:00
Добавил контроллер для отслеживания победы/поражения
This commit is contained in:
@@ -35,7 +35,7 @@ public class EnemySpawner : MonoBehaviour
|
||||
_gameManager.OnLevelChanged -= OnLevelChanged;
|
||||
}
|
||||
|
||||
private void OnLevelChanged(Level level) => StartCoroutine(SpawnCoroutine(level));
|
||||
private void OnLevelChanged(GameManager.OnLevelChangedEventArgs args) => StartCoroutine(SpawnCoroutine(args.Level));
|
||||
|
||||
private IEnumerator SpawnCoroutine(Level level)
|
||||
{
|
||||
|
||||
@@ -13,7 +13,7 @@ public class GameManager : MonoBehaviour
|
||||
[SerializeField] private Level[] _levels;
|
||||
[SerializeField] private SandPathBuildService _pathBuildService;
|
||||
[SerializeField] private UnityEvent<GameState> _onStateChanged;
|
||||
[SerializeField] private UnityEvent<Level> _onLevelChanged;
|
||||
[SerializeField] private UnityEvent<OnLevelChangedEventArgs> _onLevelChanged;
|
||||
|
||||
public event UnityAction<GameState> OnStateChanged
|
||||
{
|
||||
@@ -21,7 +21,7 @@ public class GameManager : MonoBehaviour
|
||||
remove => _onStateChanged.RemoveListener(value);
|
||||
}
|
||||
|
||||
public event UnityAction<Level> OnLevelChanged
|
||||
public event UnityAction<OnLevelChangedEventArgs> OnLevelChanged
|
||||
{
|
||||
add => _onLevelChanged.AddListener(value);
|
||||
remove => _onLevelChanged.RemoveListener(value);
|
||||
@@ -41,7 +41,8 @@ public class GameManager : MonoBehaviour
|
||||
}
|
||||
}
|
||||
|
||||
//public Level CurrentLevel => _levels[_currentLvlIndex];
|
||||
public int CurrentLevel => _currentLvlIndex + 1;
|
||||
public int TotalLevels => _levels.Length;
|
||||
|
||||
private void OnEnable()
|
||||
{
|
||||
@@ -61,6 +62,19 @@ public class GameManager : MonoBehaviour
|
||||
{
|
||||
_currentLvlIndex = (_currentLvlIndex + 1) % _levels.Length;
|
||||
CurrentState = GameState.Level;
|
||||
_onLevelChanged?.Invoke(_levels[_currentLvlIndex]);
|
||||
_onLevelChanged?.Invoke(new OnLevelChangedEventArgs
|
||||
{
|
||||
Level = _levels[_currentLvlIndex],
|
||||
CurrentLevel = CurrentLevel,
|
||||
TotalLevels = TotalLevels
|
||||
});
|
||||
}
|
||||
|
||||
public class OnLevelChangedEventArgs
|
||||
{
|
||||
public Level Level { get; set; }
|
||||
public int CurrentLevel { get; set; }
|
||||
public int TotalLevels { get; set; }
|
||||
public bool IsLastLevel => CurrentLevel == TotalLevels;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,66 @@
|
||||
using UnityEngine;
|
||||
using UnityEngine.Events;
|
||||
|
||||
public class WinLoseController : MonoBehaviour
|
||||
{
|
||||
[SerializeField] private GameManager _gameManager;
|
||||
[SerializeField] private SpawnDeathEnemyController _spawnDeathEnemyController;
|
||||
[SerializeField] private Tower _tower;
|
||||
[SerializeField] private UnityEvent<OnGameOverEventArgs> _onGameOver;
|
||||
[SerializeField] private UnityEvent _onWin;
|
||||
|
||||
public event UnityAction<OnGameOverEventArgs> OnGameOver
|
||||
{
|
||||
add => _onGameOver.AddListener(value);
|
||||
remove => _onGameOver.RemoveListener(value);
|
||||
}
|
||||
|
||||
public event UnityAction OnWin
|
||||
{
|
||||
add => _onWin.AddListener(value);
|
||||
remove => _onWin.RemoveListener(value);
|
||||
}
|
||||
|
||||
private bool _isLastLevel;
|
||||
private int _currentLevel;
|
||||
|
||||
private void OnEnable()
|
||||
{
|
||||
_gameManager.OnLevelChanged += OnLevelChanged;
|
||||
_spawnDeathEnemyController.OnLastEnemyDestroyed += OnLastEnemyDestroyed;
|
||||
_tower.OnGameOver += OnDeath;
|
||||
}
|
||||
|
||||
private void OnDisable()
|
||||
{
|
||||
_gameManager.OnLevelChanged -= OnLevelChanged;
|
||||
_spawnDeathEnemyController.OnLastEnemyDestroyed -= OnLastEnemyDestroyed;
|
||||
_tower.OnGameOver -= OnDeath;
|
||||
}
|
||||
|
||||
private void OnLastEnemyDestroyed()
|
||||
{
|
||||
if (_isLastLevel)
|
||||
_onWin?.Invoke();
|
||||
}
|
||||
|
||||
private void OnLevelChanged(GameManager.OnLevelChangedEventArgs args)
|
||||
{
|
||||
_isLastLevel = args.IsLastLevel;
|
||||
_currentLevel = args.CurrentLevel;
|
||||
}
|
||||
|
||||
private void OnDeath()
|
||||
{
|
||||
_onGameOver?.Invoke(new OnGameOverEventArgs(_currentLevel));
|
||||
}
|
||||
|
||||
public class OnGameOverEventArgs
|
||||
{
|
||||
public int CurrentLevel { get; }
|
||||
public OnGameOverEventArgs(int currentLevel)
|
||||
{
|
||||
CurrentLevel = currentLevel;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 199999b988ecd0f4a941aa248771f8ed
|
||||
Reference in New Issue
Block a user