From 7edbec43b35029ebcd5391e36f868065a7fadd4e Mon Sep 17 00:00:00 2001 From: Ader Alisma 01 Date: Sun, 12 Nov 2023 16:18:52 -0500 Subject: [PATCH] End groupSpawn delay MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Adds a ~3 seconds delay when a group has more ennemies than available rows·. Added groupSpawnDelay to GlobalConfigFile --- Assets/Scripts/General/GlobalConfigFile.cs | 3 + Assets/Scripts/LevelConfig/WaveObserver.cs | 66 +++++++++------------- Assets/Scripts/Tiles/SpawnerTile.cs | 12 ++-- 3 files changed, 36 insertions(+), 45 deletions(-) diff --git a/Assets/Scripts/General/GlobalConfigFile.cs b/Assets/Scripts/General/GlobalConfigFile.cs index 745bf97..2669970 100644 --- a/Assets/Scripts/General/GlobalConfigFile.cs +++ b/Assets/Scripts/General/GlobalConfigFile.cs @@ -16,6 +16,9 @@ public class GlobalConfigFile : ScriptableObject public Vector2 enemyRangeMultiplier = Vector2.one; public float enemyAttackSpeedMultiplier = 1; + [Header("Spawn")] + public float groupSpawnDelay = 0.05f; + [Header("Allies")] public float allyDamageMultiplier = 1; public float allyLifeMultiplier = 1; diff --git a/Assets/Scripts/LevelConfig/WaveObserver.cs b/Assets/Scripts/LevelConfig/WaveObserver.cs index 9b12dd7..67827bd 100644 --- a/Assets/Scripts/LevelConfig/WaveObserver.cs +++ b/Assets/Scripts/LevelConfig/WaveObserver.cs @@ -6,7 +6,7 @@ public class WaveObserver : Singleton private List _subjects = new List(); private List _aliveEnemyCount = new List(); private List _copyConstantSpawn; - private List> _copyGroupSpawn; + private List> _copyGroupSpawn; //Contains count of enemies per group private List _groupSpawnTimers; private WaveConfig _levelConfig; private const int MAXTOUGHNESS = 10; @@ -17,7 +17,7 @@ public class WaveObserver : Singleton public void SetLevelConfig(WaveConfig config) { - _levelConfig = config; + _levelConfig = Object.Instantiate(config); _copyConstantSpawn = new List(); _copyGroupSpawn = new List>(); _groupSpawnTimers = new List(); @@ -164,53 +164,43 @@ public class WaveObserver : Singleton */ public bool NotifyGroupSpawn() { - System.Random rand = new System.Random(); List usedRows = new List(); List currentGroup = _levelConfig.NestedGroupSpawn[_currentGroupIndex].groupSpawn; for (int groupIndex = 0; groupIndex < currentGroup.Count; groupIndex++) //Loops through enemy groups { - if (_copyGroupSpawn[_currentGroupIndex][groupIndex] == 0) - if (usedRows.Count < _subjects.Count) //TODO: decreases actual count + goes into negatives + if (_copyGroupSpawn[_currentGroupIndex][groupIndex] != 0) { - for (int i = 0; i < _subjects.Count; i++) //Loops through spawners - { - bool breakTag = false; - do - { - int currentRow = rand.Next(_subjects.Count); - if (!usedRows.Contains(currentRow)) - { - _subjects[rand.Next(_subjects.Count)].TriggerSpawn(currentGroup[groupIndex].GetEnemyObject()); - _copyGroupSpawn[_currentGroupIndex][groupIndex]--; - Debug.Log(_copyGroupSpawn[_currentGroupIndex][groupIndex]); - usedRows.Add(currentRow); - } - if(_copyGroupSpawn[_currentGroupIndex][groupIndex] == 0) //If current ennemy has reached count of 0 - { - breakTag = true; - break; - } - }while(usedRows.Count==i); - Debug.Log("Row: " + usedRows.Count); - if (breakTag) - { - break; - } - } - if (_copyGroupSpawn[_currentGroupIndex][groupIndex] > 0) //If after doing evry row, there are still some left + CycleRows(usedRows, currentGroup, groupIndex); + /*If group is done OR max rows reached while group is not done*/ + if (_copyGroupSpawn[_currentGroupIndex][groupIndex] > 0 || (usedRows.Count == _subjects.Count && _copyGroupSpawn[_currentGroupIndex][groupIndex] > 0)) { return false; } - else - { - break; - } - } else - { - return false; } } _currentGroupIndex++; return true; } + + /** + * Called to go through every row randomly without duplicate rows + */ + private void CycleRows(List usedRows, List currentGroup, int groupIndex) + { + System.Random rand = new System.Random(); + while (usedRows.Count < _subjects.Count) + { + int currentRow = rand.Next(_subjects.Count); + if (!usedRows.Contains(currentRow)) //If picked row has laready been used + { + _subjects[currentRow].TriggerSpawn(currentGroup[groupIndex].GetEnemyObject()); + _copyGroupSpawn[_currentGroupIndex][groupIndex]--; + usedRows.Add(currentRow); + } + if (_copyGroupSpawn[_currentGroupIndex][groupIndex] == 0) //If current ennemy has reached count of 0 + { + break; + } + } + } } diff --git a/Assets/Scripts/Tiles/SpawnerTile.cs b/Assets/Scripts/Tiles/SpawnerTile.cs index 56376aa..fdeac6c 100644 --- a/Assets/Scripts/Tiles/SpawnerTile.cs +++ b/Assets/Scripts/Tiles/SpawnerTile.cs @@ -21,7 +21,7 @@ public class SpawnerTile : LevelTile public void SetGroupSpawnTimers(List groupSpawnTimers) { - for (int i = 0; i < groupSpawnTimers.Count-1; i++) + for (int i = 0; i < groupSpawnTimers.Count; i++) { _groupSpawnTimers.Add(groupSpawnTimers[i]); } @@ -48,17 +48,15 @@ public class SpawnerTile : LevelTile } if (_groupSpawnTimers.Count > 0) { - if (_lifetime / 60 > _groupSpawnTimers[0]) + if (_lifetime / 60.0f > _groupSpawnTimers[0]) { _EnemiesCleared = _observer.NotifyGroupSpawn(); - if (_EnemiesCleared) + if (_EnemiesCleared) //Moves on to next group { _groupSpawnTimers.RemoveAt(0); - Debug.Log("Next..."); - } else + } else //Adds a delay to finish current group { - _groupSpawnTimers[0] += 0.05f; - Debug.Log("Timer: " + _groupSpawnTimers[0]); + _groupSpawnTimers[0] += GlobalConfig.Instance.Current.groupSpawnDelay; } } }