- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
关闭。这个问题需要更多focused .它目前不接受答案。
想改善这个问题吗?更新问题,使其仅关注一个问题 editing this post .
7年前关闭。
Improve this question
我正在尝试构建一个由瓷砖制成的随机城市,到目前为止我可以构建一个阵列,设置城市边缘,放置一个随机建筑物,例如教堂或工厂,并且可以在设置区域内放置具有随机旋转的瓷砖。
但我需要的是某种算法或例程,通过选择正确的瓷砖并以正确的方式旋转它来创建一个相互连接的街道迷宫,以免最终出现无法以某种方式到达的区域。
我拥有的瓷砖是-
特色瓷砖(如教堂或工厂)
直路
T型接头
十字路口
左角
右角
广场/回旋处/庭院
我发现了大量基于地牢的例子,但没有任何关于使用固定瓷砖和在固定区域匹配/旋转的例子,比如 10 个瓷砖 x 10 个瓷砖。
任何脚本提示都将不胜感激,因为我正在绕圈子制作极其复杂的循环功能,但仍然无法正常工作
到目前为止我所拥有的代码 -
public GameObject playerPrefab;
public GameObject cullGroup;
public GameObject cullPrefab;
//tile objects
public List<GameObject> _grid = new List<GameObject>(); //general buildings
public List<GameObject> _industrial = new List<GameObject>(); //industrial
public List<GameObject> _feature = new List<GameObject>(); //city feature
public List<GameObject> _largeSpecial = new List<GameObject>(); //large special building
public List<GameObject> _smallSpecial = new List<GameObject>(); //large special building
public List<GameObject> _docks = new List<GameObject>(); //docks
public List<GameObject> _roads = new List<GameObject>(); //outgoing roads
public List<GameObject> _edge = new List<GameObject>(); //city edge
public Vector2 citySize = new Vector2(10,10); //how many tiles in total
private int[] tileRoatation = new int[5] {0,90,180,270,0};
private int[,,] _cityMap;
private int mapType = 0;
//ingress vars
public int[,] roadIngress;
//feature vars
private int _tmpFeature;
private Vector2 _tmpPlacement;
//special vars
private int _tmpFeature1;
private Vector2 _tmpPlacement1;
//docks vars
private Vector2 _tmpDock;
private string _strDock;
private float _xAdjustment;
private float _zAdjustment;
//industrial vars
private Vector2 _indSector;
private int _indTotal = 0;
// Use this for initialization
void Start ()
{
_cityMap = new int[(int)citySize.x, (int)citySize.y, 2];
CreateMap();
}
// Update is called once per frame
void Update ()
{
}
void CreateMap()
{
//calc roads into city
RoadIngress();
//calc feature
Features();
//calculate tile adjustment from center
_xAdjustment = (_grid[0].transform.localScale.x * (citySize.x / 2) - (_grid[0].transform.localScale.x)/2);
_zAdjustment = (_grid[0].transform.localScale.z * (citySize.y / 2) - (_grid[0].transform.localScale.x)/2);
//place tiles
for(int z = 0; z < citySize.y; z++)
{
for(int x = 0; x < citySize.x; x++)
{
//instantiate tile
int tmpRot = tileRoatation[Random.Range(0,5)];
GameObject go = Instantiate(_grid[Random.Range(0, _grid.Count-1)],
new Vector3(
(x * _grid[0].transform.localScale.x) - _xAdjustment,
0 ,
(z * _grid[0].transform.localScale.z) - _zAdjustment),
Quaternion.Euler(0,tmpRot, 0)
) as GameObject;
_cityMap[x ,z ,1 ] = 1;;
_cityMap[x ,z ,0 ] = tmpRot;;
go.name = x +"/"+z;
go.transform.parent = this.transform;
//instantiate culling areas
/*
GameObject go2 = Instantiate(cullPrefab,
new Vector3(
(x * _grid[0].transform.localScale.x) - _xAdjustment,
0 ,
(z * _grid[0].transform.localScale.z) - _zAdjustment),
Quaternion.Euler(0,0, 0)
) as GameObject;
go2.name = x +"/"+z;
go2.transform.parent = cullGroup.transform;
go2.transform.localScale = new Vector3(51, 20, 51);
*/
//set map edges
if(x == 0 || z == 0 || x == citySize.x-1 || z == citySize.y-1 )
{
//does map have beach
if(mapType == 1 && z == 0)
{
go.GetComponent<MeshRenderer>().material.color = Color.blue;
}else if(roadIngress[x,z] == 1){
//set exit roads
go.SetActive(false);
GameObject rp = Instantiate(_roads[0], new Vector3(go.transform.position.x, 0 ,go.transform.position.z), Quaternion.Euler(0, 0, 0)) as GameObject;
rp.name = go.name;
rp.transform.parent = this.transform;
//go.name = "disabled";
//extend road
}else{
//go.GetComponent<MeshRenderer>().material.color = Color.green;
int tmpRot1 = tileRoatation[Random.Range(0,5)];
go.SetActive(false);
GameObject ep = Instantiate(_edge[Random.Range(0, _edge.Count-1)], new Vector3(go.transform.position.x, 0 ,go.transform.position.z), Quaternion.Euler(0, tmpRot, 0)) as GameObject;
ep.name = go.name;
ep.transform.parent = this.transform;
go.name = "disabled";
}
}
}
}
Debug.Log((citySize.x * citySize.y) * 0.1f);
Industrial();
Industrial();
Debug.Log(_indTotal);
if(citySize.x <((citySize.x * citySize.y) * 0.2f))
Industrial();
PlaceFeature();
PlaceLargeSpecial();
//docks
if(mapType == 1)
{
//go.GetComponent<MeshRenderer>().material.color = Color.red;
_tmpDock = new Vector2(Random.Range(1,citySize.y-1),0);
//_strDock = _tmpDock.x +"/"+_tmpDock.y;
}
PlaceDocks();
// spawn player
Instantiate(playerPrefab, new Vector3(0,2,0),Quaternion.identity);
}
//set roads leaving city
void RoadIngress()
{
//set roads leaving city
roadIngress = new int[(int)citySize.x, (int)citySize.y];
roadIngress[(int)Random.Range(1,citySize.x-1), 0] = 1; //east road or dock
roadIngress[0, (int)Random.Range(1,citySize.y-1)] = 1; //south road
roadIngress[(int)Random.Range(1,citySize.x-1), (int)citySize.y-1] = 1; //west road
roadIngress[(int)citySize.x-1, (int)Random.Range(1,citySize.y-1)] = 1; //north road
}
// choose main feature
void Features()
{
//choose main feature and placement
_tmpFeature = Random.Range(0,4);// 5x feature tiles
_tmpPlacement = new Vector2((int)Random.Range(2,citySize.x-2),(int)Random.Range(2,citySize.y-2));
//choose special large building and placement
_tmpFeature1 = Random.Range(0,4);// 5x feature tiles
_tmpPlacement1 = new Vector2((int)Random.Range(2,citySize.x-2),(int)Random.Range(2,citySize.y-2));
if(_tmpPlacement1 == _tmpPlacement)
{
_tmpPlacement1.x = _tmpPlacement1.x + 4;
if(_tmpPlacement1.x > citySize.x-2)
_tmpPlacement1.x -= citySize.x;
_tmpPlacement1.y = _tmpPlacement1.y + 4;
if(_tmpPlacement1.y > citySize.y-2)
_tmpPlacement1.y -= citySize.y;
}
}
//place feature
void PlaceFeature()
{
int tmpRot1 = tileRoatation[Random.Range(0,5)];
GameObject go1 = Instantiate(_feature[Random.Range(0, _feature.Count-1)],
new Vector3((_tmpPlacement.x * _grid[0].transform.localScale.x - (_grid[0].transform.localScale.x *.5f) - _xAdjustment),
0,
(_tmpPlacement.y * _grid[0].transform.localScale.z + (_grid[0].transform.localScale.z*.5f) - _zAdjustment)),
Quaternion.Euler(0,tmpRot1, 0)) as GameObject;
//clear feature area
GameObject tp = GameObject.Find(_tmpPlacement.x + "/" + _tmpPlacement.y);
tp.SetActive(false);
tp.name = "disabled";
tp = GameObject.Find(_tmpPlacement.x + "/" + (_tmpPlacement.y+1));
tp.SetActive(false);
tp.name = "disabled";
tp = GameObject.Find(_tmpPlacement.x-1 + "/" + _tmpPlacement.y);
tp.SetActive(false);
tp.name = "disabled";
tp = GameObject.Find(_tmpPlacement.x-1 + "/" + (_tmpPlacement.y+1));
tp.SetActive(false);
tp.name = "disabled";
}
//place large special
void PlaceLargeSpecial()
{
//check distance between feature and large special
int dist = (int)Vector2.Distance(_tmpPlacement, _tmpPlacement1);
//if to close move away
if((int)Vector2.Distance(_tmpPlacement, _tmpPlacement1)<3)
{
_tmpPlacement1.x = _tmpPlacement1.x+3;
if(_tmpPlacement1.x > citySize.x-2)
_tmpPlacement1.x = 3;
_tmpPlacement1.y = _tmpPlacement1.y+3;
if(_tmpPlacement1.y > citySize.y-2)
_tmpPlacement1.y = 3;
}
//Debug.Log(dist);
int tmpRot2 = tileRoatation[Random.Range(0,5)];
GameObject go2 = Instantiate(_largeSpecial[Random.Range(0, _largeSpecial.Count-1)],
new Vector3((_tmpPlacement1.x * _grid[0].transform.localScale.x - (_grid[0].transform.localScale.x *.5f) - _xAdjustment),
0,
(_tmpPlacement1.y * _grid[0].transform.localScale.z + (_grid[0].transform.localScale.z*.5f)) - _zAdjustment),
Quaternion.Euler(0,tmpRot2, 0)) as GameObject;
//clear large special area
GameObject tp = GameObject.Find(_tmpPlacement1.x + "/" + _tmpPlacement1.y);
tp.SetActive(false);
tp.name = "disabled";
tp = GameObject.Find(_tmpPlacement1.x + "/" + (_tmpPlacement1.y+1));
tp.SetActive(false);
tp.name = "disabled";
tp = GameObject.Find(_tmpPlacement1.x-1 + "/" + _tmpPlacement1.y);
tp.SetActive(false);
tp.name = "disabled";
tp = GameObject.Find(_tmpPlacement1.x-1 + "/" + (_tmpPlacement1.y+1));
tp.SetActive(false);
tp.name = "disabled";
}
//place docks
void PlaceDocks()
{
if(mapType == 1)
{
GameObject go3 = Instantiate(_docks[Random.Range(0, _docks.Count-1)],
new Vector3((_tmpDock.x * _grid[0].transform.localScale.x - (_grid[0].transform.localScale.x) - _xAdjustment),
0,
(_tmpDock.y * _grid[0].transform.localScale.z) - _zAdjustment),
Quaternion.Euler(0,0, 0)) as GameObject;
//clear dock area
/*
string[] tmp = _strDock.Split("/"[0]);
int t1 = int.Parse(tmp[0]);
for(int a=0; a<3; a++)
{
GameObject.Find(t1-a + "/" + 0).SetActive(false);
}
for(int b=0; b<3; b++)
{
GameObject.Find(t1-b + "/" + 1).SetActive(false);
}
*/
}
}
//set out industrial area
void Industrial()
{
//pick random spot industrial area
_indSector = new Vector2((int)Random.Range(2,citySize.x-2),(int)Random.Range(2,citySize.y-2));
int indSizeX = Random.Range(2,5);
int indSizeY = Random.Range(2,4);
_indTotal += (indSizeX*indSizeY);
//create area
for(int y = (int)_indSector.y; y < (int)_indSector.y+indSizeY; y++)
{
for(int x = (int)_indSector.x; x < (int)_indSector.x+indSizeX; x++)
{
//GameObject.Find(x + "/" + y).GetComponent<MeshRenderer>().material.color = Color.magenta;
GameObject tp = GameObject.Find(x + "/" + y);
//Debug.Log(x + "/" + y);
GameObject go4 = Instantiate(_industrial[Random.Range(0, _industrial.Count-1)],
new Vector3(tp.transform.position.x, 0, tp.transform.position.z), Quaternion.Euler(0,0, 0)) as GameObject;
tp.SetActive(false);
tp.name = "disabled";
go4.name = x +"/"+y;
go4.transform.parent = this.transform;
}
}
}
最佳答案
你能不能不使用数组(或多维数组,如果需要)并用位置填充它然后使用“随机”类从数组中提取这些位置?
喜欢...
s
Random rnd = new Random();
int i;
i = rnd.next(1,5);
Random rnd = new Random();
int i;
i = rnd.next(1,5);
int Positions[] = new Positions[5] {100, 200, 300, 400, 500};
Tilepos1 = Positions[i];
Tilepos2 = Positions[i];
Tilepos3 = Positions[i];
Tilepos4 = Positions[i];
Tilepos5 = Positions[i];
//you would have to make up your own Tileposition and figure out how to implement it into your system
//then make sure that the Tilepositions (Tilepos) don´t land on the same place
if (Tilepos1 == Tilepos2)
{
Tilepos1 = Positions[i];
}
if (Tilepos1 == Tilepos3)
{
Tilepos1 = Positions[i];
}
if (Tilepos1 == Tilepos4)
{
Tilepos1 = Positions[i];
}
if (Tilepos1 == Tilepos5)
{
Tilepos1 = Positions[i];
}
//then just repeat this process with Tilepos 2, 3, 4,`int i;
i = rnd.next(1,5);
int Positions[] = new Positions[5] {100, 200, 300, 400, 500};
Tilepos1 = Positions[i];
Tilepos2 = Positions[i];
Tilepos3 = Positions[i];
Tilepos4 = Positions[i];
Tilepos5 = Positions[i];
//you would have to make up your own Tileposition and figure out how to implement it into your system
//then make sure that the Tilepositions (Tilepos) don´t land on the same place
if (Tilepos1 == Tilepos2)
{
Tilepos1 = Positions[i];
}
if (Tilepos1 == Tilepos3)
{
Tilepos1 = Positions[i];
}
if (Tilepos1 == Tilepos4)
{
Tilepos1 = Positions[i];
}
if (Tilepos1 == Tilepos5)
{
Tilepos1 = Positions[i];
}
//then just repeat this process with Tilepos 2, 3, 4, 5
关于c# - 使用瓷砖随机生成城市街道,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24972880/
当我在 map 上放大得足够远时,TileLayer 消失,我看到一堆图 block ,上面写着“ map 数据尚不可用”。我相信这些图 block 可能来自提供商,但我不确定(我正在使用 ESRI
我有代码用红色方 block 填充浏览器窗口。 这个脚本正在寻找一条对 Angular 线,并且应该稍有延迟地依次改变每条对 Angular 线的颜色。也就是说,首先左上角的瓷砖变成不同的颜色,然后对
我是 Spring 框架的新手。在我的项目中,我使用 Spring 框架和 tiles 2(菜单、页眉、正文和页脚)。我想创建动态菜单并将其加载到菜单图 block (为菜单图 block 创建 Co
当包含的文件非常大时,似乎会产生开销。这与文件的内容无关,只是其大小,该文件除了注释掉的文本之外什么都没有 insertTemplate 的时间与文件的大小成正比。 comments.jsp 看起
有没有人有任何集成 Tiles 的经验进入Grails (而不是 SiteMesh)? 我找到了几篇这样的文章:http://devdevdev.wordpress.com/2009/01/据我所知,
使用 pygame 库在 python 中制作一个方 block 游戏。在游戏中,通过函数的计时器应该从左上角开始工作。一切都为我显示,但由于某种原因,该帐户未保留。你需要考虑我需要一个带秒的变量,因
有什么区别 和 你能举个例子吗? 最佳答案 参见 http://tiles.apache.org/2.2/framework/tiles-jsp/tlddoc/tiles/insertAttrib
我正在使用 spring mvc + tiles。在我的 web.xml 中我有 dispatcher org.springframework.web.servlet
我知道以前曾发布过类似主题的问题,但我遇到了一个在任何其他问题中都没有见过的错误。 我试图从二维数组制作平铺背景,但是当我运行它时,我收到此错误: current_tile = 纹理[tilemap[
我正在尝试使用 openMapTiles 矢量图 block 进行车辆路线。它似乎是一个加载速度非常快的数据源。我打算模拟很多车辆,所以重复查询 google maps api 不是一种选择。 有没有
我最近刚开始编程并遇到了这个问题。 如何计算瓷砖地板需要多少 block 瓷砖?地砖是一个宽度为20的正方形,地板的长度和宽度由用户输入。 我可以使用%计算出我需要多少个完整的瓷砖,但是一旦我到达侧面
我是 OpenLayers 的新手,想从 Google Maps API 切换过来。 现在我正在尝试找出如何将大图像放入 OpenLayers 中,就像我可以使用 Google Maps API 那样
Ionic 在加载 map 时会显示这些 splinter 的图 block 。 我试过从 Leaflet 加载 bower 和直接加载,但没有任何改变。我正在使用一个特殊的 Leaflet 指令,但
对于非视网膜我正在使用此代码请帮助如何在 ipad 中制作视网膜显示 我试过 Retina 项目
我已经通过 Active perl 安装了 Geo-OSM-Tiles。但问题是我不知道它安装在哪个路径。我还需要终端命令通过 Geo-OSM-Tiles(Active perl)下载图 block
关闭。这个问题是not reproducible or was caused by typos .它目前不接受答案。 这个问题是由于错别字或无法再重现的问题引起的。虽然类似的问题可能是on-top
我想将 tiles 定义名称直接写到模板中。但是我不知道如何访问定义名称。 为了说明我想做什么,请看一下这个解决方法:
今天早上花了一段时间寻找一个概括性的问题,以指出有关 as_strided 和/或 how to make generalized window functions 的问题的重复项。关于如何(安全地)
所以我正在编写一个面向对象的 Tile Slider Puzzle 游戏,我感觉好像我的代码是正确的,而且当我构建项目时,没有抛出任何错误。但是,当我运行我的代码(Visual Studio 2015
我正在使用 Spring MVC 3 和 Apache Tiles 3。 我想为我的其中一个页面添加自定义标题。我希望这个标题存在于特定的 View jsp 中而不是布局 jsp 中。 这是我正在做的
我是一名优秀的程序员,十分优秀!