难以理解这些 Haxe 函数

Trouble understanding these Haxe functions

我一直在为我的游戏模仿 this article。这篇文章是关于程序生成地牢地图的。我已经成功翻译了本文中的大部分 Haxe 代码。我翻译的大部分代码都是通过猜测和 trial/error 完成的。我到了一个我不完全理解的部分:

Now the goal is to connect each room so that we can walk through our dungeon and eventually reach an exit that leads to the next level. We can accomplish this by carving out corridors between the rooms.

We will need to add a point variable to the code to keep track of the center of each room created. Whenever we create and place a room, we determine its center and connect it to the previous room's center.

First, we'll implement the corridors:

private function hCorridor(x1:Int, x2:Int, y) {
    for (x in Std.int(Math.min(x1, x2))...Std.int(Math.max(x1, x2)) + 1) {
        // destory the tiles to "carve" out corridor
        map[x][y].parent.removeChild(map[x][y]);

        // place a new unblocked tile
        map[x][y] = new Tile(Tile.DARK_GROUND, false, false);

        // add tile as a new game object
        addChild(map[x][y]);

        // set the location of the tile appropriately
        map[x][y].setLoc(x, y);
    }
}

// create vertical corridor to connect rooms
private function vCorridor(y1:Int, y2:Int, x) {
    for (y in Std.int(Math.min(y1, y2))...Std.int(Math.max(y1, y2)) + 1) {
        // destroy the tiles to "carve" out corridor
        map[x][y].parent.removeChild(map[x][y]);

        // place a new unblocked tile
        map[x][y] = new Tile(Tile.DARK_GROUND, false, false);

        // add tile as a new game object
        addChild(map[x][y]);

        // set the location of the tile appropriately
        map[x][y].setLoc(x, y);
    }
}

These functions act in nearly the same way, but one carves out horizontally and the other vertically.

We need three values in order to do this. For horizontal corridors we need the starting x value, the ending x value, and the current y value. For vertical corridors we need the starting and ending y values along with the current x value.

Since we are moving from left to right we need the two corresponding x values, but only one y value since we won't be moving up or down. When we move vertically we will need the y values. In the for loop at the beginning of each function, we iterate from the starting value (x or y) to the ending value until we have carved out the entire corridor.

直接引用文章

我正在尝试找出如何在 Swift 中编写这些相同的 for 循环。我了解函数参数以及函数应该做什么。但是,我不明白这一行的 swift 等价物是什么:

for (x in Std.int(Math.min(x1, x2))...Std.int(Math.max(x1, x2)) + 1)

我所有的翻译尝试都失败了。老实说,我很困惑,甚至不知道这条线在做什么。

这是我目前的尝试:

func hCorridor(x1: Int, x2:Int, y: Int) {
    for x in x1...x2 {
        
    }
}
func vCorridor(y1: Int, y2:Int, x: Int) {
    for y in y1...y2 {
        
    }
}

(顺便说一下不行。)

到目前为止我可以添加我的代码,但我不认为将大量代码转储到一个问题中是个好主意。

I don't, however, understand what the swift equivalent for this line would be:

for (x in Std.int(Math.min(x1, x2))...Std.int(Math.max(x1, x2)) + 1)

这在 Haxe 中称为 range iteration

for (x in a...b) { }

它等同于下面的 C 风格的 for 循环:

for (x=a; x<b; x++) { }

一旦您意识到它只是使用几个函数调用作为 ab,那么该循环并不令人困惑。虽然我无法告诉您等效的 Swift 代码,但我可以告诉您该循环的伪代码:

var min_x = smaller_of(x1, x2);
var max_x = larger_of(x1, x2);
for (x = min_x; x<max_x + 1; x++) { }

注意:您可以忽略 Std.int(),因为它只是将 Float 转换为 Int。

您可能会发现 Haxe manual 对于您可能遇到的任何其他问题非常方便。

您引用的 haxe for 循环从最低的 x 值迭代到最高的 x 值。 Math.min(x1,x2) 将 return 最小值,因此它可以根据值从 x1 迭代到 x2,或从 x2 迭代到 x1。

Swift 似乎有 such comparison functions too,您当前的尝试似乎没有使用它或复制此逻辑。

处理@JeffWard 的回答(意思是我假设他理解 Haxe),a Swift 翻译为:

let min_x = min(x1, x2)
let max_x = max(x1, x2)
for _ in min_x...max_x {
}

还有几件事:

  • 如果您实际需要使用循环迭代器,请将下划线更改为变量。
  • 我没有考虑类型转换或 x1 == x2 之类的事情。 (但在后一种情况下,你应该只通过循环两次。
  • 假设以上答案正确翻译了您要执行的操作,则不需要 var; 或 C 样式循环。
  • 我试图强调这是"a"Swift翻译。我相信其他人可以做得更好。

编辑:

我立即想出了很多人会说 更好还是 "Swiftier" 更好! :-)

 for _ in min(x1,x2)...(max(x1,x2) {

 }