From 6345febb355ee9639d96c9101314169f7b1be0fc Mon Sep 17 00:00:00 2001 From: Ader Alisma 01 Date: Sun, 10 Sep 2023 15:39:19 -0400 Subject: [PATCH] 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 --- Assets/Scripts/LevelConfig/WaveConfig.cs | 18 +++-- Assets/Scripts/LevelConfig/WaveObserver.cs | 76 ++++++++++++++-------- Assets/Scripts/Opponent/Opponent.cs | 8 ++- Assets/Scripts/Tiles/SpawnerTile.cs | 14 ++-- 4 files changed, 72 insertions(+), 44 deletions(-) diff --git a/Assets/Scripts/LevelConfig/WaveConfig.cs b/Assets/Scripts/LevelConfig/WaveConfig.cs index 960f433..e4bc275 100644 --- a/Assets/Scripts/LevelConfig/WaveConfig.cs +++ b/Assets/Scripts/LevelConfig/WaveConfig.cs @@ -9,6 +9,8 @@ public class WaveConfig : ScriptableObject private List _constantSpawn = new List(); [SerializeField] private float _gameDuration = 0; + private float _enemySpawndOnStart = 0; + private int _enemySum = 0; public List 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(); } } diff --git a/Assets/Scripts/LevelConfig/WaveObserver.cs b/Assets/Scripts/LevelConfig/WaveObserver.cs index fced3ae..04eda44 100644 --- a/Assets/Scripts/LevelConfig/WaveObserver.cs +++ b/Assets/Scripts/LevelConfig/WaveObserver.cs @@ -4,40 +4,44 @@ using UnityEngine; public class WaveObserver : Singleton { private List _subjects = new List(); - private List _aliveEnemyCount = new List(); + private List _aliveEnemyCount = new List(); private List _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(); 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 } } - 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]--; - } } diff --git a/Assets/Scripts/Opponent/Opponent.cs b/Assets/Scripts/Opponent/Opponent.cs index 25de0fd..e8b686e 100644 --- a/Assets/Scripts/Opponent/Opponent.cs +++ b/Assets/Scripts/Opponent/Opponent.cs @@ -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(); Animation = gameObject.AddComponent(); _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); } } diff --git a/Assets/Scripts/Tiles/SpawnerTile.cs b/Assets/Scripts/Tiles/SpawnerTile.cs index e12f99c..e79be65 100644 --- a/Assets/Scripts/Tiles/SpawnerTile.cs +++ b/Assets/Scripts/Tiles/SpawnerTile.cs @@ -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; } } \ No newline at end of file