Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Intro_Scritping/Roguelike/Assets/Scripts/ProdGen/BoardManager.cs
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
131 lines (108 sloc)
5.55 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System; | |
using System.Collections.Generic; //Allows us to use lists | |
using Random = UnityEngine.Random; // Tells Random to use Untiy Engine Random number generator | |
//++Add in Comments++ | |
namespace Completed | |
{ | |
public class BoardManager : MonoBehaviour { | |
//Using serializable allows us to embed a class wit hsub properties in sinpsector | |
[Serializable] | |
public class Count | |
{ | |
public int minimum; //Minimum value for our count class | |
public int maximum; //Maximum value for our count class | |
//assignment constructor | |
public Count(int min, int max) | |
{ | |
minimum = min; | |
maximum = max; | |
} | |
} | |
//Declarations-------------------------------------------------- | |
public int columns = 8; //Number of columns in our game baord | |
public int rows = 8; //Number of Rows in our game board | |
public Count wallCount = new Count(5, 9); //Lower and upper limit for our random number | |
public Count foodCount = new Count(1, 5); //Lowra nd uper limit for our random food count | |
public GameObject exit; //Prefab to spawn for exit | |
//Arrays | |
public GameObject[] floorTiles; //Array of floor prefabs | |
public GameObject[] wallTiles; //Array of wall prefabs | |
public GameObject[] foodTiles; //Array of food prefabs | |
public GameObject[] enemyTiles; //Array of enemy prefabs | |
public GameObject[] outerWallTiles; //Array of ourter tile prefabs | |
private Transform boardHolder; //A variable to store references to the transform of something | |
private List<Vector3> gridPositions = new List<Vector3>(); //A list of possible locations to place tiles | |
//Clears our list gridPositions and prepares it to generate a new board. | |
void InitialiseList() | |
{ | |
//clear our list gridPositions | |
gridPositions.Clear(); | |
//Loot through x axis (columns) | |
for (int x=1; x< columns - 1; x++) | |
{ | |
//within each column, loop through the y axis (rows) | |
for (int y = 1; y < rows -1; y++) | |
{ | |
//At each index add a new vector3 to our lists with the x and y coordinates of that position | |
gridPositions.Add(new Vector3(x, y, 0f)); | |
} | |
} | |
} | |
//Sets up the outer walls and flolors of the game | |
void BoardSetup() | |
{ | |
//instanciates baords and sets iup boardholder to its transform | |
boardHolder = new GameObject("Board").transform; | |
//loop along the x axis, starting from - 1 (to fill corner) with floor and outerwall edge tiles | |
for(int x = -1; x<columns + 1; x++) | |
{ | |
//Loop along y axis, starting fomr -1 to place floor or outerwall tiles | |
for(int y = -1; y < rows + 1; y++) | |
{ | |
//choose a random tile from our array for the floor tile prefabs and prepare to instanciete | |
GameObject toInstanciate = floorTiles[Random.Range(0, floorTiles.Length)]; | |
//check if we current position is at board edge, if so choose a random outerwall prefab | |
if (x == -1 ||x == columns|| y == -1 || y == rows) | |
{ | |
toInstanciate = outerWallTiles[Random.Range(0, outerWallTiles.Length)]; | |
} | |
//Instanciate Gameobject instance using prefabt chosen for toInstanciate | |
GameObject instance = Instantiate(toInstanciate, new Vector3(x, y, 0f), Quaternion.identity) as GameObject; | |
//Set the paretn of our newly isntanciated object isnatne to boardHolder | |
instance.transform.SetParent(boardHolder); | |
} | |
} | |
} | |
//Making random positions to spawn board pieces | |
//RanndomPosition returns a random position from our gridpositions | |
Vector3 RandomPosition() | |
{ | |
int randomIndex = Random.Range(0, gridPositions.Count); | |
Vector3 randomPosition = gridPositions[randomIndex]; | |
gridPositions.RemoveAt(randomIndex); | |
return randomPosition; | |
} | |
// | |
void LayoutObjectAtRandom(GameObject[] tileArray, int minimum, int maximum) | |
{ | |
int objectCount = Random.Range(minimum, maximum + 1); | |
for(int i = 0; i< objectCount; i++) | |
{ | |
Vector3 randomPosition = RandomPosition(); | |
GameObject tileChoice = tileArray[Random.Range(0, tileArray.Length)]; | |
Instantiate(tileChoice, randomPosition, Quaternion.identity); | |
} | |
} | |
public void SetupScene(int level) | |
{ | |
BoardSetup(); | |
InitialiseList(); | |
LayoutObjectAtRandom(wallTiles, wallCount.minimum, wallCount.maximum); | |
LayoutObjectAtRandom(foodTiles, foodCount.minimum, foodCount.maximum); | |
int enemyCount = (int)Mathf.Log(level, 2f); | |
LayoutObjectAtRandom(enemyTiles, enemyCount, enemyCount); | |
Instantiate(exit, new Vector3(columns - 1, rows - 1, 0f), Quaternion.identity); | |
} | |
} | |
} |