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
This commit is contained in:
Ader Alisma 01 2023-09-10 15:39:19 -04:00
parent fcc29ad88a
commit 6345febb35
4 changed files with 72 additions and 44 deletions

View File

@ -9,6 +9,8 @@ public class WaveConfig : ScriptableObject
private List<EnemyType> _constantSpawn = new List<EnemyType>();
[SerializeField]
private float _gameDuration = 0;
private float _enemySpawndOnStart = 0;
private int _enemySum = 0;
public List<EnemyType> ConstantSpawn
{
get
@ -18,7 +20,13 @@ public class WaveConfig : ScriptableObject
}
public float GetInterval()
{
float interval = SumCount().ToFloat() / _gameDuration * 0.01f;
float interval = _gameDuration * 60.0f / (_enemySum > 0 ? _enemySum.ToFloat() : SumCount()) ;
return interval;
}
public float GetUpdatedInterval()
{
_enemySpawndOnStart++;
float interval = Mathf.Max(_gameDuration * 60.0f / (_enemySum - _enemySpawndOnStart).ToFloat(), 0);
return interval;
}
public EnemyType GetRandomSpawn()
@ -29,15 +37,13 @@ public class WaveConfig : ScriptableObject
}
return _constantSpawn[Random.Range(0, _constantSpawn.Count - 1)];
}
private int SumCount()
private float SumCount()
{
int sum = 0;
foreach (EnemyType enemy in _constantSpawn)
{
sum += enemy.Count;
_enemySum += enemy.Count;
}
return sum;
return _enemySum.ToFloat();
}
}

View File

@ -4,40 +4,44 @@ using UnityEngine;
public class WaveObserver : Singleton<WaveObserver>
{
private List<SpawnerTile> _subjects = new List<SpawnerTile>();
private List<int> _aliveEnemyCount = new List<int>();
private List<float> _aliveEnemyCount = new List<float>();
private List<int> _copyConstantSpawn;
private WaveConfig _levelConfig;
private const int MAXENEMYCOUNT = 3;
public WaveConfig LevelConfig
{
set
{
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.InitialSpawnSpeed(_levelConfig.GetInterval());
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)
if (_levelConfig.ConstantSpawn[i].GetEnemyObject() == paramPrefab)
{
currentCount = _copyConstantSpawn[i]--;
break;
@ -56,29 +60,49 @@ public class WaveObserver : Singleton<WaveObserver>
}
}
public int NotifyEnemy(float yPosition)
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)
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]--;
}
}

View File

@ -8,7 +8,8 @@ public class Opponent : Entity
private Vector2 _movementVector = Vector2.zero;
private Rigidbody2D _rigidbody;
private WaveObserver _observer;
private int _index;
private int _observerIndex;
private float _toughness;
public override void Start()
{
@ -17,7 +18,8 @@ public class Opponent : Entity
_rigidbody = GetComponent<Rigidbody2D>();
Animation = gameObject.AddComponent<AnimationEntity>();
_observer = WaveObserver.Instance;
_index = _observer.NotifyEnemy(transform.position.y);
_toughness = Mathf.Round((base.Hp / 10) / 2);
_observerIndex = _observer.NotifyEnemy(transform.position.y, _toughness);
}
void Update()
@ -52,7 +54,7 @@ public class Opponent : Entity
private void OnDisable()
{
_observer.NotifyDies(_index);
_observer.NotifyDies(_observerIndex, _toughness);
}
}

View File

@ -14,9 +14,6 @@ public class SpawnerTile : LevelTile
[SerializeField, Range(0, 1.001f)]
private float _spawnCounter = 0;
private WaveObserver _observer;
private float _initialSpawnSpeed;
private const float MAX_SPAWN_SPEED = 0.01f;
private const float RANDOM_MODIFIER = 5.0f;
private bool stopped = false;
@ -27,6 +24,7 @@ public class SpawnerTile : LevelTile
if (_spawnOnStart && _lifetime <= 0)
{
_prefab.Create(Position, parent: LevelManager.Instance.LevelTransform);
_observer.NotifyOnStart();
}
}
@ -35,13 +33,12 @@ public class SpawnerTile : LevelTile
_lifetime += Time.deltaTime;
if (!stopped)
{
_spawnCounter += Time.deltaTime * _spawnSpeed;
_spawnCounter += Time.deltaTime;
}
if (_spawnCounter < 1) return;
if (_spawnCounter < _spawnSpeed) return;
_spawnCounter = 0;
_prefab.Create(Position, parent: LevelManager.Instance.LevelTransform);
_spawnSpeed = Mathf.Max(_initialSpawnSpeed / Random.Range(1.0f, RANDOM_MODIFIER), MAX_SPAWN_SPEED);
_observer.NotifySpawned(this);
}
public override bool Equals(ILevelObject other)
@ -98,9 +95,8 @@ public class SpawnerTile : LevelTile
}
}
public void InitialSpawnSpeed(float value)
public void ChangeSpawnSpeed(float value)
{
_initialSpawnSpeed = value;
_spawnSpeed = Mathf.Max(_initialSpawnSpeed / Random.Range(1.0f, RANDOM_MODIFIER - 2.0f), MAX_SPAWN_SPEED);
_spawnSpeed = value;
}
}