Reworked constant spawn

One row will spawn at a set interval

Maximum enemy per row determined by active toughness on that row
This commit is contained in:
Ader Alisma 01 2023-09-17 18:56:30 -04:00
parent 6345febb35
commit 56410139fd
4 changed files with 59 additions and 14 deletions

View File

@ -1,5 +1,6 @@
using System.Collections.Generic;
using UnityEngine;
using System;
public class WaveObserver : Singleton<WaveObserver>
{
@ -9,6 +10,7 @@ public class WaveObserver : Singleton<WaveObserver>
private WaveConfig _levelConfig;
private const int MAXTOUGHNESS = 10;
private int _spawnerTiming = 0;
private List<int> _intervalTiming = new List<int>();
public WaveConfig LevelConfig
{
set
@ -22,16 +24,23 @@ public class WaveObserver : Singleton<WaveObserver>
}
}
/**
* Called by spawner at the start of the game
* Assigns enemy to spawn and registers them
*/
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!!!");
_intervalTiming.Add(++_spawnerTiming);
}
/**
* Called by spawner when making enemies
* Assigns a new interval
*/
public void NotifySpawned(SpawnerTile spawnerSubject)
{
GameObject paramPrefab = spawnerSubject.Prefab;
@ -43,7 +52,7 @@ public class WaveObserver : Singleton<WaveObserver>
{
if (_levelConfig.ConstantSpawn[i].GetEnemyObject() == paramPrefab)
{
currentCount = _copyConstantSpawn[i]--;
currentCount = --_copyConstantSpawn[i];
break;
}
}
@ -60,6 +69,10 @@ public class WaveObserver : Singleton<WaveObserver>
}
}
/**
* Called by enemy when they spawn
* Keeps track of their row
*/
public int NotifyEnemy(float yPosition, float toughness)
{
int index = FindEnemyIndex(yPosition);
@ -71,6 +84,10 @@ public class WaveObserver : Singleton<WaveObserver>
return index;
}
/**
* Called when an enemy dies
* Reactivates spawning on that row if disabled before
*/
public void NotifyDies(int position, float toughness)
{
_aliveEnemyCount[position] -= toughness;
@ -78,22 +95,23 @@ public class WaveObserver : Singleton<WaveObserver>
{
_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()
public void NotifyOnStart(SpawnerTile spawnerSubject)
{
float interval = _levelConfig.GetUpdatedInterval();
foreach (var subject in _subjects)
{
subject.ChangeSpawnSpeed(interval);
}
NotifySpawned(spawnerSubject);
}
// To find which spawner an ennemy came from.
private int FindEnemyIndex(float yPosition)
{
for (int i = 0; i < _subjects.Count; i++)
@ -105,4 +123,20 @@ public class WaveObserver : Singleton<WaveObserver>
}
return -1;
}
/**
* Spawners wait 1 second to have time to register them all
* Then gets assigned a random spawn interval
*/
public void NotifyEndCooldown(SpawnerTile spawnerTile)
{
System.Random rand = new System.Random();
int index;
do
{
index = rand.Next(_subjects.Count);
} while (_intervalTiming.Count <= index);
spawnerTile.ChangeSpawnSpeed(_levelConfig.GetInterval() * _intervalTiming[index]);
_intervalTiming.Remove(index);
}
}

View File

@ -52,7 +52,7 @@ public class Opponent : Entity
AttackSpeedWait += Time.deltaTime;
}
private void OnDisable()
private void OnDestroy()
{
_observer.NotifyDies(_observerIndex, _toughness);
}

View File

@ -10,11 +10,12 @@ public class SpawnerTile : LevelTile
private bool _spawnOnStart;
private float _lifetime;
[SerializeField]
private float _spawnSpeed = 0;
private float _spawnSpeed = 1.0f;
[SerializeField, Range(0, 1.001f)]
private float _spawnCounter = 0;
private WaveObserver _observer;
private bool stopped = false;
private bool _stopped = false;
private bool _cooldownEnded = false;
public override void LevelStart()
@ -24,20 +25,26 @@ public class SpawnerTile : LevelTile
if (_spawnOnStart && _lifetime <= 0)
{
_prefab.Create(Position, parent: LevelManager.Instance.LevelTransform);
_observer.NotifyOnStart();
_observer.NotifyOnStart(this);
}
}
public override void LevelUpdate()
{
_lifetime += Time.deltaTime;
if (!stopped)
if (!_stopped)
{
_spawnCounter += Time.deltaTime;
}
if (_spawnCounter < _spawnSpeed) return;
_spawnCounter = 0;
if (!_cooldownEnded)
{
_observer.NotifyEndCooldown(this);
_cooldownEnded = true;
return;
}
_prefab.Create(Position, parent: LevelManager.Instance.LevelTransform);
_observer.NotifySpawned(this);
}
@ -62,12 +69,12 @@ public class SpawnerTile : LevelTile
internal void StopSpawn()
{
stopped = true;
_stopped = true;
}
internal void StartSpawn()
{
stopped = false;
_stopped = false;
}
@ -95,6 +102,10 @@ public class SpawnerTile : LevelTile
}
}
/**
* Called by observer
* Assigns a new spawn interval
*/
public void ChangeSpawnSpeed(float value)
{
_spawnSpeed = value;

View File

@ -14,5 +14,5 @@ MonoBehaviour:
m_EditorClassIdentifier:
_constantSpawn:
- _enemy: {fileID: 313037212318601125, guid: 5bbf0d85fa5bb3f4599da79f0a84e3a9, type: 3}
_count: 20
_gameDuration: 3
_count: 5
_gameDuration: 1