地牢生成算法正在生成不良结果

时间:2018-09-24 13:15:22

标签: ios swift sprite-kit sktilemapnode

我编写了一种算法,使用SpriteKit在iOS的Swift中生成地牢。该代码按预期功能运行,生成具有四个边的规则多边形。但是,当我切换到8路邻接组时,生成器开始混乱。

这是生成地牢的代码,其中包含用于代表房间和走廊的单个瓷砖的一些屏幕截图。

import Foundation
import SpriteKit

private let mapWidth = 100
private let mapHeight = 100


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 y2 >= mapHeight - 1 || x2 >= mapWidth - 1 {
            return true
        }
        if x1 <= room.x2 + 2 && x2 >= room.x1 - 2 && y1 <= room.y2 + 2 && y2 >= room.y1 - 2 {
            return true
        }
        return false
    }
}

//******* 1 *******//
//******* 1 *******//
//******* 1 *******//

class TileEngine: SKNode {

    var rooms = [Room]()

    init (tileSize: CGSize) {
        super.init()

        let tile1 = SKTexture(imageNamed: "black")
        let tile2 = SKTexture(imageNamed: "red")

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

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

        let tileMap = SKTileMapNode(tileSet: tileSet, columns: mapWidth, rows: mapHeight, tileSize: tileSize)

        func createRooms() {

            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(red, forColumn: c, row: r)
                        } else if tileMap.tileGroup(atColumn: c, row: r) != red && tileMap.tileGroup(atColumn: c, row: r) != red {
                        tileMap.setTileGroup(black, forColumn: c, row: r)
                        }
                    }
                }
            }
            self.addChild(tileMap)
            tileMap.setScale(0.3)
        }

        //******* 2 *******//
        //******* 2 *******//
        //******* 2 *******//

        func placeRooms() {

            let numberOfRooms = Int(arc4random_uniform(25) + 15)

            for i in 0..<numberOfRooms {
                let w = Int(arc4random_uniform(8) + 4);
                let h =  Int(arc4random_uniform(8) + 4);
                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 != true {
                    let newCenter = newRoom.center

                    if rooms.isEmpty != true {
                        let prevCenter = rooms[rooms.count - 1].center

                        let rand = Int(arc4random_uniform(2) + 1)

                        if rand == 1 {
                            hCorridor(x1: Int(prevCenter.x), x2: Int(newCenter.x), y: Int(prevCenter.y))
                            vCorridor(y1: Int(prevCenter.y), y2: Int(newCenter.y), x: Int(newCenter.x))
                        } else {
                            vCorridor(y1: Int(prevCenter.y), y2: Int(newCenter.y), x: Int(prevCenter.x))
                            hCorridor(x1: Int(prevCenter.x), x2: Int(newCenter.x), y: Int(newCenter.y))
                        }
                    }
                }
                if failed != true {
                    rooms.append(newRoom)
                }
            }
            createRooms()
        }

        //******* 3 *******//
        //******* 3 *******//
        //******* 3 *******//

        func hCorridor(x1: Int, x2:Int, y: Int) {
            for x in min(x1,x2)..<max(x1,x2) {
                tileMap.setTileGroup(red, forColumn: x, row: y)
            }
        }
        func vCorridor(y1: Int, y2:Int, x: Int) {
            for y in Int(min(y1,y2))..<Int(max(y1,y2) + 1) {
                tileMap.setTileGroup(red, forColumn: x, row: y)
            }
        }

        placeRooms()
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

此代码生成基本的,干净的地下城,如下所示:

Image 1

Image 2

Image 3

我在Aseprite中绘制了一些图块,因为我想生成带纹理的贴图。我将它们放入8向邻接组中,以便将其加载到地图中。在我构建和运行时,代码不再生成常规的四边多边形。我开始收到不良结果:

就像几乎连接到另一个走廊但被切断的走廊一样:

Image 4

形状不规则的房间:

Image 5

每次我运行代码时都会生成这些。看来只有在我使用8向邻接组而不是单个图块时才会发生。

代码本身似乎没有任何问题。就在我添加图块组时,它搞砸了。我想要的结果是矩形房间,走廊相互连接。为什么平铺组将其弄乱了?我该如何解决?

Here是一篇文章,解释了该算法的工作方式和运行方式。

0 个答案:

没有答案