Permalink
@@ -0,0 +1,152 @@ | |||
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() | |||
{ | |||
//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); | |||
|
|||
//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); | |||
} | |||
|
|||
} | |||
//} |
@@ -0,0 +1,56 @@ | |||
using UnityEngine; | |||
using System.Collections; | |||
|
|||
namespace Completed | |||
{ | |||
using System.Collections.Generic; //Allows us to use Lists. | |||
|
|||
public class GameManager : MonoBehaviour | |||
{ | |||
|
|||
public static GameManager instance = null; //Static instance of GameManager which allows it to be accessed by any other script. | |||
private BoardManager boardScript; //Store a reference to our BoardManager which will set up the level. | |||
private int level = 3; //Current level number, expressed in game as "Day 1". | |||
|
|||
//Awake is always called before any Start functions | |||
void Awake() | |||
{ | |||
//Check if instance already exists | |||
if (instance == null) | |||
|
|||
//if not, set instance to this | |||
instance = this; | |||
|
|||
//If instance already exists and it's not this: | |||
else if (instance != this) | |||
|
|||
//Then destroy this. This enforces our singleton pattern, meaning there can only ever be one instance of a GameManager. | |||
Destroy(gameObject); | |||
|
|||
//Sets this to not be destroyed when reloading scene | |||
DontDestroyOnLoad(gameObject); | |||
|
|||
//Get a component reference to the attached BoardManager script | |||
boardScript = GetComponent<BoardManager>(); | |||
|
|||
//Call the InitGame function to initialize the first level | |||
InitGame(); | |||
} | |||
|
|||
//Initializes the game for each level. | |||
void InitGame() | |||
{ | |||
//Call the SetupScene function of the BoardManager script, pass it current level number. | |||
boardScript.SetupScene(level); | |||
|
|||
} | |||
|
|||
|
|||
|
|||
//Update is called every frame. | |||
void Update() | |||
{ | |||
|
|||
} | |||
} | |||
} |
@@ -0,0 +1,136 @@ | |||
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> | |||
<html xmlns="http://www.w3.org/1999/xhtml"> | |||
<head> | |||
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"> | |||
<title>Unity Web Player | Roguelike2d</title> | |||
<script type='text/javascript' src='https://ssl-webplayer.unity3d.com/download_webplayer-3.x/3.0/uo/jquery.min.js'></script> | |||
<script type="text/javascript"> | |||
<!-- | |||
var unityObjectUrl = "http://webplayer.unity3d.com/download_webplayer-3.x/3.0/uo/UnityObject2.js"; | |||
if (document.location.protocol == 'https:') | |||
unityObjectUrl = unityObjectUrl.replace("http://", "https://ssl-"); | |||
document.write('<script type="text\/javascript" src="' + unityObjectUrl + '"><\/script>'); | |||
--> | |||
</script> | |||
<script type="text/javascript"> | |||
<!-- | |||
var config = { | |||
width: 960, | |||
height: 600, | |||
params: { enableDebugging:"0" } | |||
|
|||
}; | |||
var u = new UnityObject2(config); | |||
|
|||
jQuery(function() { | |||
|
|||
var $missingScreen = jQuery("#unityPlayer").find(".missing"); | |||
var $brokenScreen = jQuery("#unityPlayer").find(".broken"); | |||
$missingScreen.hide(); | |||
$brokenScreen.hide(); | |||
|
|||
u.observeProgress(function (progress) { | |||
switch(progress.pluginStatus) { | |||
case "broken": | |||
$brokenScreen.find("a").click(function (e) { | |||
e.stopPropagation(); | |||
e.preventDefault(); | |||
u.installPlugin(); | |||
return false; | |||
}); | |||
$brokenScreen.show(); | |||
break; | |||
case "missing": | |||
$missingScreen.find("a").click(function (e) { | |||
e.stopPropagation(); | |||
e.preventDefault(); | |||
u.installPlugin(); | |||
return false; | |||
}); | |||
$missingScreen.show(); | |||
break; | |||
case "installed": | |||
$missingScreen.remove(); | |||
break; | |||
case "first": | |||
break; | |||
} | |||
}); | |||
u.initPlugin(jQuery("#unityPlayer")[0], "Roguelike2d.unity3d"); | |||
}); | |||
--> | |||
</script> | |||
<style type="text/css"> | |||
<!-- | |||
body { | |||
font-family: Helvetica, Verdana, Arial, sans-serif; | |||
background-color: white; | |||
color: black; | |||
text-align: center; | |||
} | |||
a:link, a:visited { | |||
color: #000; | |||
} | |||
a:active, a:hover { | |||
color: #666; | |||
} | |||
p.header { | |||
font-size: small; | |||
} | |||
p.header span { | |||
font-weight: bold; | |||
} | |||
p.footer { | |||
font-size: x-small; | |||
} | |||
div.content { | |||
margin: auto; | |||
width: 960px; | |||
} | |||
div.broken, | |||
div.missing { | |||
margin: auto; | |||
position: relative; | |||
top: 50%; | |||
width: 193px; | |||
} | |||
div.broken a, | |||
div.missing a { | |||
height: 63px; | |||
position: relative; | |||
top: -31px; | |||
} | |||
div.broken img, | |||
div.missing img { | |||
border-width: 0px; | |||
} | |||
div.broken { | |||
display: none; | |||
} | |||
div#unityPlayer { | |||
cursor: default; | |||
height: 600px; | |||
width: 960px; | |||
} | |||
--> | |||
</style> | |||
</head> | |||
<body> | |||
<p class="header"><span>Unity Web Player | </span>Roguelike2d</p> | |||
<div class="content"> | |||
<div id="unityPlayer"> | |||
<div class="missing"> | |||
<a href="http://unity3d.com/webplayer/" title="Unity Web Player. Install now!"> | |||
<img alt="Unity Web Player. Install now!" src="http://webplayer.unity3d.com/installation/getunity.png" width="193" height="63" /> | |||
</a> | |||
</div> | |||
<div class="broken"> | |||
<a href="http://unity3d.com/webplayer/" title="Unity Web Player. Install now! Restart your browser after install."> | |||
<img alt="Unity Web Player. Install now! Restart your browser after install." src="http://webplayer.unity3d.com/installation/getunityrestart.png" width="193" height="63" /> | |||
</a> | |||
</div> | |||
</div> | |||
</div> | |||
<p class="footer">« created with <a href="http://unity3d.com/unity/" title="Go to unity3d.com">Unity</a> »</p> | |||
</body> | |||
</html> |
Binary file not shown.

Oops, something went wrong.