C# 程序贴图生成-确定相邻分幅
我最初使用的是一个2d“Tile”数组来存储程序生成的地图及其各种内容 每个平铺都包含一个相邻列表,该列表允许每个平铺知道哪些顶点最靠近它,并在8条不同的边(直线相邻和对角相邻)上接触它 总体思路取自Amit的多边形贴图生成,但我试图通过使用网格设置而不是voronois来简化它,但是我遇到的麻烦比我最初认为的要多。我目前的困境是,当我放弃2d阵列时,如何计算邻接 以下是我在更改为列表之前的做法:C# 程序贴图生成-确定相邻分幅,c#,list,procedural-generation,C#,List,Procedural Generation,我最初使用的是一个2d“Tile”数组来存储程序生成的地图及其各种内容 每个平铺都包含一个相邻列表,该列表允许每个平铺知道哪些顶点最靠近它,并在8条不同的边(直线相邻和对角相邻)上接触它 总体思路取自Amit的多边形贴图生成,但我试图通过使用网格设置而不是voronois来简化它,但是我遇到的麻烦比我最初认为的要多。我目前的困境是,当我放弃2d阵列时,如何计算邻接 以下是我在更改为列表之前的做法: private void ConstructAdjacencyList() { // C
private void ConstructAdjacencyList() {
// Create Adjacency List
for (int x = 0; x < mapWidth; x++) {
for (int y = 0; y < mapHeight; y++) {
// Bool to find position of point
bool omitLeft = false; bool omitRight = false;
bool omitTop = false; bool omitBottom = false;
// Enable bools based on position, reset on each loop
if (x == 0)
omitLeft = true;
else if (x == mapWidth - 1)
omitRight = true;
if (y == 0)
omitTop = true;
else if (y == mapHeight - 1)
omitBottom = true;
// Add entries to list based on bool settings
if (!omitLeft) {
// Left center
islandMap[x,y].adjacent.Add(islandMap[x-1,y]);
if (!omitTop)
islandMap[x,y].adjacent.Add(islandMap[x-1,y-1]);
if (!omitBottom)
islandMap[x,y].adjacent.Add(islandMap[x-1,y+1]);
}
if (!omitTop) // Top Center
islandMap[x,y].adjacent.Add(islandMap[x,y-1]);
if (!omitBottom) // Bottom Center
islandMap[x,y].adjacent.Add(islandMap[x,y+1]);
if (!omitRight) {
// Right Center
islandMap[x,y].adjacent.Add(islandMap[x+1,y]);
if (!omitTop)
islandMap[x,y].adjacent.Add(islandMap[x+1,y-1]);
if (!omitBottom)
islandMap[x,y].adjacent.Add(islandMap[x+1,y+1]);
}
}
} // End Adjacency
Debug.Log ("Adjacencies Built");
}
抱歉,如果这太长了。。。我刚刚意识到它是相当大的-。-假设你没有做任何事情来扰乱积分的顺序,你可以治疗一次。我链接的示例是C语言,但其思想是不可知的 也就是说,假设在初始化过程中只执行一次&鉴于点数相对较少,您可以使用循环点列表并根据需要选择邻居的函数轻松地强制执行。我的C#有点生锈,但我说的是这样的:
private List<Vector2> getAdjacenctPointList(List<Vector2> pointsList, Vector2 point){
var adjacencyList = new List<Vector2>();
foreach (var pt in pointList){
var offset = Math.abs(pt.x - point.x) + Math.abs(pt.y - point.y);
if(offset > 0 && offset <= 1.0){
adjacencyList.add(pt);
}
}
return adjacencyList;
}
私有列表getAdjacenctPointList(列表点列表,向量2点){
var adjacencyList=新列表();
foreach(点列表中的变量pt){
var offset=Math.abs(pt.x-point.x)+Math.abs(pt.y-point.y);
如果(offset>0&&offset假设您没有做任何事情来扰乱点的顺序,那么您可以处理a。我链接的示例是C语言,但idea语言是不可知的
这就是说,假设在初始化过程中只执行一次&鉴于点数相对较少,您可以使用一个函数轻松地强制执行它,该函数循环遍历点列表并根据需要选择邻居。我的C#有点生疏,但我说的是这样的:
private List<Vector2> getAdjacenctPointList(List<Vector2> pointsList, Vector2 point){
var adjacencyList = new List<Vector2>();
foreach (var pt in pointList){
var offset = Math.abs(pt.x - point.x) + Math.abs(pt.y - point.y);
if(offset > 0 && offset <= 1.0){
adjacencyList.add(pt);
}
}
return adjacencyList;
}
私有列表getAdjacenctPointList(列表点列表,向量2点){
var adjacencyList=新列表();
foreach(点列表中的变量pt){
var offset=Math.abs(pt.x-point.x)+Math.abs(pt.y-point.y);
如果(offset>0&&offset我的最终答案是所涉及的工作量最少,并且允许我回收上面发布的原始二维数组代码,那就是简单地构造一个临时二维数组并将对它的所有引用存储在其中-根据需要创建所有邻接,然后进行处置(简单地通过丢失范围)二维数组的
下面是2d数组系统的实际方法。它只需要使用前几条语句,然后是嵌套的for循环:
private void ConstructAdjacencyList() {
Tile[,] tempArray = new Tile[mapWidth, mapHeight];
int count = 0;
// Populate the temp 2D array with list references
for (int x = 0; x < mapWidth; x++) {
for (int y = 0; y < mapHeight; y++) {
tempArray[x,y] = islandMap[count];
count++;
}
}
// Create Adjacency List using our TempArray
for (int x = 0; x < mapWidth; x++) {
for (int y = 0; y < mapHeight; y++) {
// Bool to find position of point
bool omitLeft = false; bool omitRight = false;
bool omitTop = false; bool omitBottom = false;
// Enable bools based on position, reset on each loop
if (x == 0)
omitLeft = true;
else if (x == mapWidth - 1) // Optimize with if else to split checks in half.
omitRight = true;
if (y == 0)
omitTop = true;
else if (y == mapHeight - 1)
omitBottom = true;
// Add entries to list based on bool settings
if (!omitLeft) {
// Left center
tempArray[x,y].adjacent.Add(tempArray[x-1,y]);
if (!omitTop)
tempArray[x,y].adjacent.Add(tempArray[x-1,y-1]);
if (!omitBottom)
tempArray[x,y].adjacent.Add(tempArray[x-1,y+1]);
}
if (!omitTop) // Top Center
tempArray[x,y].adjacent.Add(tempArray[x,y-1]);
if (!omitBottom) // Bottom Center
tempArray[x,y].adjacent.Add(tempArray[x,y+1]);
if (!omitRight) {
// Right Center
tempArray[x,y].adjacent.Add(tempArray[x+1,y]);
if (!omitTop)
tempArray[x,y].adjacent.Add(tempArray[x+1,y-1]);
if (!omitBottom)
tempArray[x,y].adjacent.Add(tempArray[x+1,y+1]);
}
}
} // End Adjacency
Debug.Log ("Adjacencies Built");
}
private void constructionadjacencylist(){
Tile[,]tempArray=新Tile[mapWidth,mapHeight];
整数计数=0;
//使用列表引用填充临时二维数组
对于(int x=0;x
为了确保这是我想要的结果,我通过设置一个随机立方体对其进行了视觉测试,并测试了该区域周围的各个点,以确保没有错误
最终结果如预期,如下所示:
谢谢你的帮助:)我的最终答案是,我只需构建一个临时二维数组,并将所有对它的引用存储在其中,根据需要创建所有邻接,然后进行处理(只需失去作用域),这是一个最简单的答案,所涉及的工作量最小,并允许我回收上面发布的原始二维数组代码二维数组的
下面是2d数组系统的实际方法。它只需要使用前几条语句,然后是嵌套的for循环:
private void ConstructAdjacencyList() {
Tile[,] tempArray = new Tile[mapWidth, mapHeight];
int count = 0;
// Populate the temp 2D array with list references
for (int x = 0; x < mapWidth; x++) {
for (int y = 0; y < mapHeight; y++) {
tempArray[x,y] = islandMap[count];
count++;
}
}
// Create Adjacency List using our TempArray
for (int x = 0; x < mapWidth; x++) {
for (int y = 0; y < mapHeight; y++) {
// Bool to find position of point
bool omitLeft = false; bool omitRight = false;
bool omitTop = false; bool omitBottom = false;
// Enable bools based on position, reset on each loop
if (x == 0)
omitLeft = true;
else if (x == mapWidth - 1) // Optimize with if else to split checks in half.
omitRight = true;
if (y == 0)
omitTop = true;
else if (y == mapHeight - 1)
omitBottom = true;
// Add entries to list based on bool settings
if (!omitLeft) {
// Left center
tempArray[x,y].adjacent.Add(tempArray[x-1,y]);
if (!omitTop)
tempArray[x,y].adjacent.Add(tempArray[x-1,y-1]);
if (!omitBottom)
tempArray[x,y].adjacent.Add(tempArray[x-1,y+1]);
}
if (!omitTop) // Top Center
tempArray[x,y].adjacent.Add(tempArray[x,y-1]);
if (!omitBottom) // Bottom Center
tempArray[x,y].adjacent.Add(tempArray[x,y+1]);
if (!omitRight) {
// Right Center
tempArray[x,y].adjacent.Add(tempArray[x+1,y]);
if (!omitTop)
tempArray[x,y].adjacent.Add(tempArray[x+1,y-1]);
if (!omitBottom)
tempArray[x,y].adjacent.Add(tempArray[x+1,y+1]);
}
}
} // End Adjacency
Debug.Log ("Adjacencies Built");
}
private void constructionadjacencylist(){
Tile[,]tempArray=新Tile[mapWidth,mapHeight];
整数计数=0;
//使用列表引用填充临时二维数组
对于(int x=0;xprivate void ConstructAdjacencyList() {
Tile[,] tempArray = new Tile[mapWidth, mapHeight];
int count = 0;
// Populate the temp 2D array with list references
for (int x = 0; x < mapWidth; x++) {
for (int y = 0; y < mapHeight; y++) {
tempArray[x,y] = islandMap[count];
count++;
}
}
// Create Adjacency List using our TempArray
for (int x = 0; x < mapWidth; x++) {
for (int y = 0; y < mapHeight; y++) {
// Bool to find position of point
bool omitLeft = false; bool omitRight = false;
bool omitTop = false; bool omitBottom = false;
// Enable bools based on position, reset on each loop
if (x == 0)
omitLeft = true;
else if (x == mapWidth - 1) // Optimize with if else to split checks in half.
omitRight = true;
if (y == 0)
omitTop = true;
else if (y == mapHeight - 1)
omitBottom = true;
// Add entries to list based on bool settings
if (!omitLeft) {
// Left center
tempArray[x,y].adjacent.Add(tempArray[x-1,y]);
if (!omitTop)
tempArray[x,y].adjacent.Add(tempArray[x-1,y-1]);
if (!omitBottom)
tempArray[x,y].adjacent.Add(tempArray[x-1,y+1]);
}
if (!omitTop) // Top Center
tempArray[x,y].adjacent.Add(tempArray[x,y-1]);
if (!omitBottom) // Bottom Center
tempArray[x,y].adjacent.Add(tempArray[x,y+1]);
if (!omitRight) {
// Right Center
tempArray[x,y].adjacent.Add(tempArray[x+1,y]);
if (!omitTop)
tempArray[x,y].adjacent.Add(tempArray[x+1,y-1]);
if (!omitBottom)
tempArray[x,y].adjacent.Add(tempArray[x+1,y+1]);
}
}
} // End Adjacency
Debug.Log ("Adjacencies Built");
}