Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 地牢生成算法正在生成不需要的结果_Ios_Swift_Sprite Kit_Sktilemapnode - Fatal编程技术网

Ios 地牢生成算法正在生成不需要的结果

Ios 地牢生成算法正在生成不需要的结果,ios,swift,sprite-kit,sktilemapnode,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 hol

我使用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")
    }
}
<代码>导入基础 进口SpriteKit 私有let mapWidth=100 私人出租地图高度=100 教室{ //这些值保留房间每个角落的栅格坐标 变量x1:Int 变量x2:Int 变量y1:Int 变量y2:Int 变量宽度:Int 变量高度:Int //房间的中心点 var中心:CGPoint init(x:Int,y:Int,w:Int,h:Int){ x1=x x2=x+w y1=y y2=y+h 宽度=w 高度=h 中心=点(x:(x1+x2)/2, y:(y1+y2)/2) } func相交(房间:房间)->Bool{ 如果y2>=贴图高度-1 | x2>=贴图宽度-1{ 返回真值 } 如果x1=房间x1-2和y1=房间y1-2{ 返回真值 } 返回错误 } } //******* 1 *******// //******* 1 *******// //******* 1 *******// 类TileEngine:SKNode{ var房间=[房间]() 初始化(tileSize:CGSize){ super.init() 让tile1=SKTexture(图像名为:“黑色”) 让tile2=SKTexture(图像名称:“红色”) 设black=SKTileGroup(tileDefinition:SKTileDefinition(纹理:tile1,大小:CGSize(宽度:32,高度:32))) 设red=SKTileGroup(tileDefinition:SKTileDefinition(纹理:tile2,大小:CGSize(宽度:32,高度:32))) 让tileSet=SKTileSet(tilegroup:[黑色,红色]) 让tileMap=SKTileMapNode(tileSet:tileSet,列:mapWidth,行:mapHeight,tileSize:tileSize) func createRooms(){ 对于0中的c。。