diff --git a/Assets/Scripts/src/Interfaces/IDynamicLevelData.cs b/Assets/Scripts/src/Interfaces/IDynamicLevelData.cs new file mode 100644 index 0000000..bbf89ae --- /dev/null +++ b/Assets/Scripts/src/Interfaces/IDynamicLevelData.cs @@ -0,0 +1,9 @@ +using src.Level.src.Level; + +namespace src.Interfaces +{ + public interface IDynamicLevelData + { + void SetLevelData(LevelData levelData); + } +} \ No newline at end of file diff --git a/Assets/Scripts/src/Interfaces/IDynamicLevelData.cs.meta b/Assets/Scripts/src/Interfaces/IDynamicLevelData.cs.meta new file mode 100644 index 0000000..a295905 --- /dev/null +++ b/Assets/Scripts/src/Interfaces/IDynamicLevelData.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 80c7acaaae4f4352a6564a3ede932f13 +timeCreated: 1564830284 \ No newline at end of file diff --git a/Assets/Scripts/src/Level/LevelManager.cs b/Assets/Scripts/src/Level/LevelManager.cs index d231fe9..dfbc436 100644 --- a/Assets/Scripts/src/Level/LevelManager.cs +++ b/Assets/Scripts/src/Level/LevelManager.cs @@ -1,6 +1,7 @@ using System.Collections.Generic; using src.Base; using src.Helpers; +using src.Interfaces; using src.Level.src.Level; using src.Managers; using src.Wall; @@ -9,7 +10,7 @@ using Random = UnityEngine.Random; namespace src.Level { - public class LevelManager : GameplayComponent + public class LevelManager : GameplayComponent, IDynamicLevelData { public Count DestructibleWallCount { @@ -31,10 +32,12 @@ namespace src.Level /* Used to group spawned objects */ public Transform boardHolder; + /* Holds the starting position of the player */ public Transform startPosition; + /* Holds references to prefabs for the specified level. */ - public GameObject indestructibleWallPrefab; + private GameObject _indestructibleWallPrefab; private GameObject[] _destructibleWallPrefabs; private GameObject[] _enemiesPrefab; @@ -46,13 +49,14 @@ namespace src.Level /* The size of the board. */ private const int Columns = 30; private const int Rows = 20; + private bool _isBoardInitialized; /* Holds the available positions */ private readonly List _freeGridPositionsBoard = new List(); private List _freeGridPositions; private List _destructibleWalls; private List _enemies; - + /* Singletons */ private GameStateManager _gameStateManager = GameStateManager.Instance; @@ -63,8 +67,9 @@ namespace src.Level _enemyCount = levelData.enemyCount; _enemiesPrefab = levelData.enemiesPrefab; _destructibleWallPrefabs = levelData.destructibleWallsPrefab; + _indestructibleWallPrefab = levelData.indestructibleWallsPrefab.ChoseRandom(); } - + /* Modifies walls from _destructibleWalls in order to setup upgrades*/ private void SetupSpawnables() { @@ -84,13 +89,13 @@ namespace src.Level DebugHelper.LogInfo($"Spawned upgrade at: x:{wall.XCoordinate} y:{wall.YCoordinate}"); wall.SpawnsUpgrade(); } - + if (count > wallsSize - 2) { Debug.LogWarning("No destructible walls found, cannot spawn exit!"); return; } - + var exitWall = _destructibleWalls[count + 1].GetComponent(); DebugHelper.LogInfo($"Spawned exit at: x:{exitWall.XCoordinate} y:{exitWall.YCoordinate}"); exitWall.SpawnsExit(); @@ -100,6 +105,12 @@ namespace src.Level * unused positions in a list. */ public void InitBoard() { + if (_isBoardInitialized) + { + /* We only want to initialize the board once since it doesn't change... for now*/ + return; + } + /* We want to iterate over the X axis taking into consideration the startPosition's offset */ for (var x = startPosition.position.x; x < Columns; x++) { @@ -134,6 +145,8 @@ namespace src.Level _freeGridPositionsBoard.Add(new Vector3(x, y, 0f)); } } + + _isBoardInitialized = true; } /* Randomly places destructible tiles on the level. */ @@ -148,10 +161,12 @@ namespace src.Level { break; } + usedPositions.Add(nextPosition); PlaceDestructibleTile(nextPosition); numberOfWallsRemaining -= 1; } + foreach (var usedPosition in usedPositions) { _freeGridPositions.Remove(usedPosition); @@ -179,7 +194,7 @@ namespace src.Level } var instance = - Instantiate(indestructibleWallPrefab, new Vector3(x, y, 0f), Quaternion.identity); + Instantiate(_indestructibleWallPrefab, new Vector3(x, y, 0f), Quaternion.identity); instance.transform.SetParent(boardHolder); return true; } @@ -234,6 +249,7 @@ namespace src.Level { Destroy(wall); } + DebugHelper.LogInfo("LevelManager: Cleared level!"); } } diff --git a/Assets/Scripts/src/Managers/GameManager.cs b/Assets/Scripts/src/Managers/GameManager.cs index fceee49..cd68580 100644 --- a/Assets/Scripts/src/Managers/GameManager.cs +++ b/Assets/Scripts/src/Managers/GameManager.cs @@ -14,7 +14,7 @@ namespace src.Managers private LevelManager _levelManager; private UpgradeManager _upgradeManager; private BombsUtilManager _bombsUtilManager; - + // External Components public GameObject preStageUiPrefab; private PlayerController _playerController; @@ -45,17 +45,17 @@ namespace src.Managers public void Start() { - _levelManager.InitBoard(); StartLevel(); } private void StartLevel() { var levelData = LevelResource.GetLevelData(_gameStateManager.Level); - StartCoroutine(PreInitGame()); _levelManager.SetLevelData(levelData); + _levelManager.InitBoard(); + + StartCoroutine(PreInitGame()); _upgradeManager.SetLevelData(levelData); - _levelManager.InitLevel(); _playerController.Respawn(); } @@ -69,7 +69,7 @@ namespace src.Managers { return _bombsUtilManager; } - + private IEnumerator PreInitGame() { var preStageUi = Instantiate(preStageUiPrefab); // Will destroy itself. diff --git a/Assets/Scripts/src/Managers/UpgradeManager.cs b/Assets/Scripts/src/Managers/UpgradeManager.cs index bd3ec2f..9469e1f 100644 --- a/Assets/Scripts/src/Managers/UpgradeManager.cs +++ b/Assets/Scripts/src/Managers/UpgradeManager.cs @@ -2,12 +2,13 @@ using System; using System.Collections.Generic; using src.Base; using src.Helpers; +using src.Interfaces; using src.Level.src.Level; using UnityEngine; namespace src.Managers { - public class UpgradeManager : GameplayComponent + public class UpgradeManager : GameplayComponent, IDynamicLevelData { public static UpgradeManager Instance; private List _unclaimedUpgrades = new List();