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/Roguelike2d/Assets/Script/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.
156 lines (128 sloc)
7.36 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 | |
public GameObject[] batteryTiles; //Array of the Batteries for unlocking | |
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() | |
{ | |
//Declare an integer randomInxdex, set its value to a ranom umber btween 0 and the count of items | |
int randomIndex = Random.Range(0, gridPositions.Count); | |
//Declare a variable of type Vector3 called Randomp position, set its value to the etnry ant RandomIndex | |
Vector3 randomPosition = gridPositions[randomIndex]; | |
//Remove the etnry at randomIndex from the list so that it can't be re-used | |
gridPositions.RemoveAt(randomIndex); | |
//return the randomly selected Vector3 position | |
return randomPosition; | |
} | |
//LaoyutObjectAtRandom acepts an array of game obejcts to choose from along with a minimu and maximum | |
void LayoutObjectAtRandom(GameObject[] tileArray, int minimum, int maximum) | |
{ | |
//Choose a random number of objects to instanciate within the minimum and maximum limits | |
int objectCount = Random.Range(minimum, maximum + 1); | |
//Instanciate objects until the randomly chosen limit objectcount is reached | |
for(int i = 0; i< objectCount; i++) | |
{ | |
//Choose a random number of objects to isnanciate wtihin the minimum and maximum limits | |
Vector3 randomPosition = RandomPosition(); | |
//Choose a random tile from tile Array and assign it to tileChoice | |
GameObject tileChoice = tileArray[Random.Range(0, tileArray.Length)]; | |
//Insanciate tileCHoice at the position returned by RandomPositon with no change in rotation | |
Instantiate(tileChoice, randomPosition, Quaternion.identity); | |
} | |
} | |
//SetupScene initializeds our level and calls the previous functions to lay out the game board | |
public void SetupScene(int level) | |
{ | |
//creates the outer walls and floor | |
BoardSetup(); | |
//resets our list of gridposionts | |
InitialiseList(); | |
//Instanciate a random number of walll tiles based on min/max, at random positions | |
LayoutObjectAtRandom(wallTiles, wallCount.minimum, wallCount.maximum); | |
//Instanciate a random number of food tiles based on min/max, at random positions | |
LayoutObjectAtRandom(foodTiles, foodCount.minimum, foodCount.maximum); | |
//instanciate a fixed number of batteries | |
LayoutObjectAtRandom(batteryTiles, 4, 4); | |
//Determine number of enemies based on current level number, based on logarithim progression | |
int enemyCount = (int)Mathf.Log(level, 2f); | |
//Instanciate arndom number of enmies based on min/max, at random positions | |
LayoutObjectAtRandom(enemyTiles, enemyCount, enemyCount); | |
//Instanciate the xtilre til in the upper right hand corner of our game board | |
// Instantiate(exit, new Vector3(columns - 1, rows - 1, 0f), Quaternion.identity); | |
} | |
} | |
//} |