Ader Alisma 01 6345febb35 Start reworking constant spawn
Made interval between spawns adapt to enemies spawned at the Start

Removed the random factor of interval to respect the game duration

Made max spawn per row dependent of enemy toughness on that row
2023-09-10 15:39:19 -04:00

109 lines
3.1 KiB
C#

using System.Collections.Generic;
using UnityEngine;
public class WaveObserver : Singleton<WaveObserver>
{
private List<SpawnerTile> _subjects = new List<SpawnerTile>();
private List<float> _aliveEnemyCount = new List<float>();
private List<int> _copyConstantSpawn;
private WaveConfig _levelConfig;
private const int MAXTOUGHNESS = 10;
private int _spawnerTiming = 0;
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.ChangeSpawnSpeed(_levelConfig.GetInterval() * ++_spawnerTiming);
_subjects.Add(spawnerSubject);
_aliveEnemyCount.Add(0);
Debug.Log("Je suis le " + _spawnerTiming + "e a agir!!!");
}
public void NotifySpawned(SpawnerTile spawnerSubject)
{
GameObject paramPrefab = spawnerSubject.Prefab;
spawnerSubject.ChangeSpawnSpeed(_levelConfig.GetInterval() * _spawnerTiming);
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, float toughness)
{
int index = FindEnemyIndex(yPosition);
_aliveEnemyCount[index] += toughness;
if (_aliveEnemyCount[index] >= MAXTOUGHNESS)
{
_subjects[index].StopSpawn();
}
return index;
}
public void NotifyDies(int position, float toughness)
{
_aliveEnemyCount[position] -= toughness;
if (_aliveEnemyCount[position] < MAXTOUGHNESS)
{
_subjects[position].StartSpawn();
}
Debug.Log("Fallen monster ");
}
/**
* Called when an enemy is spawned automatically at the start of the game
* Adjusts the intervall between spawns
*/
public void NotifyOnStart()
{
float interval = _levelConfig.GetUpdatedInterval();
foreach (var subject in _subjects)
{
subject.ChangeSpawnSpeed(interval);
}
}
private int FindEnemyIndex(float yPosition)
{
for (int i = 0; i < _subjects.Count; i++)
{
if (_subjects[i].Position.y == yPosition)
{
return i;
}
}
return -1;
}
}