Advertisement
Guest User

Code

a guest
Sep 28th, 2016
375
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.93 KB | None | 0 0
  1. boardmanager:
  2.  
  3. using UnityEngine;
  4. using System;
  5. using System.Collections.Generic;       //Allows us to use Lists.
  6. using Random = UnityEngine.Random;      //Tells Random to use the Unity Engine random number generator.
  7.  
  8.    
  9.     public class BoardManager : MonoBehaviour
  10.     {
  11.         // Using Serializable allows us to embed a class with sub properties in the inspector.
  12.         [Serializable]
  13.         public class Count
  14.         {
  15.             public int minimum;             //Minimum value for our Count class.
  16.             public int maximum;             //Maximum value for our Count class.
  17.            
  18.            
  19.             //Assignment constructor.
  20.             public Count (int min, int max)
  21.             {
  22.                 minimum = min;
  23.                 maximum = max;
  24.             }
  25.         }
  26.        
  27.        
  28.         public int columns = 8;                                         //Number of columns in our game board.
  29.         public int rows = 8;                                            //Number of rows in our game board.
  30.         public Count wallCount = new Count (5, 9);                      //Lower and upper limit for our random number of walls per level.
  31.         public Count foodCount = new Count (1, 5);                      //Lower and upper limit for our random number of food items per level.
  32.         public GameObject exit;                                         //Prefab to spawn for exit.
  33.         public GameObject[] floorTiles;                                 //Array of floor prefabs.
  34.         public GameObject[] wallTiles;                                  //Array of wall prefabs.
  35.         public GameObject[] foodTiles;                                  //Array of food prefabs.
  36.         public GameObject[] enemyTiles;                                 //Array of enemy prefabs.
  37.         public GameObject[] outerWallTiles;                             //Array of outer tile prefabs.
  38.        
  39.         private Transform boardHolder;                                  //A variable to store a reference to the transform of our Board object.
  40.         private List <Vector3> gridPositions = new List <Vector3> ();   //A list of possible locations to place tiles.
  41.        
  42.        
  43.         //Clears our list gridPositions and prepares it to generate a new board.
  44.         void InitialiseList ()
  45.         {
  46.             //Clear our list gridPositions.
  47.             gridPositions.Clear ();
  48.            
  49.             //Loop through x axis (columns).
  50.             for(int x = 1; x < columns-1; x++)
  51.             {
  52.                 //Within each column, loop through y axis (rows).
  53.                 for(int y = 1; y < rows-1; y++)
  54.                 {
  55.                     //At each index add a new Vector3 to our list with the x and y coordinates of that position.
  56.                     gridPositions.Add (new Vector3(x, y, 0f));
  57.                 }
  58.             }
  59.         }
  60.        
  61.        
  62.         //Sets up the outer walls and floor (background) of the game board.
  63.         void BoardSetup ()
  64.         {
  65.             //Instantiate Board and set boardHolder to its transform.
  66.             boardHolder = new GameObject ("Board").transform;
  67.            
  68.             //Loop along x axis, starting from -1 (to fill corner) with floor or outerwall edge tiles.
  69.             for(int x = -1; x < columns + 1; x++)
  70.             {
  71.                 //Loop along y axis, starting from -1 to place floor or outerwall tiles.
  72.                 for(int y = -1; y < rows + 1; y++)
  73.                 {
  74.                     //Choose a random tile from our array of floor tile prefabs and prepare to instantiate it.
  75.                     GameObject toInstantiate = floorTiles[Random.Range (0,floorTiles.Length)];
  76.                    
  77.                     //Check if we current position is at board edge, if so choose a random outer wall prefab from our array of outer wall tiles.
  78.                     if(x == -1 || x == columns || y == -1 || y == rows)
  79.                         toInstantiate = outerWallTiles [Random.Range (0, outerWallTiles.Length)];
  80.                    
  81.                     //Instantiate the GameObject instance using the prefab chosen for toInstantiate at the Vector3 corresponding to current grid position in loop, cast it to GameObject.
  82.                     GameObject instance =
  83.                         Instantiate (toInstantiate, new Vector3 (x, y, 0f), Quaternion.identity) as GameObject;
  84.                    
  85.                     //Set the parent of our newly instantiated object instance to boardHolder, this is just organizational to avoid cluttering hierarchy.
  86.                     instance.transform.SetParent (boardHolder);
  87.                 }
  88.             }
  89.         }
  90.        
  91.        
  92.         //RandomPosition returns a random position from our list gridPositions.
  93.         Vector3 RandomPosition ()
  94.         {
  95.             //Declare an integer randomIndex, set it's value to a random number between 0 and the count of items in our List gridPositions.
  96.             int randomIndex = Random.Range (0, gridPositions.Count);
  97.            
  98.             //Declare a variable of type Vector3 called randomPosition, set it's value to the entry at randomIndex from our List gridPositions.
  99.             Vector3 randomPosition = gridPositions[randomIndex];
  100.            
  101.             //Remove the entry at randomIndex from the list so that it can't be re-used.
  102.             gridPositions.RemoveAt (randomIndex);
  103.            
  104.             //Return the randomly selected Vector3 position.
  105.             return randomPosition;
  106.         }
  107.        
  108.        
  109.         //LayoutObjectAtRandom accepts an array of game objects to choose from along with a minimum and maximum range for the number of objects to create.
  110.         void LayoutObjectAtRandom (GameObject[] tileArray, int minimum, int maximum)
  111.         {
  112.             //Choose a random number of objects to instantiate within the minimum and maximum limits
  113.             int objectCount = Random.Range (minimum, maximum+1);
  114.            
  115.             //Instantiate objects until the randomly chosen limit objectCount is reached
  116.             for(int i = 0; i < objectCount; i++)
  117.             {
  118.                 //Choose a position for randomPosition by getting a random position from our list of available Vector3s stored in gridPosition
  119.                 Vector3 randomPosition = RandomPosition();
  120.                
  121.                 //Choose a random tile from tileArray and assign it to tileChoice
  122.                 GameObject tileChoice = tileArray[Random.Range (0, tileArray.Length)];
  123.                
  124.                 //Instantiate tileChoice at the position returned by RandomPosition with no change in rotation
  125.                 Instantiate(tileChoice, randomPosition, Quaternion.identity);
  126.             }
  127.         }
  128.        
  129.        
  130.         //SetupScene initializes our level and calls the previous functions to lay out the game board
  131.         public void SetupScene (int level)
  132.         {
  133.             //Creates the outer walls and floor.
  134.             BoardSetup ();
  135.            
  136.             //Reset our list of gridpositions.
  137.             InitialiseList ();
  138.            
  139.             //Instantiate a random number of wall tiles based on minimum and maximum, at randomized positions.
  140.             LayoutObjectAtRandom (wallTiles, wallCount.minimum, wallCount.maximum);
  141.            
  142.             //Instantiate a random number of food tiles based on minimum and maximum, at randomized positions.
  143.             LayoutObjectAtRandom (foodTiles, foodCount.minimum, foodCount.maximum);
  144.            
  145.             //Determine number of enemies based on current level number, based on a logarithmic progression
  146.             int enemyCount = (int)Mathf.Log(level, 2f);
  147.            
  148.             //Instantiate a random number of enemies based on minimum and maximum, at randomized positions.
  149.             LayoutObjectAtRandom (enemyTiles, enemyCount, enemyCount);
  150.            
  151.             //Instantiate the exit tile in the upper right hand corner of our game board
  152.             Instantiate (exit, new Vector3 (columns - 1, rows - 1, 0f), Quaternion.identity);
  153.         }
  154.     }
  155.  
  156. Game Manager:
  157.  
  158. using UnityEngine;
  159. using System.Collections;
  160. using System.Collections.Generic;
  161.  
  162. public class GameManager : MonoBehaviour {
  163.  
  164.     public BoardManager boardScript;
  165.  
  166.     private int level = 3;
  167.  
  168.     void Awake() {
  169.         boardScript = GetComponent<BoardManager>();
  170.         InitGame();
  171.     }
  172.  
  173.     void InitGame() {
  174.         boardScript.SetupScene(level);
  175.     }
  176.  
  177.     // Use this for initialization
  178.     void Start () {
  179.    
  180.     }
  181.    
  182.     // Update is called once per frame
  183.     void Update () {
  184.    
  185.     }
  186. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement