//Creates all enemy objects public void CreateEnemyObjects() { int objCount = 0; int rand; for (int y = 0; y < boardInfo.height; y++) { if (objCount < boardInfo.nEnemies) { for (int x = 0; x < boardInfo.width; x++) { if (objCount < boardInfo.nEnemies) { if (nodes[x, y] != null) { placementChance = Functions.GetPlacementChance(x, y, boardInfo.width, boardInfo.height, boardInfo.nEnemies); rand = RNG.Next(100); int winObjCount = 0; foreach (WinObject winObj in winObjects) { //if the node is taken by a winObject, jump to the next node if (nodes[x, y].position == winObj.position) { break; } winObjCount++; } if (rand > placementChance) { if (winObjCount == winObjects.Count && nodes[x, y].isEmpty && (x != 0 || y != 0) && !(x == 1 || y == 0) && !(x == 1 || y == 1)) { //create and place the spikes Spike spike = new Spike(game); spike.position = nodes[x, y].position; bool error = false; //confirms if the enemy is separated from the other enemies by at least X units foreach (EnemyObject enemy in enemyObjects) { if (Math.Abs(enemy.position.X - spike.position.X) < 2 && Math.Abs(enemy.position.Y - spike.position.Y) < 2) { error = true; break; } } //Place the spike if (!error) { rand = RNG.Next(100); if (rand > 30) { enemyObjects.Add(spike); } else { Vector2 dir = new Vector2(0, 1); if (rand <= 15 && x != boardInfo.width - 1) { dir = new Vector2(0, -1); } MovableEnemy movingEnemy = new MovableEnemy(nodes[x, y].position, 3, "Copycat", dir); enemyObjects.Add(movingEnemy); } objCount++; } } } } } else { break; } } } else { break; } } }
Board LoadLevel(out Player player) { string[] file = File.ReadAllLines(Content.RootDirectory + "/level.txt"); width = file[0].Length; height = file.Length; List <Obstacle> obstacles = new List <Obstacle>(); List <WinObject> winObjects = new List <WinObject>(); List <EnemyObject> enemyObjects = new List <EnemyObject>(); Vector2 playerPos = new Vector2(); int holesCount = 0, boxCount = 0, enemyCount = 0, collectibleCount = 0; for (int i = 0; i < height; i++) { for (int j = 0; j < width; j++) { // Hole if (file[i][j] == '#') { holesCount++; } //Box else if (file[i][j] == 'B') { boxCount++; } //Enemy else if (file[i][j] == 'E') { enemyCount++; } //Collectible else if (file[i][j] == 'C') { collectibleCount++; } } } Board tempBoard = new HexaBoard(width, height, holesCount, boxCount, enemyCount, nPortals, nLasers, nCollectibles, nDirections, this); Vector2[] holesPosition = new Vector2[holesCount]; holesCount = 0; for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { // Player if (file[y][x] == 'P') { playerPos = new Vector2(x, y); } //Box else if (file[y][x] == 'B') { Box box = new Box(tempBoard, this) { position = new Vector2(x, y) }; obstacles.Add(box); } //Toggle else if (file[y][x] == 'T') { Toggle toggle = new Toggle(this); toggle.position = new Vector2(x, y); winObjects.Add(toggle); } //Collectible else if (file[y][x] == 'C') { Collectible collectible = new Collectible(this); collectible.position = new Vector2(x, y); winObjects.Add(collectible); } //Hole else if (file[y][x] == '#') { holesPosition[holesCount] = new Vector2(x, y); holesCount++; } //Enemy else if (file[y][x] == 'E') { Spike spike = new Spike(this); spike.position = new Vector2(x, y); enemyObjects.Add(spike); } } } Board board = new HexaBoard(width, height, holesPosition, obstacles, enemyObjects, winObjects, nDirections, this); if (nDirections == 4) { board = new QuadBoard(width, height, holesPosition, obstacles, enemyObjects, winObjects, nDirections, this); } player = new Player(board, playerPos); currentGameState = new GameState(board, player); return(board); }