85 lines
2.4 KiB
C#
85 lines
2.4 KiB
C#
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
public class WaveObserver : Singleton<WaveObserver>
|
|
{
|
|
private List<SpawnerTile> _subjects = new List<SpawnerTile>();
|
|
private List<int> _aliveEnemyCount = new List<int>();
|
|
private List<int> _copyConstantSpawn;
|
|
private WaveConfig _levelConfig;
|
|
private const int MAXENEMYCOUNT = 3;
|
|
public WaveConfig LevelConfig
|
|
{
|
|
set
|
|
{
|
|
_levelConfig = value;
|
|
_copyConstantSpawn = new List<int>();
|
|
foreach (EnemyType enemy in _levelConfig.ConstantSpawn)
|
|
{
|
|
_copyConstantSpawn.Add(enemy.Count);
|
|
}
|
|
}
|
|
}
|
|
|
|
public void Attach(SpawnerTile spawnerSubject)
|
|
{
|
|
spawnerSubject.Prefab = _levelConfig.GetRandomSpawn().GetEnemyObject();
|
|
spawnerSubject.InitialSpawnSpeed(_levelConfig.GetInterval());
|
|
_subjects.Add(spawnerSubject);
|
|
_aliveEnemyCount.Add(0);
|
|
}
|
|
|
|
public void NotifySpawned(SpawnerTile spawnerSubject)
|
|
{
|
|
GameObject paramPrefab = spawnerSubject.Prefab;
|
|
if (paramPrefab.Equals(_levelConfig.ConstantSpawn[0].GetEnemyObject()))
|
|
{
|
|
int currentCount = 0;
|
|
for (int i = 0; i < _copyConstantSpawn.Count; i++)
|
|
{
|
|
if(_levelConfig.ConstantSpawn[i].GetEnemyObject() == paramPrefab)
|
|
{
|
|
currentCount = _copyConstantSpawn[i]--;
|
|
break;
|
|
}
|
|
}
|
|
if (currentCount <= 0)
|
|
{
|
|
foreach (SpawnerTile spawner in _subjects)
|
|
{
|
|
if (spawner.Prefab.Equals(paramPrefab))
|
|
{
|
|
spawner.StopSpawn();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
public int NotifyEnemy(float yPosition)
|
|
{
|
|
for (int i = 0; i < _subjects.Count; i++)
|
|
{
|
|
if(_subjects[i].Position.y == yPosition)
|
|
{
|
|
_aliveEnemyCount[i]++;
|
|
if(_aliveEnemyCount[i] >= MAXENEMYCOUNT)
|
|
{
|
|
_subjects[i].StopSpawn();
|
|
}
|
|
return i;
|
|
}
|
|
}
|
|
return -1;
|
|
}
|
|
|
|
public void NotifyDies(int position)
|
|
{
|
|
if(_aliveEnemyCount[position] >= MAXENEMYCOUNT)
|
|
{
|
|
_subjects[position].StartSpawn();
|
|
}
|
|
_aliveEnemyCount[position]--;
|
|
}
|
|
}
|