Advertisement
gundambison

BoardManager

Feb 22nd, 2020
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.93 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5.  
  6. using Random = UnityEngine.Random;
  7.  
  8. public class BoardManager : MonoBehaviour
  9. {
  10.     // Using Serializable allows us to embed a class with sub properties in the inspector.
  11.     [Serializable]
  12.     public class Count
  13.     {
  14.         public int minimum;
  15.         public int maximum;
  16.        
  17.         public Count (int min, int max){
  18.             minimum = min;
  19.             maximum = max;
  20.            
  21.         }
  22.     }
  23.      
  24.         public int columns = 8;                                         //Number of columns in our game board.
  25.         public int rows = 8;                                            //Number of rows in our game board.
  26.         private int columnsBefore = 0;
  27.         private int rowsBefore = 0;
  28.  
  29.         public int minLevelExpand = 4;
  30.  
  31.         public Count wallCount = new Count (5, 9);                      //Lower and upper limit for our random number of walls per level.
  32.         public Count foodCount = new Count (1, 5);                      //Lower and upper limit for our random number of food items per level.
  33.         public GameObject exit;                                         //Prefab to spawn for exit.
  34.         public GameObject[] floorTiles;                                 //Array of floor prefabs.
  35.         public GameObject[] wallTiles;                                  //Array of wall prefabs.
  36.         public GameObject[] foodTiles;                                  //Array of food prefabs.
  37.         public GameObject[] enemyTiles;                                 //Array of enemy prefabs.
  38.         public GameObject[] outerWallTiles;                             //Array of outer tile prefabs.
  39.        
  40.         private Transform boardHolder;                                  //A variable to store a reference to the transform of our Board object.
  41.         private List <Vector3> gridPositions = new List <Vector3> ();   //A list of possible locations to place tiles.
  42.      
  43.     /*
  44.     public int columns = 8;
  45.     public int rows    = 8;
  46.    
  47.     public Count wallCount = new Count (5,9);
  48.     public Count foodCount = new Count (1,5);
  49.    
  50.     public GameObject exit;
  51.    
  52.     public GameObject[] floorTiles;
  53.     public GameObject[] wallTiles;
  54.     public GameObject[] foodTiles;
  55.     public GameObject[] enemyTiles;
  56.     public GameObject[] outerWallTiles;
  57.  
  58.     private Transform boardHolder;
  59.     private List <Vector3> gridPositions = new List <Vector3> ();
  60.     */
  61.     void InitiaLiseList()
  62.     {
  63.         gridPositions.Clear();
  64.         for(int x = 1; x < columns - 1; x++){
  65.             for(int y = 1; y < rows -1; y++){
  66.                 gridPositions.Add(new Vector3(x,y,0f));
  67.                
  68.             }
  69.         }
  70.     }
  71.  
  72.     void BoardSetup (int level)
  73.     {
  74.         boardHolder = new GameObject ("Board").transform;
  75.          
  76.         if(level > minLevelExpand)
  77.         {
  78.             int levelExpand = level - minLevelExpand;
  79.             int expand = (int)Math.Log(levelExpand, 2f);
  80.             int expand2 = expand + levelExpand;
  81.             if (level % 3 == 1)
  82.             {
  83.                 columns += expand;
  84.                 rows += expand;
  85.  
  86.             }
  87.             Debug.Log("BM: c,r (" + columns +","+rows+")" );
  88.  
  89.             wallCount = new Count(expand*2, expand2 * 3);                    
  90.             foodCount = new Count(level, level+expand2);
  91.             if (level > 6)
  92.             {
  93.                 int minFood = Mathf.RoundToInt(level/3);
  94.                 int maxFood = minFood +4;
  95.                 if (minFood > 5) minFood = 5;
  96.                 if (maxFood > 10) maxFood = 10;
  97.                 Debug.Log("BM: food c,r (" + minFood + "," + maxFood + ")");
  98.                 foodCount = new Count(minFood, maxFood);
  99.                 int minWall = expand * 2;
  100.                 int maxWall = expand2 * 3;
  101.                 if (level > 10) minWall = expand2 *3;
  102.                 if (level > 15) maxWall = expand2 * 4;
  103.  
  104.                 Debug.Log("BM: wall c,r (" + minWall + "," + maxWall + ")");
  105.                 wallCount = new Count(minWall, maxWall);
  106.  
  107.             }
  108.         }
  109.         else  
  110.         {
  111.             columnsBefore = columns;
  112.             rowsBefore = rows;
  113.         }
  114.  
  115.         for(int x = -1; x < columns+1;x++){
  116.             for(int y = -1;y < rows+1; y++){
  117.                 GameObject toInstantiate = floorTiles[Random.Range (0, floorTiles.Length ) ];
  118.                
  119.                 if( x== -1|| x== columns|| y== -1||y==rows){
  120.                     toInstantiate = outerWallTiles[ Random.Range(0, outerWallTiles.Length) ];
  121.                 }
  122.                
  123.                 GameObject instance = Instantiate( toInstantiate, new Vector3(x,y,0f), Quaternion.identity) as GameObject;
  124.                
  125.                 instance.transform.SetParent( boardHolder);
  126.                
  127.             }
  128.            
  129.         }
  130.     }
  131.    
  132.     Vector3 RandomPosition(){
  133.         int randomIndex = Random.Range(0, gridPositions.Count);
  134.         Vector3 randomPosition = gridPositions[randomIndex];
  135.         gridPositions.RemoveAt(randomIndex);
  136.         return randomPosition; 
  137.     }
  138.    
  139.     void LayoutObjectAtRandom(GameObject[] tileArray, int minimum, int maximum ){
  140.         int objectCount = Random.Range(minimum, maximum +1 );
  141.         for(int i=0;i < objectCount; i++){
  142.             Vector3 randomPosition = RandomPosition();
  143.             GameObject tileChoice = tileArray[ Random.Range (0, tileArray.Length) ];
  144.            
  145.             Instantiate(tileChoice, randomPosition, Quaternion.identity);
  146.         }
  147.        
  148.     }
  149.  
  150.     void LayoutObjectExit(){
  151.         //bila kolom,row => 16,16 lakukan normal
  152.         if( columns<16 || rows <16 ){
  153.             Instantiate(exit, new Vector3(columns -1, rows -1, 0f), Quaternion.identity);
  154.             return ;
  155.         }
  156.  
  157.         //cari posisi yg sesuai untuk exit
  158.         int exitX = Random.Range(13, columns-3);
  159.         int exitY = Random.Range(13, rows-3);
  160.         if (columns < 20 || rows < 20)
  161.         {
  162.             LayoutObjectAtRandom(wallTiles, 15, exitX );
  163.             Debug.Log("BM tambah wall "+exitX );
  164.         }
  165.         //hapus walltiles 3 kotak sekitarnya ganti dengan floor
  166.         for (int i = exitX-3; i < exitX+3; i++)
  167.         {
  168.             for (int j = exitY-3; j < exitY+3; j++)
  169.             {
  170.                 GameObject tileChoice = floorTiles[Random.Range(0, floorTiles.Length)];
  171.                 Instantiate(tileChoice, new Vector3(i, j, 0f), Quaternion.identity);
  172.             }
  173.         }
  174.         Debug.Log("BM posisi exit " + exitX+", "+exitY);
  175.         Instantiate(exit, new Vector3(exitX, exitY, 0f), Quaternion.identity);
  176.  
  177.     }
  178.  
  179.     public void SetupScene(int level){
  180.         BoardSetup(level);
  181.         InitiaLiseList();
  182.         Debug.Log("level:  " + level);
  183.         LayoutObjectAtRandom(wallTiles, wallCount.minimum, wallCount.maximum);
  184.         LayoutObjectAtRandom(foodTiles, foodCount.minimum, foodCount.maximum);
  185.         //enemy
  186.         int enemyCount0 = (int)Math.Log(level, 2f);
  187.         int enemyCount1 = enemyCount0;
  188.         if (level > 10 && level%2==1)
  189.         {
  190.             enemyCount1 = enemyCount0+(level-10); //Mathf.RoundToInt(wallCount.minimum / 2) ;
  191.         }
  192.         else if (level > 10 && level % 2 == 0)
  193.         {
  194.             enemyCount1 = enemyCount0 + (level - 10) -1;
  195.  
  196.         }
  197.         int selisih = enemyCount1 - enemyCount0;
  198.         int minSelish = 5 + Mathf.RoundToInt(level / 5);
  199.         if(selisih > minSelish)
  200.         {
  201.             enemyCount1 = enemyCount0 + minSelish;
  202.         }
  203.  
  204.         Debug.Log("BM: musuh "+enemyCount0+" - "+enemyCount1);
  205.         LayoutObjectAtRandom(enemyTiles, enemyCount0, enemyCount1);
  206.        
  207.         //exit
  208.         LayoutObjectExit();
  209.         //Instantiate(exit, new Vector3(columns -1, rows -1, 0f), Quaternion.identity);
  210.        
  211.     }
  212.    
  213. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement