Skip to content
Permalink
7c2f95ca20
Switch branches/tags

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?
Go to file
 
 
Cannot retrieve contributors at this time
112 lines (91 sloc) 3.38 KB
using UnityEngine;
using System;
using System.Collections.Generic;
using Random = UnityEngine.Random;
//++Add in Comments++
namespace Completed
{
public class BoardManager : MonoBehaviour {
[Serializable]
public class Count
{
public int minimum;
public int maximum;
public Count(int min, int max)
{
minimum = min;
maximum = max;
}
}
//--------------------------------------------------
public int columns = 8;
public int rows = 8;
public Count wallCount = new Count(5, 9);
public Count foodCount = new Count(1, 5);
public GameObject exit;
public GameObject[] floorTiles;
public GameObject[] wallTiles;
public GameObject[] foodTiles;
public GameObject[] enemyTiles;
public GameObject[] outerWallTiles;
private Transform boardHolder;
private List<Vector3> gridPositions = new List<Vector3>();
void InitialiseList()
{
gridPositions.Clear();
for(int x=1; x< columns - 1; x++)
{
//Do this thing while x>columns - 1
for (int y = 1; y < rows -1; y++)
{
gridPositions.Add(new Vector3(x, y, 0f));
}
}
}
void BoardSetup()
{
boardHolder = new GameObject("Board").transform;
for(int x = -1; x<columns + 1; x++)
{
for(int y = -1; y < rows + 1; y++)
{
GameObject toInstanciate = floorTiles[Random.Range(0, floorTiles.Length)];
//Detect Edges
if (x == -1 ||x == columns|| y == -1 || y == rows)
{
toInstanciate = outerWallTiles[Random.Range(0, outerWallTiles.Length)];
}
GameObject instance = Instantiate(toInstanciate, new Vector3(x, y, 0f), Quaternion.identity) as GameObject;
instance.transform.SetParent(boardHolder);
}
}
}
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);
}
}
}