ios - 所有这些 if 语句真的有必要吗?

标签 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 语句那样有效。我的尝试给了我死胡同和无法进入的房间。

最佳答案

如果我正确理解你的问题,你

  • 先随机选择左边的两个点point1, point2(resp. right) 房间,和
  • 然后用从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))
}

关于ios - 所有这些 if 语句真的有必要吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52678708/

相关文章:

ios - 放置范围并避免在文本字段中使用特殊字符

ios - 将两个以上的参数传递给选择器,该选择器由字符串组成

ios - 我怎样才能让这个过程向相反的方向发展呢?

swift - 如何从另一个 NSView (不同的 swift 文件)更改 NSTextField 值

ios - 更新多个同名的 Spritekit 节点

swift - SKAction.resize 不起作用

android - 为 android 搜索 "PaintCode"

ios - AFNetworking 2.0并授权Imgur API

ios - 尝试使用 Storyboard ID 从 GameScene 移动到 ViewController 时发生 fatal error

ios - 添加了 Fabric Crashlytics 中的测试设备或 Beta 用户无法下载应用程序