Rogue BoardManager 自动成成内外墙 和食物 敌人
来源:互联网 发布:windows wc命令 编辑:程序博客网 时间:2024/04/27 17:06
using UnityEngine;
using System;
using System.Collections.Generic; //Allows us to use Lists.允许使用数组
namespace Completed
{
public class BoardManager : MonoBehaviour
{
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;
}
}
public int columns = 8; //Number of columns in our game board.
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 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.
//Clears our list gridPositions and prepares it to generate a new board.
void InitialiseList ()
{
//Clear our list gridPositions.
gridPositions.Clear ();
//Loop through x axis (columns).
for(int x = 1; x < columns-1; x++)
{
//Within each column, loop through y axis (rows).
for(int y = 1; y < rows-1; y++)
{
//At each index add a new Vector3 to our list with the x and y coordinates of that position.
gridPositions.Add (new Vector3(x, y, 0f));
}
}
}
//Sets up the outer walls and floor (background) of the game board外墙和地板.
void BoardSetup ()
{
//Instantiate Board and set boardHolder to its transform.
boardHolder = new GameObject ("Board").transform;
//Loop along x axis, starting from -1 (to fill corner) with floor or outerwall edge tiles.
for(int x = -1; x < columns + 1; x++)
{
//Loop along y axis, starting from -1 to place floor or outerwall tiles.
for(int y = -1; y < rows + 1; y++)
{
//Choose a random tile from our array of floor tile prefabs and prepare to instantiate it.
GameObject toInstantiate = floorTiles[Random.Range (0,floorTiles.Length)];
//Check if we current position is at board edge, if so choose a random outer wall prefab from our array of outer wall tiles.
if(x == -1 || x == columns || y == -1 || y == rows)
toInstantiate = outerWallTiles [Random.Range (0, outerWallTiles.Length)];
//Instantiate the GameObject instance using the prefab chosen for toInstantiate at the Vector3 corresponding to current grid position in loop, cast it to GameObject.
GameObject instance =
Instantiate (toInstantiate, new Vector3 (x, y, 0f), Quaternion.identity) as GameObject;
//Set the parent of our newly instantiated object instance to boardHolder, this is just organizational to avoid cluttering hierarchy设置父类.
}
}
//RandomPosition returns a random position from our list gridPositions.
Vector3 RandomPosition ()
{
//Declare an integer randomIndex, set it's value to a random number between 0 and the count of items in our List gridPositions.
int randomIndex = Random.Range (0, gridPositions.Count);
//Declare a variable of type Vector3 called randomPosition, set it's value to the entry at randomIndex from our List gridPositions.
Vector3 randomPosition = gridPositions[randomIndex];
//Remove the entry at randomIndex from the list so that it can't be re-used.
gridPositions.RemoveAt (randomIndex);
//Return the randomly selected Vector3 position.
return randomPosition;
}
//LayoutObjectAtRandom accepts an array of game objects to choose from along with a minimum and maximum range for the number of objects to create.
void LayoutObjectAtRandom (GameObject[] tileArray, int minimum, int maximum)
{
//Choose a random number of objects to instantiate within the minimum and maximum limits
int objectCount = Random.Range (minimum, maximum+1);
//Instantiate objects until the randomly chosen limit objectCount is reached
for(int i = 0; i < objectCount; i++)
{
//Choose a position for randomPosition by getting a random position from our list of available Vector3s stored in gridPosition
Vector3 randomPosition = RandomPosition();
//Choose a random tile from tileArray and assign it to tileChoice
GameObject tileChoice = tileArray[Random.Range (0, tileArray.Length)];
//Instantiate tileChoice at the position returned by RandomPosition with no change in rotation
Instantiate(tileChoice, randomPosition, Quaternion.identity);
}
}
//SetupScene initializes 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 ();
//Reset our list of gridpositions.
InitialiseList ();
//Instantiate a random number of wall tiles based on minimum and maximum, at randomized positions.
LayoutObjectAtRandom (wallTiles, wallCount.minimum, wallCount.maximum);
//Instantiate a random number of food tiles based on minimum and maximum, at randomized positions.
LayoutObjectAtRandom (foodTiles, foodCount.minimum, foodCount.maximum);
//Determine number of enemies based on current level number, based on a logarithmic progression
int enemyCount = (int)Mathf.Log(level, 2f);
//Instantiate a random number of enemies based on minimum and maximum, at randomized positions.
LayoutObjectAtRandom (enemyTiles, enemyCount, enemyCount);
//Instantiate the exit tile in the upper right hand corner of our game board
Instantiate (exit, new Vector3 (columns - 1, rows - 1, 0f), Quaternion.identity);
}
}
}
using System;
using System.Collections.Generic; //Allows us to use Lists.允许使用数组
using Random = UnityEngine.Random;//使用unity自己的随机引擎
namespace Completed
{
public class BoardManager : MonoBehaviour
{
// Using Serializable allows us to embed a class with sub properties in the inspector.
//Serializable使得变量可以被Inspector界面显示 并可以展开或者收起 我实验关掉也没影响
[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;
}
}
public int columns = 8; //Number of columns in our game board.
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 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.
//Clears our list gridPositions and prepares it to generate a new board.
void InitialiseList ()
{
//Clear our list gridPositions.
gridPositions.Clear ();
//Loop through x axis (columns).
for(int x = 1; x < columns-1; x++)
{
//Within each column, loop through y axis (rows).
for(int y = 1; y < rows-1; y++)
{
//At each index add a new Vector3 to our list with the x and y coordinates of that position.
gridPositions.Add (new Vector3(x, y, 0f));
}
}
}
//Sets up the outer walls and floor (background) of the game board外墙和地板.
void BoardSetup ()
{
//Instantiate Board and set boardHolder to its transform.
boardHolder = new GameObject ("Board").transform;
//Loop along x axis, starting from -1 (to fill corner) with floor or outerwall edge tiles.
for(int x = -1; x < columns + 1; x++)
{
//Loop along y axis, starting from -1 to place floor or outerwall tiles.
for(int y = -1; y < rows + 1; y++)
{
//Choose a random tile from our array of floor tile prefabs and prepare to instantiate it.
GameObject toInstantiate = floorTiles[Random.Range (0,floorTiles.Length)];
//Check if we current position is at board edge, if so choose a random outer wall prefab from our array of outer wall tiles.
if(x == -1 || x == columns || y == -1 || y == rows)
toInstantiate = outerWallTiles [Random.Range (0, outerWallTiles.Length)];
//Instantiate the GameObject instance using the prefab chosen for toInstantiate at the Vector3 corresponding to current grid position in loop, cast it to GameObject.
GameObject instance =
Instantiate (toInstantiate, new Vector3 (x, y, 0f), Quaternion.identity) as GameObject;
//Set the parent of our newly instantiated object instance to boardHolder, this is just organizational to avoid cluttering hierarchy设置父类.
instance.transform.SetParent (boardHolder);
}}
}
//RandomPosition returns a random position from our list gridPositions.
Vector3 RandomPosition ()
{
//Declare an integer randomIndex, set it's value to a random number between 0 and the count of items in our List gridPositions.
int randomIndex = Random.Range (0, gridPositions.Count);
//Declare a variable of type Vector3 called randomPosition, set it's value to the entry at randomIndex from our List gridPositions.
Vector3 randomPosition = gridPositions[randomIndex];
//Remove the entry at randomIndex from the list so that it can't be re-used.
gridPositions.RemoveAt (randomIndex);
//Return the randomly selected Vector3 position.
return randomPosition;
}
//LayoutObjectAtRandom accepts an array of game objects to choose from along with a minimum and maximum range for the number of objects to create.
void LayoutObjectAtRandom (GameObject[] tileArray, int minimum, int maximum)
{
//Choose a random number of objects to instantiate within the minimum and maximum limits
int objectCount = Random.Range (minimum, maximum+1);
//Instantiate objects until the randomly chosen limit objectCount is reached
for(int i = 0; i < objectCount; i++)
{
//Choose a position for randomPosition by getting a random position from our list of available Vector3s stored in gridPosition
Vector3 randomPosition = RandomPosition();
//Choose a random tile from tileArray and assign it to tileChoice
GameObject tileChoice = tileArray[Random.Range (0, tileArray.Length)];
//Instantiate tileChoice at the position returned by RandomPosition with no change in rotation
Instantiate(tileChoice, randomPosition, Quaternion.identity);
}
}
//SetupScene initializes 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 ();
//Reset our list of gridpositions.
InitialiseList ();
//Instantiate a random number of wall tiles based on minimum and maximum, at randomized positions.
LayoutObjectAtRandom (wallTiles, wallCount.minimum, wallCount.maximum);
//Instantiate a random number of food tiles based on minimum and maximum, at randomized positions.
LayoutObjectAtRandom (foodTiles, foodCount.minimum, foodCount.maximum);
//Determine number of enemies based on current level number, based on a logarithmic progression
int enemyCount = (int)Mathf.Log(level, 2f);
//Instantiate a random number of enemies based on minimum and maximum, at randomized positions.
LayoutObjectAtRandom (enemyTiles, enemyCount, enemyCount);
//Instantiate the exit tile in the upper right hand corner of our game board
Instantiate (exit, new Vector3 (columns - 1, rows - 1, 0f), Quaternion.identity);
}
}
}
0 0
- Rogue BoardManager 自动成成内外墙 和食物 敌人
- Unity3D开发敌人自动攻击和自动寻路
- 什么是酸性食物和碱性食物
- 敌人
- swing 飞机大战 四 生成敌人飞机和敌人子弹
- Rogue Builds
- Rogue Guide
- 如何选择外墙瓷砖
- 食物
- 食物
- 养胃的食物和水果
- 噩梦系列篇其他敌人制作与敌人自动生成功能
- 酸性食物和碱性食物分别有哪些~
- MapReduce和并行数据库,朋友还是敌人?
- SOA和敏捷:是朋友?还是敌人?
- MapReduce和并行数据库,朋友还是敌人?
- 微信和它的真假敌人
- MapReduce和并行数据库,朋友还是敌人?
- 02_JNI中Java代码调用C代码,Android中使用log库打印日志,javah命令的使用,Android.mk文件的编写,交叉编译
- Kotlin语言文档翻译项目
- hdu2147巴什博弈
- IOS StoryBoard简单说明
- Fragment的练习
- Rogue BoardManager 自动成成内外墙 和食物 敌人
- JSON4:嵌套的JsonObject与JSONArray的取值
- Leetcode #223 Rectangle Area
- @MappedSuperclass 介绍
- HibernateTemplate实现分页
- 华为是怎样研发的(4)——器件选型
- HDOJ 5298 Solid Geometry Homework
- System类的学习
- Eclipse(MyEclipse) 自动补全