代码之家  ›  专栏  ›  技术社区  ›  Steve G.

TileMap集中的自定义组没有选择正确的tile

  •  1
  • Steve G.  · 技术社区  · 7 年前

    Custom group screenshot

    我在TileMap上启用自动映射模式时使用它,结果如下:

    Render in the editor

    如您所见,有几个错误:

    Annotated render (green = proper tile, red = wrong tile)

    我尝试了编程和编辑器。行为是一样的。

    我的想法是,自动映射引擎在处理角点(左上,右下)时,会使用第一个匹配元素。以下是一些与引擎应具有的元素相关的错误:

    Annotated tiles

    所有错误的都在匹配的瓷砖之前。但是我找不到任何方法来重新排序tiles来测试这个理论(在编辑器中,甚至在配置文件中,因为组在二进制文件中)。

    有办法解决这个问题吗?

    提前谢谢你的时间。

    我试图删除并重新创建一些错误的图块,它证实了这个理论:在处理角度时,自动映射引擎会获取第一个匹配元素,即使其中的角被禁用。它不会检查是否有更好的瓷砖匹配也角规则。

    问题是一样的:有没有一种方法来纠正这种行为,或者我必须编码自动映射引擎?

    1 回复  |  直到 7 年前
        1
  •  2
  •   E. Huckabee    7 年前

    我已经做了两个星期了。我有一些严重的问题与SpriteKit SKTileMapNode自动平铺。我跟着 this article

    // Auto-tiling tool to bypass the auto-mapping in SpriteKit.
    // This tool opens up a lot more options than the one provided by SpriteKit
    class TileData {
    
        var map:SKTileMapNode
        var column:Int
        var row:Int
        //var array2D = [[Int]]()
    
        init(Column: Int, Row: Int, Map: SKTileMapNode) {
            column = Column
            row = Row
            map = Map
        }
    
        func returnTileData(C: Int, R: Int) -> Int {
            var directions = 0
            if map.tileGroup(atColumn: C, row: R) == tileGroups[48] {
                if map.tileGroup(atColumn: C - 1, row: R + 1) == tileGroups[48] {
                    directions += 1
                }
                if map.tileGroup(atColumn: C, row: R + 1) == tileGroups[48] {
                    directions += 2
                }
                if map.tileGroup(atColumn: C + 1, row: R + 1) == tileGroups[48] {
                    directions += 4
                }
                if map.tileGroup(atColumn: C - 1, row: R) == tileGroups[48] {
                    directions += 8
                }
                if map.tileGroup(atColumn: C + 1, row: R) == tileGroups[48] {
                    directions += 16
                }
                if map.tileGroup(atColumn: C - 1, row: R - 1) == tileGroups[48] {
                    directions += 32
                }
                if map.tileGroup(atColumn: C, row: R - 1) == tileGroups[48] {
                    directions += 64
                }
                if map.tileGroup(atColumn: C + 1, row: R - 1) == tileGroups[48] {
                    directions += 128
                }
            }
            let east = (directions & Dir.East.rawValue) == Dir.East.rawValue
            let west = (directions & Dir.West.rawValue) == Dir.West.rawValue
            let south = (directions & Dir.South.rawValue) == Dir.South.rawValue
            let north = (directions & Dir.North.rawValue) == Dir.North.rawValue
            let northEast = (directions & Dir.NorthEast.rawValue) == Dir.NorthEast.rawValue
            let northWest = (directions & Dir.NorthWest.rawValue) == Dir.NorthWest.rawValue
            let southEast = (directions & Dir.SouthEast.rawValue) == Dir.SouthEast.rawValue
            let southWest = (directions & Dir.SouthWest.rawValue) == Dir.SouthWest.rawValue
    
            return getTileData(east: east, west: west, north: north, south: south,
                           northWest: northWest, northEast: northEast, southWest:southWest, southEast: southEast)
        }
    
        func getTileData(east: Bool, west: Bool, north: Bool, south: Bool, northWest: Bool, northEast: Bool, southWest: Bool, southEast: Bool) -> Int {
            var directions = (east ? Dir.East.rawValue : 0) | (west ? Dir.West.rawValue : 0)  | (north ? Dir.North.rawValue : 0) | (south ? Dir.South.rawValue : 0)
    
            directions |= ((north && west) && northWest) ? Dir.NorthWest.rawValue : 0
            directions |= ((north && east) && northEast) ? Dir.NorthEast.rawValue : 0
            directions |= ((south && west) && southWest) ? Dir.SouthWest.rawValue : 0
            directions |= ((south && east) && southEast) ? Dir.SouthEast.rawValue : 0
    
            return directions
        }
    }
    

    我会尽量解释它到底是做什么的,以及如何让它工作。

    这是整个过程。当然,这可能是可以改进的。但这需要时间。

    // There are 47 possible tile orientations.
    let tileBits = [2, 8, 10, 11, 16, 18, 22, 24, 26, 27,
                    30, 31, 64, 66, 72, 74, 75, 80, 82, 86,
                    88, 90, 91, 94, 95, 104, 106, 107, 120,
                    122, 123, 126, 127, 208, 210, 214, 216, 218,
                    219, 222, 223, 248, 250, 251, 254, 255, 0]
    
    // Because of how buggy SKTileMapNodes currently are, two tile maps are needed for this process
    
    let tileMap = SKTileMapNode(tileSet: tileSet, columns: columns, rows: rows, tileSize: tileSize)
    let tileMap2 = SKTileMapNode(tileSet: tileSet, columns: columns, rows: rows, tileSize: tileSize)
    
    for c in 0..<tileMap.numberOfColumns {
        for r in 0..<tileMap.numberOfRows {
            // Fill your first tile map in here. 
            // Pretty standard stuff.
        }
    }
    
    for c in 0..<tileMap2.numberOfColumns {
        for r in 0..<tileMap2.numberOfRows {
            // Assign variable to the class and pass in the pre-filled tileMap
            let tile = TileData(Column: c, Row: r, Map: tileMap)
    
            // Get the bit-mask of the tile at (column, row)
            let number = tile.returnTileData(C: c, R: r)
    
            // If the array of tileBits contains the bitmask
            if tileBits.contains(number) {
                // Find out where it is at in the array
    
                guard let bit = tileBits.firstIndex(of: number) else { return }
    
                // Set the Tile Group
                tileMap2.setTileGroup(tileGroups[bit], forColumn: c, row: r)
            }
        }
    }
    // tileMap.setScale(0.2)
    self.addChild(tileMap2)
    

    有47种可能的瓷砖布置。基本上,您必须创建一个包含48个平铺组的数组。1-47是可能的瓷砖方向。48表示第一张地图中的填充空间。另一个数组存储所有47个位掩码,用于可能的块方向。它获取返回的位掩码并将其与该数组进行比较,以找到它所在的索引。然后它访问tile组的数组,并根据位掩码数组的索引将一个tile设置到第二个tile映射中。

    希望这是有道理的。

    这里是瓷砖精灵表与每个瓷砖组织从左到右,从最小到最大。

    2, 8, 10, 11, 16, 18, 22, 24, 26, 27, 30, 31, 64, 66, 72, 74, 75, 80, 82, 86, 88, 90, 91, 94, 95, 104, 106, 107, 120, 122, 123, 126, 127, 208, 210, 214, 216, 218, 219, 222, 223, 248, 250, 251, 254, 255, 0

    Image 1

    推荐文章