diff --git a/Assets/Scripts/LevelConfig/WaveObserver.cs b/Assets/Scripts/LevelConfig/WaveObserver.cs index 04eda44..f1ee0e3 100644 --- a/Assets/Scripts/LevelConfig/WaveObserver.cs +++ b/Assets/Scripts/LevelConfig/WaveObserver.cs @@ -1,5 +1,6 @@ using System.Collections.Generic; using UnityEngine; +using System; public class WaveObserver : Singleton { @@ -9,6 +10,7 @@ public class WaveObserver : Singleton private WaveConfig _levelConfig; private const int MAXTOUGHNESS = 10; private int _spawnerTiming = 0; + private List _intervalTiming = new List(); public WaveConfig LevelConfig { set @@ -22,16 +24,23 @@ public class WaveObserver : Singleton } } + /** + * 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 { if (_levelConfig.ConstantSpawn[i].GetEnemyObject() == paramPrefab) { - currentCount = _copyConstantSpawn[i]--; + currentCount = --_copyConstantSpawn[i]; break; } } @@ -60,6 +69,10 @@ public class WaveObserver : Singleton } } + /** + * 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 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 { _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 } 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); + } } diff --git a/Assets/Scripts/Opponent/Opponent.cs b/Assets/Scripts/Opponent/Opponent.cs index e8b686e..24cd742 100644 --- a/Assets/Scripts/Opponent/Opponent.cs +++ b/Assets/Scripts/Opponent/Opponent.cs @@ -52,7 +52,7 @@ public class Opponent : Entity AttackSpeedWait += Time.deltaTime; } - private void OnDisable() + private void OnDestroy() { _observer.NotifyDies(_observerIndex, _toughness); } diff --git a/Assets/Scripts/Tiles/SpawnerTile.cs b/Assets/Scripts/Tiles/SpawnerTile.cs index e79be65..38450a7 100644 --- a/Assets/Scripts/Tiles/SpawnerTile.cs +++ b/Assets/Scripts/Tiles/SpawnerTile.cs @@ -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; diff --git a/Assets/WaveConfig/Config01.asset b/Assets/WaveConfig/Config01.asset index 827612f..371055b 100644 --- a/Assets/WaveConfig/Config01.asset +++ b/Assets/WaveConfig/Config01.asset @@ -14,5 +14,5 @@ MonoBehaviour: m_EditorClassIdentifier: _constantSpawn: - _enemy: {fileID: 313037212318601125, guid: 5bbf0d85fa5bb3f4599da79f0a84e3a9, type: 3} - _count: 20 - _gameDuration: 3 + _count: 5 + _gameDuration: 1