diff --git a/Assets/Design/Levels/WaveConfig01.asset b/Assets/Design/Levels/WaveConfig01.asset index 76aa0cf..db67372 100644 --- a/Assets/Design/Levels/WaveConfig01.asset +++ b/Assets/Design/Levels/WaveConfig01.asset @@ -18,10 +18,10 @@ MonoBehaviour: _nestedGroupSpawn: - groupSpawn: - _enemy: {fileID: 313037212318601125, guid: 5bbf0d85fa5bb3f4599da79f0a84e3a9, type: 3} - _count: 11 + _count: 0 - _enemy: {fileID: 313037212318601125, guid: 5bbf0d85fa5bb3f4599da79f0a84e3a9, type: 3} - _count: 4 - triggerTime: 0.5 + _count: 2 + triggerTime: 0.1 - groupSpawn: - _enemy: {fileID: 313037212318601125, guid: 5bbf0d85fa5bb3f4599da79f0a84e3a9, type: 3} _count: 1 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 9b8c8c0..5c1de7e 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,9 +17,9 @@ public class WaveObserver : Singleton public void SetLevelConfig(WaveConfig config) { - _levelConfig = config; + _levelConfig = Object.Instantiate(config); _copyConstantSpawn = new List(); - _copyGroupSpawn = new List(); + _copyGroupSpawn = new List>(); _groupSpawnTimers = new List(); if (!_levelConfig) { @@ -32,7 +32,11 @@ public class WaveObserver : Singleton } for (int index = 0; index < _levelConfig.NestedGroupSpawn.Count; index++) { - _copyGroupSpawn.Add(_levelConfig.NestedGroupSpawn[index]); + _copyGroupSpawn.Add(new List()); + for (int nestedIndex = 0; nestedIndex < _levelConfig.NestedGroupSpawn[index].groupSpawn.Count; nestedIndex++) + { + _copyGroupSpawn[index].Add(_levelConfig.NestedGroupSpawn[index].groupSpawn[nestedIndex].Count); + } _groupSpawnTimers.Add(_levelConfig.NestedGroupSpawn[index].triggerTime); } } @@ -163,16 +167,45 @@ public class WaveObserver : Singleton * Called when it is time to spawn a group * Assigns a random element of the group to a random spawner */ - public void NotifyGroupSpawn() + public bool NotifyGroupSpawn() { - System.Random rand = new System.Random(); - foreach (EnemyType groupEnemy in _copyGroupSpawn[_currentGroupIndex].groupSpawn) + List usedRows = new List(); + List currentGroup = _levelConfig.NestedGroupSpawn[_currentGroupIndex].groupSpawn; + for (int groupIndex = 0; groupIndex < currentGroup.Count; groupIndex++) //Loops through enemy groups { - for (int i = 0; i < groupEnemy.Count; i++) + if (_copyGroupSpawn[_currentGroupIndex][groupIndex] != 0) { - _subjects[rand.Next(_subjects.Count)].TriggerSpawn(groupEnemy.GetEnemyObject()); + 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; + } } } _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 b37aa4f..fdeac6c 100644 --- a/Assets/Scripts/Tiles/SpawnerTile.cs +++ b/Assets/Scripts/Tiles/SpawnerTile.cs @@ -16,11 +16,12 @@ public class SpawnerTile : LevelTile private WaveObserver _observer; private bool _stopped = false; private bool _cooldownEnded = false; + private bool _EnemiesCleared = true; private List _groupSpawnTimers = new List(); 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]); } @@ -47,10 +48,16 @@ public class SpawnerTile : LevelTile } if (_groupSpawnTimers.Count > 0) { - if (_lifetime / 60 > _groupSpawnTimers[0]) + if (_lifetime / 60.0f > _groupSpawnTimers[0]) { - _groupSpawnTimers.RemoveAt(0); - _observer.NotifyGroupSpawn(); + _EnemiesCleared = _observer.NotifyGroupSpawn(); + if (_EnemiesCleared) //Moves on to next group + { + _groupSpawnTimers.RemoveAt(0); + } else //Adds a delay to finish current group + { + _groupSpawnTimers[0] += GlobalConfig.Instance.Current.groupSpawnDelay; + } } } if (_spawnCounter < _spawnSpeed) return;