I've been working on mimicking this article for my game. The article is about procedurally generating dungeon maps. I have successfully translated most of the Haxe code in this article. Most of the code I have translated, I have done so by mostly guessing and trial/error. I got to a part I don't fully understand:
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.
Directly quoted from the article.
I am trying to figure out how to write these same for-loops in Swift. I understand the function parameters and what the functions are supposed to do. 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)
All my attempts to translate it have failed. And to be honest, I am utterly confused and have no idea what this line is even doing.
Here is my current attempt:
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 {
}
}
(It doesn't work by the way.)
I can add my code so far, but I don't think dumping a ton of code into a question is a good idea.
Working off @JeffWard's answer (meaning I assume he understands Haxe), a Swift translation would be:
let min_x = min(x1, x2)
let max_x = max(x1, x2)
for _ in min_x...max_x {
}
A few more things:
x1 == x2
. (But in the latter case, you should only pass through the loop twice.var
, ;
, or C style loops.EDIT:
I immediately came up with what many would say is better or "Swiftier"! :-)
for _ in min(x1,x2)...(max(x1,x2) {
}