I'm drawing the map like this:
-- 2 is the layers count for i=2,1,-1 do map(fetch"map/0.map"[i].bmp) end |
And I can check for collisions for map tiles with fget(mget(x/16,y/16))
to get the flag of the tile, all good here, except I can only check for collisions on the top layer, while every other layer is ignored.
How can I check for the flags on every layer?
@merwok No, I meant get
. get
is a function for looking inside userdata, which is necessary because mget
cannot look at anything other than the top layer, so a call to get
with fetch"map/0.map"[LAYER].bmp
as the first argument is being used instead of mget
.
Thank you, that works perfectly, this opens so many options now.
I could do collisions only on a specific layer, or all the layers!
I think I'm going to dedicate a specific layer to being solid and have a background and a foreground layer exclusively for decorations, I suppose I could even dedicate a layer to object markers, the layer would not be rendered but a function would scan the map and spawn the corresponding object on map load.
Here's some code resulting from my tinkering:
Layers definition:
LAYERS = { {index=4, name="background", render=true, render_objects=false, spawn_objects=false}, {index=3, name="solid", render=true, render_objects=false, spawn_objects=false}, {index=2, name="foreground", render=true, render_objects=true, spawn_objects=false}, {index=1, name="objects", render=false, render_objects=false, spawn_objects=true} } |
Note that the layers must be specified backwards if looping forwards
Drawing:
function _draw() cls(0) foreach(LAYERS, render_layer) end function render_layer(layer) if (layer.render) then map(fetch"map/0.map"[layer.index].bmp) end if (layer.render_objects) then -- Render all objects here, in this case my player is in a table but anything can be rendered here for p in all(player) do p:draw() end end end |
And finally, my collision functions:
function get_layer_tile(x,y,layer) return get(fetch"map/0.map"[layer].bmp,x,y) end function is_tile(tile_type,x,y) local tile = get_layer_tile(x/tile_width,y/tile_height,3) --Hardcoded to solid layer, which is 3 local has_flag = fget(tile,tile_type) return has_flag end function is_tile_solid(x,y) return is_tile(1,x,y) == 1 end |
At this point there's really no more need to actually check the flag of the sprite, but it can be useful for advanced behavior
[Please log in to post a comment]