Rogue BoardManager 自动成成内外墙 和食物 敌人

using UnityEngine;

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);

}

}

}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-09-30 16:41:23

Rogue BoardManager 自动成成内外墙 和食物 敌人的相关文章

poi excel自动转换成javabean 支持引用类型属性二级转换

最近项目需要使用excel导入功能,导入学生的时候需要指定所在班级,使用excel一次性导入! 将以前的代码改改支持属性内引用类的转换. 测试对象为User对象,javabean结构: private String username; private int id; private String name; private Date birthday; private long height; private double salary; private User user; 使用测试代码: M

Apache 实现http协议自动转成https协议,Apache 防DDOS攻击 使用mod_rpaf模块 mod_evasive模块

一:实践环境介绍 二:配置Apache,实现访问http页面自动转成https页面 需求1:整个站点都实现http自动转https 需求2:整个站点使用http协议,只有某个单独页面从http自动转到https 实验环境介绍 使用yum 安装apache Apache版本 # httpd -v Server version: Apache/2.2.15 (Unix) Server built:   Aug 13 2013 17:29:28 使用yum 安装openssl # yum instal

利用反射自动封装成实体对象

利用此方法的时候需要传递的参数的名称,必须以行号结尾,去掉行号就是属性名称,比如页面传递name+rowNo,那么实体对象的属性名应该为name.代码如下 //获取页面数据,自动封装成bean对象 public List getObjectList(Class clazz,String[] rowNos) throws Exception{ List objList = new ArrayList(); for(int i=0;rowNos!=null && i<rowNos.len

Java实现文件自动打包成zip并下载的代码

import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; public class ZipOutputStreamDemo { public static void main(String[] args) throws Exception { by

游戏资源自动转换成Lua表

关于这个问题,几年前已经做过一个工具,自动导出成一个c++的struct,然后用vector存储这些数据,然而在实践中发现在遇到多层嵌套时,这种数据表现是非常乏力的.比如掉落表会配多个掉落物,每个掉落物都有数量.概率等.那么最好是将物品ID,数量,概率设置为一个struct然后用一个vector存储,而不是用drop_id1, drop_id2, drop_num1,drop_num2来表示,类似这样的情况是很常见的.后来工作中用Lua比较多,也曾写过一个将表导出lua的工具,对上述问题的解决方

java_js_method实现输入小写字母自动转化成大写的代码实例

方法一: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Con

vs生成配置release自动恢复成debug无法配置的问题

软件为visual studio 2015,右键单个项目选属性,设置“生成配置”release,保存后,再次右键属性,却发现又自动恢复成debug,怎么回事?是无法配置吗?点击生成试试,果然2b了. 解决方法: 在整个解决方案上右键属性,然后选配置属性-->配置--> 一一配置需要设置release的项目,然后保存即可.

WebApi系列~FromUri参数自动解析成实体的要求

回到目录 关于webapi我之前写了一些文章,大家可以根据目录去浏览,今天要说的是个怪问题,也是被我忽略的一个问题,当你的Url参数需要被Api自动解析成实体的属性,实事上是要有条件的,不是所以属性都可以被自动赋值的,下面我们就来看看先决条件: 条件一:类属性名称必须和参数名称相同 条件二:API参数必须以[FromUri]来修饰 条件三:属性类型为“类”的,如果使用类名(导航属性在本类的名称,可以不是类的原名).属性名的形式,例如Page.PageIndex 条件四:属性类型为“数组,集合”时

代码声明的JNI方法自动生成成在Eclipse里利用javah把java

阿萨德法国染色阿城v In Eclipse, open Run | External Tools | External Tools Configurations- Name: MyProject javah Location: ${system_path:javah} Working Directory: ${project_loc}/jni Arguments: -classpath "${project_classpath}; ${env_var:ANDROID_SDK_HOME}/plat