Implement DynamicLevelData interface and refactor LevelManager

This commit is contained in:
Denis-Cosmin Nutiu 2019-08-03 14:23:49 +03:00
parent 6160c52428
commit 0251b20cb2
5 changed files with 42 additions and 13 deletions

View file

@ -0,0 +1,9 @@
using src.Level.src.Level;
namespace src.Interfaces
{
public interface IDynamicLevelData
{
void SetLevelData(LevelData levelData);
}
}

View file

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 80c7acaaae4f4352a6564a3ede932f13
timeCreated: 1564830284

View file

@ -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,6 +49,7 @@ 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>();
@ -63,6 +67,7 @@ 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*/
@ -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!");
} }
} }

View file

@ -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);
_upgradeManager.SetLevelData(levelData); _levelManager.InitBoard();
StartCoroutine(PreInitGame());
_upgradeManager.SetLevelData(levelData);
_levelManager.InitLevel(); _levelManager.InitLevel();
_playerController.Respawn(); _playerController.Respawn();
} }

View file

@ -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>();