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