所有这些if陈述真的必要吗?

时间:2018-10-06 11:44:46

标签: ios swift sprite-kit

我具有将此功能连接到走廊的两个房间。看来确实效率不高,但是我找不到更好的方法来做到这一点。它是我生成地牢的BSP算法的一部分。您可以找到完整的算法here

public func createHall(left:Room, right:Room) {
    // Connects 2 rooms together with hallways

    // Reset hallways function to make sure its empty. hallways = [Room]()
    hallways = []

    // get width and height of first room
    let point1 = CGPoint(x: Int.random(in: (left.x1 + 1)..<(left.x2 - 1)),
                         y: Int.random(in: (left.y1 + 1)..<(left.y2 - 1)))

    // get width and height of second room
    let point2 = CGPoint(x: Int.random(in: (right.x1 + 1)..<(right.x2 - 1)),
                         y: Int.random(in: (right.y1 + 1)..<(right.y2 - 1)))

    let w = point2.x - point1.x
    let h = point2.y - point1.y

    if w < 0 {
        if h < 0 {
            if Double.random(in: 0..<1.0) > 0.5 {
                hallways.append(Room(X: Int(point2.x), Y: Int(point1.y), W: Int(abs(w)), H: 1))
                hallways.append(Room(X: Int(point2.x), Y: Int(point2.y), W: 1, H: Int(abs(h))))
            } else {
                hallways.append(Room(X: Int(point2.x), Y: Int(point2.y), W: Int(abs(w)), H: 1))
                hallways.append(Room(X: Int(point1.x), Y: Int(point2.y), W: 1, H: Int(abs(h))))
            }
        } else if h > 0 {
            if Double.random(in: 0..<1.0) > 0.5 {
                hallways.append(Room(X: Int(point2.x), Y: Int(point1.y), W: Int(abs(w)), H: 1))
                hallways.append(Room(X: Int(point2.x), Y: Int(point1.y), W: 1, H: Int(abs(h))))
            } else {
                hallways.append(Room(X: Int(point2.x), Y: Int(point2.y), W: Int(abs(w)), H: 1))
                hallways.append(Room(X: Int(point1.x), Y: Int(point1.y), W: 1, H: Int(abs(h))))
            }
        } else {
            hallways.append(Room(X: Int(point2.x), Y: Int(point2.y), W: Int(abs(w)), H: 1))
        }
    } else if w > 0 {
        if h < 0 {
            if Double.random(in: 0..<1.0) > 0.5 {
                hallways.append(Room(X: Int(point1.x), Y: Int(point2.y), W: Int(abs(w)), H: 1))
                hallways.append(Room(X: Int(point1.x), Y: Int(point2.y), W: 1, H: Int(abs(h))))
            } else {
                hallways.append(Room(X: Int(point1.x), Y: Int(point1.y), W: Int(abs(w)), H: 1))
                hallways.append(Room(X: Int(point2.x), Y: Int(point2.y), W: 1, H: Int(abs(h))))
            }
        } else if h > 0 {
            if Double.random(in: 0..<1.0) > 0.5 {
                hallways.append(Room(X: Int(point1.x), Y: Int(point1.y), W: Int(abs(w)), H: 1))
                hallways.append(Room(X: Int(point2.x), Y: Int(point1.y), W: 1, H: Int(abs(h))))
            } else {
                hallways.append(Room(X: Int(point1.x), Y: Int(point2.y), W: Int(abs(w)), H: 1))
                hallways.append(Room(X: Int(point1.x), Y: Int(point1.y), W: 1, H: Int(abs(h))))
            }
        } else {
            hallways.append(Room(X: Int(point1.x), Y: Int(point1.y), W: Int(abs(w)), H: 1))
        }
    } else {
        if h < 0 {
            hallways.append(Room(X: Int(point2.x), Y: Int(point2.y), W: 1, H: Int(abs(h))))
        } else if h > 0 {
            hallways.append(Room(X: Int(point1.x), Y: Int(point1.y), W: 1, H: Int(abs(h))))
        }
    }
}

其中Room是我编写的自定义类,用于计算矩形及其 中心:

class Room {
    var x1:Int
    var x2:Int
    var y1:Int
    var y2:Int
    var center:CGPoint

    init(X: Int, Y: Int, W: Int, H: Int) {
        x1 = X
        x2 = X + W
        y1 = Y
        y2 = Y + H
        center = CGPoint(x: (x1 + x2) / 2, y: (y1 + y2) / 2)
    }
}

我最成功的尝试:

func hCorridor(x1: Int, x2: Int, y: Int) {
    for x in min(x1,x2)...max(x1,x2) {
        hallways.append(Room(X: y, Y: y, W: 1, H: Int(abs(h))))
    }
}

func vCorridor(y1: Int, y2: Int, x: Int) {
    for y in min(y1,y2)...max(y1,y2) {
        hallways.append(Room(X: y, Y: y, W: Int(abs(w), H: 1))
    }
}

// Randomly choose to start with horizontal or vertical corridors
if Double.random(in: 0..<1.0) > 0.5 {
    hCorridor(x1: Int(point1.x), x2: Int(point2.x), y: Int(point1.y))
    vCorridor(y1: Int(point1.y), y2: Int(point2.y), x: Int(point2.x))
} else {
    vCorridor(y1: Int(point1.y), y2: Int(point2.y), x: Int(point1.x))
    hCorridor(x1: Int(point1.x), x2: Int(point2.x), y: Int(point2.y))
}

createHall()函数中的所有if语句真的必要吗?如果没有,哪种更好的书写方式?我所有的尝试都不像if语句那样有效。我的尝试给了我死胡同和无法进入的房间。

1 个答案:

答案 0 :(得分:2)

如果我正确理解了您的问题,那么您

  • 首先在左侧(分别是右侧)选择两个随机点point1point2 房间和
  • 然后将房间与从point1point2的两个“走廊”相连, 先水平然后垂直,反之亦然。

除了随机选择,如果您使用,则不需要if语句 minabs来计算道路坐标。 诸如此类(更多内联说明):

if Bool.random() {
    // Horizontally first, then vertically:
    // From point1 to (point2.x, point1.y):
    hallways.append(Room(X: Int(min(point1.x, point2.x)), Y: Int(point1.y),
                         W: Int(abs(point1.x - point2.x)), H: 1))
    // From (point2.x, point1.y) to point2:
    hallways.append(Room(X: Int(point2.x), Y: Int(min(point1.y, point2.y)),
                         W: 1, H: Int(abs(point1.y - point2.y))))
} else {
    // Vertically first, then Horizontally:
    // From point1 to (point1.x, point2.y):
    hallways.append(Room(X: Int(point1.x), Y: Int(min(point1.y, point2.y)),
                         W: 1, H: Int(abs(point1.y - point2.y))))
    // From (point1.x, point2.y) to point2:
    hallways.append(Room(X: Int(min(point1.x, point2.x)), Y: Int(point2.y),
                         W: Int(abs(point1.x - point2.x)), H: 1))
}