在 Swift 中检查坐标的安全方式和呈现视觉叠加图的智能方式
Secure way of checking coordinates in Swift and smart way to present vision overlay map
我有一个 [[Int]] 的坐标图,我用它来确定玩家是否已经到过某个地方并提供该地点的视野。
为了让它看起来更好看,我想在黑点的边缘有一个溶解的边框。参见示例 2:
为了做到这一点,我正在检查我的视觉阵列以了解该区域周围的 9 个区域,如果特定星座为黑色,它将选择相应的图像进行显示。
func getTileImage(index: Int) -> String {
let tileCoordinates = convertInto2D(index: index)
var tile : [Bool] = [false,false,false,false,false,false,false,false,false,false]
if (tileCoordinates.x >= 1) && (tileCoordinates.y >= 1) { if visionMap[tileCoordinates.x - 1][tileCoordinates.y - 1] == 0 { tile[1] = true } }
if (tileCoordinates.x >= 1) && (tileCoordinates.y >= 0) { if visionMap[tileCoordinates.x - 1][tileCoordinates.y - 0] == 0 { tile[2] = true } }
if (tileCoordinates.x >= 1) && (tileCoordinates.y < 19) { if visionMap[tileCoordinates.x - 1][tileCoordinates.y + 1] == 0 { tile[3] = true } }
if (tileCoordinates.x >= 0) && (tileCoordinates.y >= 1) { if visionMap[tileCoordinates.x - 0][tileCoordinates.y - 1] == 0 { tile[4] = true } }
if visionMap[tileCoordinates.x - 0][tileCoordinates.y - 0] == 0 { tile[5] = true }
if (tileCoordinates.x >= 0) && (tileCoordinates.y < 19) { if visionMap[tileCoordinates.x - 0][tileCoordinates.y + 1] == 0 { tile[6] = true } }
if (tileCoordinates.x < 14) && (tileCoordinates.y >= 1) { if visionMap[tileCoordinates.x + 1][tileCoordinates.y - 1] == 0 { tile[7] = true } }
if (tileCoordinates.x < 14) && (tileCoordinates.y >= 0) { if visionMap[tileCoordinates.x + 1][tileCoordinates.y - 0] == 0 { tile[8] = true } }
if (tileCoordinates.x < 14) && (tileCoordinates.y < 19) { if visionMap[tileCoordinates.x + 1][tileCoordinates.y + 1] == 0 { tile[9] = true } }
if tile[2] == true && tile[3] == true && tile[6] == true {return "righttop"}
if tile[3] == true && tile[6] == true && tile[7] == true && tile[8] == true && tile[9] == true {return"rightbottom"}
if tile[3] == true && tile[6] == true && tile[8] == true && tile[9] == true {return"rightbottom"}
if tile[6] == true && tile[7] == true && tile[8] == true && tile[9] == true {return"rightbottom"}
if tile[1] == true && tile[2] == true && tile[4] == true {return "lefttop"}
if tile[2] == true && tile[6] == false && tile[4] == false {return "top"}
if tile[4] == true && tile[2] == false && tile[8] == false {return "left"}
if tile[6] == true && tile[2] == false && tile[8] == false {return "right"}
if tile[7] == true && tile[4] == true && tile[8] == true {return "leftbottom"}
if tile[8] == true && tile[4] == false && tile[5] == false {return "bottom"}
if tile[9] == true && tile[6] == true && tile[8] == true {return "bottomright"}
if tile[4] == true && tile[1] == true && tile[2] == true && tile[3] == true && tile[6] == true {return "lefttopright"}
if tile[4] == true && tile[6] == true && tile[7] == true && tile[8] == true && tile[9] == true {return "leftrightbottom"}
if tile[1] == true && tile[2] == true && tile[4] == true && tile[7] == true && tile[8] == true {return "lefttopbottom"}
if tile[2] == true && tile[3] == true && tile[6] == true && tile[8] == true && tile[9] == true {return"righttopbottom"}
return ""
}
这段代码在我看来不是很聪明。
首先,有一种通用的方法可以检查我是否在坐标系的边界,这样我就不必检查每个图块的地图大小:
例如摆脱
if (tileCoordinates.x >= 1) && (tileCoordinates.y >= 1)
因为这取决于地图大小,目前为 15x20。
2:有没有更聪明的方法return对应的图像而不是检查所有潜在的可能性?
我很确定这被用于许多游戏。因此,如果有人可以提供帮助,我们将不胜感激。
意识到我只需要考虑水平和垂直相邻的字段后,我根据图像覆盖的边按以下顺序重命名图像:
"top", "left", "right", "bottom"
例如覆盖顶部和左侧的叠加图像将被称为“topleft”
这允许我将代码减少到 4 if 条件检查边是否不可见,在这种情况下将边的名称添加到字符串中:
func getTileImage(index: Int) -> String {
let tileCoordinates = convertInto2D(index: index)
var tile : [Int] = [1,1,1,
1,1,1,
1,1,1]
if (tileCoordinates.x >= 1) && (tileCoordinates.y >= 0) { if visionMap[tileCoordinates.x - 1][tileCoordinates.y - 0] == 0 { tile[1] = 0 } }
if (tileCoordinates.x >= 0) && (tileCoordinates.y >= 1) { if visionMap[tileCoordinates.x - 0][tileCoordinates.y - 1] == 0 { tile[3] = 0 } }
if (tileCoordinates.x >= 0) && (tileCoordinates.y < 19) { if visionMap[tileCoordinates.x - 0][tileCoordinates.y + 1] == 0 { tile[5] = 0 } }
if (tileCoordinates.x < 14) && (tileCoordinates.y >= 0) { if visionMap[tileCoordinates.x + 1][tileCoordinates.y - 0] == 0 { tile[7] = 0 } }
if (tile[1] == 1) && (tile[3] == 1) && (tile[5] == 1) && (tile[7] == 1) {return "noOverlay" }
var returnString = ""
if tile[1] == 0 {returnString.append("top")}
if tile[3] == 0 {returnString.append("left")}
if tile[5] == 0 {returnString.append("right")}
if tile[7] == 0 {returnString.append("bottom")}
return returnString
}
我有一个 [[Int]] 的坐标图,我用它来确定玩家是否已经到过某个地方并提供该地点的视野。
为了让它看起来更好看,我想在黑点的边缘有一个溶解的边框。参见示例 2:
为了做到这一点,我正在检查我的视觉阵列以了解该区域周围的 9 个区域,如果特定星座为黑色,它将选择相应的图像进行显示。
func getTileImage(index: Int) -> String {
let tileCoordinates = convertInto2D(index: index)
var tile : [Bool] = [false,false,false,false,false,false,false,false,false,false]
if (tileCoordinates.x >= 1) && (tileCoordinates.y >= 1) { if visionMap[tileCoordinates.x - 1][tileCoordinates.y - 1] == 0 { tile[1] = true } }
if (tileCoordinates.x >= 1) && (tileCoordinates.y >= 0) { if visionMap[tileCoordinates.x - 1][tileCoordinates.y - 0] == 0 { tile[2] = true } }
if (tileCoordinates.x >= 1) && (tileCoordinates.y < 19) { if visionMap[tileCoordinates.x - 1][tileCoordinates.y + 1] == 0 { tile[3] = true } }
if (tileCoordinates.x >= 0) && (tileCoordinates.y >= 1) { if visionMap[tileCoordinates.x - 0][tileCoordinates.y - 1] == 0 { tile[4] = true } }
if visionMap[tileCoordinates.x - 0][tileCoordinates.y - 0] == 0 { tile[5] = true }
if (tileCoordinates.x >= 0) && (tileCoordinates.y < 19) { if visionMap[tileCoordinates.x - 0][tileCoordinates.y + 1] == 0 { tile[6] = true } }
if (tileCoordinates.x < 14) && (tileCoordinates.y >= 1) { if visionMap[tileCoordinates.x + 1][tileCoordinates.y - 1] == 0 { tile[7] = true } }
if (tileCoordinates.x < 14) && (tileCoordinates.y >= 0) { if visionMap[tileCoordinates.x + 1][tileCoordinates.y - 0] == 0 { tile[8] = true } }
if (tileCoordinates.x < 14) && (tileCoordinates.y < 19) { if visionMap[tileCoordinates.x + 1][tileCoordinates.y + 1] == 0 { tile[9] = true } }
if tile[2] == true && tile[3] == true && tile[6] == true {return "righttop"}
if tile[3] == true && tile[6] == true && tile[7] == true && tile[8] == true && tile[9] == true {return"rightbottom"}
if tile[3] == true && tile[6] == true && tile[8] == true && tile[9] == true {return"rightbottom"}
if tile[6] == true && tile[7] == true && tile[8] == true && tile[9] == true {return"rightbottom"}
if tile[1] == true && tile[2] == true && tile[4] == true {return "lefttop"}
if tile[2] == true && tile[6] == false && tile[4] == false {return "top"}
if tile[4] == true && tile[2] == false && tile[8] == false {return "left"}
if tile[6] == true && tile[2] == false && tile[8] == false {return "right"}
if tile[7] == true && tile[4] == true && tile[8] == true {return "leftbottom"}
if tile[8] == true && tile[4] == false && tile[5] == false {return "bottom"}
if tile[9] == true && tile[6] == true && tile[8] == true {return "bottomright"}
if tile[4] == true && tile[1] == true && tile[2] == true && tile[3] == true && tile[6] == true {return "lefttopright"}
if tile[4] == true && tile[6] == true && tile[7] == true && tile[8] == true && tile[9] == true {return "leftrightbottom"}
if tile[1] == true && tile[2] == true && tile[4] == true && tile[7] == true && tile[8] == true {return "lefttopbottom"}
if tile[2] == true && tile[3] == true && tile[6] == true && tile[8] == true && tile[9] == true {return"righttopbottom"}
return ""
}
这段代码在我看来不是很聪明。
首先,有一种通用的方法可以检查我是否在坐标系的边界,这样我就不必检查每个图块的地图大小: 例如摆脱
if (tileCoordinates.x >= 1) && (tileCoordinates.y >= 1)
因为这取决于地图大小,目前为 15x20。
2:有没有更聪明的方法return对应的图像而不是检查所有潜在的可能性?
我很确定这被用于许多游戏。因此,如果有人可以提供帮助,我们将不胜感激。
意识到我只需要考虑水平和垂直相邻的字段后,我根据图像覆盖的边按以下顺序重命名图像:
"top", "left", "right", "bottom"
例如覆盖顶部和左侧的叠加图像将被称为“topleft”
这允许我将代码减少到 4 if 条件检查边是否不可见,在这种情况下将边的名称添加到字符串中:
func getTileImage(index: Int) -> String {
let tileCoordinates = convertInto2D(index: index)
var tile : [Int] = [1,1,1,
1,1,1,
1,1,1]
if (tileCoordinates.x >= 1) && (tileCoordinates.y >= 0) { if visionMap[tileCoordinates.x - 1][tileCoordinates.y - 0] == 0 { tile[1] = 0 } }
if (tileCoordinates.x >= 0) && (tileCoordinates.y >= 1) { if visionMap[tileCoordinates.x - 0][tileCoordinates.y - 1] == 0 { tile[3] = 0 } }
if (tileCoordinates.x >= 0) && (tileCoordinates.y < 19) { if visionMap[tileCoordinates.x - 0][tileCoordinates.y + 1] == 0 { tile[5] = 0 } }
if (tileCoordinates.x < 14) && (tileCoordinates.y >= 0) { if visionMap[tileCoordinates.x + 1][tileCoordinates.y - 0] == 0 { tile[7] = 0 } }
if (tile[1] == 1) && (tile[3] == 1) && (tile[5] == 1) && (tile[7] == 1) {return "noOverlay" }
var returnString = ""
if tile[1] == 0 {returnString.append("top")}
if tile[3] == 0 {returnString.append("left")}
if tile[5] == 0 {returnString.append("right")}
if tile[7] == 0 {returnString.append("bottom")}
return returnString
}