使用 LOVE2D 制作平台游戏 - 垂直水平没有超过特定点的碰撞

Making a platformer with LOVE2D- Vertical levels lack collision past a certain point

对于在线 class,我正在使用 LÖVE2D 和 lua 创建平台游戏。关卡是由瓦片生成的——对于正常级别,瓦片是垂直填充的,对于垂直级别是水平填充的——这些瓦片的 ID 对应于它们是否是实心的。但是,在垂直级别中,在某个点之后,应该可碰撞的图块变为不可碰撞。 (可以看出here

感觉跟瓦片地图以及它的制作方式有关。我试过的是改变水平的宽度,这就是我如何发现我为垂直水平设置的宽度决定了碰撞被切断的地方。例如,如果关卡宽度为 16 格,则碰撞结束向下 16 格,如果关卡宽度为 10 格,则碰撞结束向下 10 格。

我还没有弄清楚是什么原因造成的。谁能帮我解决这个问题?这是我的瓦片地图代码:

TileMap = Class{}

function TileMap:init(width, height, orientation)
    self.width = width
    self.height = height
    self.tiles = {}
    self.orientation = orientation
end

--[[
    If our tiles were animated, this is potentially where we could iterate over all of them
    and update either per-tile or per-map animations for appropriately flagged tiles!
]]
function TileMap:update(dt)

end

--[[
    Returns the x, y of a tile given an x, y of coordinates in the world space.
]]
function TileMap:pointToTile(x, y)
    if self.orientation == 'horizontal' then
        if x < 0 or x > self.width * TILE_SIZE or y < 0 or y > self.height * TILE_SIZE then
            return nil
        end
    
        return self.tiles[math.floor(y / TILE_SIZE) + 1][math.floor(x / TILE_SIZE) + 1]
    elseif self.orientation == 'vertical' then
        if x < 0 or x > self.width * TILE_SIZE or y < 0 or y > self.height * TILE_SIZE then
            return nil
        end
        return self.tiles[math.floor(x / TILE_SIZE) + 1][math.floor(y / TILE_SIZE) + 1]
    end
end

function TileMap:render()
    for y = 1, self.height do
        for x = 1, self.width do
            self.tiles[y][x]:render()
        end
    end
end

这是我的垂直关卡制作工具:

VertLevelMaker = Class{}

function VertLevelMaker.generate(height, width)
    local tiles = {}
    local entities = {}
    local objects = {}

    local tileID = TILE_ID_GROUND
    
    -- whether we should draw our tiles with toppers
    local topper = true
    local tileset = math.random(20)
    local topperset = math.random(20)
    
    for y = 1, width do
        table.insert(tiles, {})
    end
    
    local leftPlat = height % 8
    local rightPlat = height % 8 + 4
    -- place all the ground as you go down
    for y = 1, 10 do
        tileID = TILE_ID_EMPTY
        for x = 1, width do
            table.insert(tiles[x], 
                Tile(x, y, tileID, nil, tileset, topperset))
        end
    end
    
    tileID = TILE_ID_GROUND
    for y = 11, height - 1 do
        table.insert(tiles[1],
            Tile(1, y, tileID, y == 11 and topper or nil, tileset, topperset))
        if leftPlat >= 8 then
            leftPlat = leftPlat % 8
            for x = 2, 6 do
                table.insert(tiles[x],
                    Tile(x, y, tileID, y == y and topper or nil, tileset, topperset))
            end
            tileID = TILE_ID_EMPTY
            for x = 7, width - 1 do
                table.insert(tiles[x],
                    Tile(x, y, tileID, nil, tileset, topperset))
            end
        elseif rightPlat >= 8 then
            rightPlat = rightPlat % 8
            tileID = TILE_ID_EMPTY
            for x = 2, width - 6 do
                table.insert(tiles[x],
                    Tile(x, y, tileID, nil, tileset, topperset))
            end
            tileID = TILE_ID_GROUND
            for x = width - 5, width - 1 do
                table.insert(tiles[x],
                    Tile(x, y, tileID, y == y and topper or nil, tileset, topperset))
            end
        else
            tileID = TILE_ID_EMPTY
            for x = 2, width - 1 do
                table.insert(tiles[x],
                    Tile(x, y, tileID, nil, tileset, topperset))
            end
        end
        tileID = TILE_ID_GROUND
        table.insert(tiles[width],
            Tile(width, y, tileID, y == 11 and topper or nil, tileset, topperset))
        leftPlat = leftPlat + 1
        rightPlat = rightPlat + 1
    end
    for x = 1, width do
        table.insert(tiles[x], 
            Tile(x, height, tileID, topper, tileset, topperset))
    end
    
    local map = TileMap(height, width, 'vertical')
    map.tiles = tiles
    
    return Level(entities, objects, map, width, height)
end

(对不起,如果措辞不是最好的,这是我在这里发布的第一个问题) (另外,在你问之前,高度和宽度是故意向后放入瓦片地图的,反之则不起作用)

我解决了!问题不在于图块的状态,而在于它们的排列方式。在垂直水平生成器中更改了图块的结构后 table 使图块水平排序,我只需要调整图块地图中的 pointToTile 函数以匹配它。