Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using Random = UnityEngine.Random;
- public class BoardManager : MonoBehaviour
- {
- // Using Serializable allows us to embed a class with sub properties in the inspector.
- [Serializable]
- public class Count
- {
- public int minimum;
- public int maximum;
- public Count (int min, int max){
- minimum = min;
- maximum = max;
- }
- }
- public int columns = 8; //Number of columns in our game board.
- public int rows = 8; //Number of rows in our game board.
- private int columnsBefore = 0;
- private int rowsBefore = 0;
- public int minLevelExpand = 4;
- public Count wallCount = new Count (5, 9); //Lower and upper limit for our random number of walls per level.
- public Count foodCount = new Count (1, 5); //Lower and upper limit for our random number of food items per level.
- public GameObject exit; //Prefab to spawn for exit.
- 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 outer tile prefabs.
- private Transform boardHolder; //A variable to store a reference to the transform of our Board object.
- private List <Vector3> gridPositions = new List <Vector3> (); //A list of possible locations to place tiles.
- /*
- 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++){
- for(int y = 1; y < rows -1; y++){
- gridPositions.Add(new Vector3(x,y,0f));
- }
- }
- }
- void BoardSetup (int level)
- {
- boardHolder = new GameObject ("Board").transform;
- if(level > minLevelExpand)
- {
- int levelExpand = level - minLevelExpand;
- int expand = (int)Math.Log(levelExpand, 2f);
- int expand2 = expand + levelExpand;
- if (level % 3 == 1)
- {
- columns += expand;
- rows += expand;
- }
- Debug.Log("BM: c,r (" + columns +","+rows+")" );
- wallCount = new Count(expand*2, expand2 * 3);
- foodCount = new Count(level, level+expand2);
- if (level > 6)
- {
- int minFood = Mathf.RoundToInt(level/3);
- int maxFood = minFood +4;
- if (minFood > 5) minFood = 5;
- if (maxFood > 10) maxFood = 10;
- Debug.Log("BM: food c,r (" + minFood + "," + maxFood + ")");
- foodCount = new Count(minFood, maxFood);
- int minWall = expand * 2;
- int maxWall = expand2 * 3;
- if (level > 10) minWall = expand2 *3;
- if (level > 15) maxWall = expand2 * 4;
- Debug.Log("BM: wall c,r (" + minWall + "," + maxWall + ")");
- wallCount = new Count(minWall, maxWall);
- }
- }
- else
- {
- columnsBefore = columns;
- rowsBefore = rows;
- }
- for(int x = -1; x < columns+1;x++){
- for(int y = -1;y < rows+1; y++){
- GameObject toInstantiate = floorTiles[Random.Range (0, floorTiles.Length ) ];
- if( x== -1|| x== columns|| y== -1||y==rows){
- toInstantiate = outerWallTiles[ Random.Range(0, outerWallTiles.Length) ];
- }
- GameObject instance = Instantiate( toInstantiate, 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);
- }
- }
- void LayoutObjectExit(){
- //bila kolom,row => 16,16 lakukan normal
- if( columns<16 || rows <16 ){
- Instantiate(exit, new Vector3(columns -1, rows -1, 0f), Quaternion.identity);
- return ;
- }
- //cari posisi yg sesuai untuk exit
- int exitX = Random.Range(13, columns-3);
- int exitY = Random.Range(13, rows-3);
- if (columns < 20 || rows < 20)
- {
- LayoutObjectAtRandom(wallTiles, 15, exitX );
- Debug.Log("BM tambah wall "+exitX );
- }
- //hapus walltiles 3 kotak sekitarnya ganti dengan floor
- for (int i = exitX-3; i < exitX+3; i++)
- {
- for (int j = exitY-3; j < exitY+3; j++)
- {
- GameObject tileChoice = floorTiles[Random.Range(0, floorTiles.Length)];
- Instantiate(tileChoice, new Vector3(i, j, 0f), Quaternion.identity);
- }
- }
- Debug.Log("BM posisi exit " + exitX+", "+exitY);
- Instantiate(exit, new Vector3(exitX, exitY, 0f), Quaternion.identity);
- }
- public void SetupScene(int level){
- BoardSetup(level);
- InitiaLiseList();
- Debug.Log("level: " + level);
- LayoutObjectAtRandom(wallTiles, wallCount.minimum, wallCount.maximum);
- LayoutObjectAtRandom(foodTiles, foodCount.minimum, foodCount.maximum);
- //enemy
- int enemyCount0 = (int)Math.Log(level, 2f);
- int enemyCount1 = enemyCount0;
- if (level > 10 && level%2==1)
- {
- enemyCount1 = enemyCount0+(level-10); //Mathf.RoundToInt(wallCount.minimum / 2) ;
- }
- else if (level > 10 && level % 2 == 0)
- {
- enemyCount1 = enemyCount0 + (level - 10) -1;
- }
- int selisih = enemyCount1 - enemyCount0;
- int minSelish = 5 + Mathf.RoundToInt(level / 5);
- if(selisih > minSelish)
- {
- enemyCount1 = enemyCount0 + minSelish;
- }
- Debug.Log("BM: musuh "+enemyCount0+" - "+enemyCount1);
- LayoutObjectAtRandom(enemyTiles, enemyCount0, enemyCount1);
- //exit
- LayoutObjectExit();
- //Instantiate(exit, new Vector3(columns -1, rows -1, 0f), Quaternion.identity);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement