我正在构建一种算法,以使用Swift 4和SpriteKit程序生成地牢。我已经完成了生成房间的工作,但是现在我需要一些帮助来裁剪房间。

let mapWidth = 225
let mapHeight = 150

var tileMap = SKTileMapNode()

var rooms = [Room]()

class Room {
    // these values hold grid coordinates for each corner of the room
    var x1:Int
    var x2:Int
    var y1:Int
    var y2:Int

    var width:Int
    var height:Int

    // center point of the room
    var center:CGPoint

    init (x:Int, y:Int, w:Int, h:Int) {

        x1 = x
        x2 = x + w
        y1 = y
        y2 = y + h
        width = w
        height = h
        center = CGPoint(x: ((x1 + x2) / 2),
                         y: ((y1 + y2) / 2))
    }
    func intersects(room:Room) -> Bool {
        if x1 <= room.x2 && x2 >= room.x1 && y1 <= room.y2 && y2 >= room.y1 {
            return true
        } else {
            return false
        }
    }
}

这是我用来确定房间大小,四个角的坐标以及tileMap网格中房间位置的代码段。

我从this article改编了这段代码。

从文章继续,我从房间生成器收集信息,并将其放置在我可以访问的数组中,以便可以在tileMap中划分房间。
func placeRooms() {

    let numberOfRooms = Int(arc4random_uniform(20) + 10)

    for i in 0..<numberOfRooms {
        let w = Int(arc4random_uniform(15) + 5);
        let h =  Int(arc4random_uniform(15) + 5);
        let x = Int(arc4random_uniform(UInt32(mapWidth)));
        let y = Int(arc4random_uniform(UInt32(mapHeight)));

        // create room with randomized values
        let newRoom = Room(x:x, y:y, w:w, h:h);

        var failed = false
        for otherRoom in rooms {
            if newRoom.intersects(room: otherRoom) {
                failed = true
            }
        }
        if failed == false {
            rooms.append(newRoom)
        }
    }
    createRooms()
}

但是现在我要使用所有信息来划分tileMap中的房间。我有四个角位,我只需要知道如何填写它们之间的所有内容。到目前为止,这是我得到的:
func createRooms() {
    let tile1 = SKTexture(imageNamed: "black")
    let tile2 = SKTexture(imageNamed: "gray")

    let black = SKTileGroup(tileDefinition: SKTileDefinition(texture: tile1, size: CGSize(width: 32, height: 32)))
    let gray = SKTileGroup(tileDefinition: SKTileDefinition(texture: tile2, size: CGSize(width: 32, height: 32)))

    let tileSet = SKTileSet(tileGroups: [gray,black])

    tileMap = SKTileMapNode(tileSet: tileSet, columns: mapWidth, rows: mapHeight, tileSize: CGSize(width: 32, height: 32))

    for c in 0..<tileMap.numberOfColumns {
        for r in 0..<tileMap.numberOfRows  {
            for i in 0..<rooms.count {
                if rooms[i].x1 <= c && rooms[i].x2 >= c && rooms[i].y1 <= r && rooms[i].y2 >= r {
                    tileMap.setTileGroup(gray, forColumn: c, row: r)
                    print("Room Tile")
                }
                if tileMap.tileGroup(atColumn: c, row: r) != gray {
                    tileMap.setTileGroup(black, forColumn: c, row: r)
                }
            }
        }
    }
    self.addChild(tileMap)
    tileMap.setScale(0.05)
}

编辑: ^^这是您唯一需要关注的部分,以防万一您感到不知所措。我只需要知道如何获取4个角坐标并填写两者之间的所有内容。

最佳答案

您可以解析房间,对于每个房间,都可以使用坐标解析图块。

就像是:

for room in rooms {
  for column in room.x1...(room.x1 + room.width) {
    for row in room.y1...(room.y1 + room.height) {
      tileMap.setTileGroup(tile1, forColumn: column, row: row)
    }
  }
}

除了存储x1y1x2y2heightwidth,您还可以使用CGRect(或基于它的自定义结构)。您唯一需要的就是原点和大小(也许是columnrowwidthheight吗?)。

关于ios - 在编写给定4个角位置的平铺 map 时无法编写算法来填充,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/52201305/

10-12 00:19