public List <Vector2> PrepareBoard(LevelConfiguration configuration) { var clearTileVectors = new List <Vector2>(); var length = configuration.PlayableBoardWidth; var height = configuration.PlayableBoardHeight; var board = new GameObject("Board"); _board = new GameObject[length + 2, height + 2]; for (var y = 0; y <= height + 1; y++) { for (var x = 0; x <= length + 1; x++) { var isEdge = x == 0 || y == 0 || x == length + 1 || y == height + 1; var isColumn = x % 2 == 0 && y % 2 == 0; var prefab = Block; if (!(isEdge || isColumn)) { prefab = Grass; clearTileVectors.Add(new Vector2(x, y)); } var tile = Instantiate(prefab, new Vector3(x, y, 100), Quaternion.identity, board.transform); _board[x, y] = tile; } } return(clearTileVectors); }
private void SetCameraToCentreOfBoard(LevelConfiguration levelConfiguration) { var totalWidth = levelConfiguration.PlayableBoardWidth + 2; var totalHeight = levelConfiguration.PlayableBoardWidth + 2; Camera.transform.position = new Vector3(totalWidth / 2f, totalHeight / 2f); }
private Stack <GameObject> AddAllPowerUpsToStack(LevelConfiguration configuration) { var powerups = new Stack <GameObject>(); powerups.Repeat(SpeedPowerUp, configuration.SpeedPowerUpCount); powerups.Repeat(BombRangePowerUp, configuration.BombRangePowerUpCount); powerups.Repeat(BombsHeldPowerUp, configuration.BombsHeldPowerUpCount); return(powerups); }
void Start() { var levelConfiguration = new LevelConfiguration { PlayableBoardHeight = 5, PlayableBoardWidth = 5, BombRangePowerUpCount = 2, BombsHeldPowerUpCount = 2, SpeedPowerUpCount = 2, CrateCount = 5, EnemyCount = 2 }; var clearTileVectors = PrepareBoard(levelConfiguration); RemovePathsInThePlayerStartZone(clearTileVectors); AddCratesToBoard(levelConfiguration, clearTileVectors); DistributeEnemies(levelConfiguration, clearTileVectors); SetCameraToCentreOfBoard(levelConfiguration); }
LevelConfiguration ILevelGenerator.GenerateLevel(int minItemCount, int maxItemCount, float worldRadius, float minRotationSpeed, float maxRotationSpeed, float minScale, float maxScale, float sphereRadius) { int objsAmount = Random.Range(minItemCount, maxItemCount); var levelObjects = new Dictionary <int, Item>(); GenerateGameObjectsConfig(worldRadius, minRotationSpeed, maxRotationSpeed, minScale, maxScale, objsAmount, levelObjects, sphereRadius); int solultionObjId = Random.Range(1, objsAmount + 1); var solutionPosition = generateRandVec3(worldRadius).normalized *sphereRadius; LevelConfiguration levelConfig = new LevelConfiguration(); levelConfig.items = levelObjects.Values.ToArray(); levelConfig.solutionItemId = solultionObjId; levelConfig.solutionStartPositionOnItem = solutionPosition; return(levelConfig); }
private void AddCratesToBoard(LevelConfiguration configuration, IList <Vector2> clearTileVectors) { var crateCount = configuration.CrateCount; var cratesParent = new GameObject("Crates"); var powerUps = AddAllPowerUpsToStack(configuration); var clearTileStack = new Stack <Vector2>(clearTileVectors.KnuthShuffle()); while (crateCount > 0 && clearTileStack.Count > 0) { var path = clearTileStack.Pop(); var crate = Instantiate(Crate, new Vector3(path.x, path.y, 99), Quaternion.identity, cratesParent.transform); if (powerUps.Count > 0) { crate.GetComponent <Tile>().PowerUp = powerUps.Pop(); } _board[(int)path.x, (int)path.y] = crate; crateCount--; } }
private void DistributeEnemies(LevelConfiguration configuration, ICollection <Vector2> clearTileStack) { if (clearTileStack.Count < configuration.EnemyCount) { throw new InvalidOperationException("There is not enough space to distribute the enemies. Good maths!"); } var enemyManager = FindObjectOfType <EnemyManager>(); var enemyCount = configuration.EnemyCount; foreach (var vector2 in clearTileStack) { if (enemyCount == 0) { break; } Instantiate(Troll, new Vector3(vector2.x, vector2.y, 10), Quaternion.identity); enemyCount--; enemyManager.EnemyCount++; } }
public void SetLevelConfiguration(LevelConfiguration value) { levelConfiguration = value; }