-1

I am working on a tile-based game. However, due to things such as furniture my map has multiple layers. I have (for the time being) created a square to represent my player. In order to stop my player walking on furniture, I need to make a function that checks for the layer. How do I do that? (Supposing I need to check the layer on the tile to the immediate right of my player) Pseudo-code ideas:

function checkLayers()
    for every layer in map 
        if layer == "furniturelayer" then
            stop player
        end
    end
end

EDIT: I have found a possible way to do it, but it does not work. I have an array containing the GID of all tiles that are collidable. I am then looping through all layers and checking if the tile has that GID. Code:

function gCheckGID(gMap, gLayer, tileX, tileY)

    tilex = gMap.layers[gLayer]:get(tileX, tileY)
    return tilex.id

end

function gCheckMovement(gMap, gArray, gTileX, gTileY)

    local retVal = true
    local layerArray = gMap.layers
    local layers = table.getn(layerArray)

    for layerCounter = 1, layers, 1 do
        currGID = gCheckGID(gMap, layerArray[layerCounter], gTileX, gTileY)
        for gidCounter = 1, table.getn(gArray), 1 do
            if currGID == gArray[gidCounter] then
                retVal = false
                break
            end
        end
    end

    return retVal

end

I can then use an if statement to get the result and determine whether to move my character or not.

Forrest4096
  • 159
  • 1
  • 8

1 Answers1

3

I assume you are using the Tiled library "Simple Tiled Implementation"? If so, I am the author. I have just recently added full collision support to STI using love.physics (Box2D). If you want to create a layer that is entirely collidable (such a sa furniture layer), then all you need to do is add a custom property to your layer in Tiled called "collidable" and set the value to "true".

Tiled now has a collision editor that can be used to add collision data to individual tiles in a tileset. STI also supports this out of the box with no custom properties required.

For more information about STI, check out the LOVE forum thread here.

Karai17
  • 923
  • 2
  • 9
  • 26
  • I am not using STI, I am using Advanced Tiled Loader. https://github.com/Kadoba/Advanced-Tiled-Loader – Forrest4096 Sep 24 '14 at 07:25
  • 2
    ATL is no longer being maintained and has been deprecated. STI supersedes ATL in every way. I would highly recommend switching. https://love2d.org/forums/viewtopic.php?f=5&t=2567&hilit=advanced&start=240#p159482 – Karai17 Sep 24 '14 at 15:50
  • Can you elaborate? What didn't work? What versions of Tiled and STI are you using? – Karai17 Jan 04 '17 at 15:29