[Solved] Empty space in tile grid?

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
Ryne
Party member
Posts: 444
Joined: Fri Jan 29, 2010 11:10 am

[Solved] Empty space in tile grid?

Post by Ryne »

Hi, I was wondering if there was a way to have an empty space in a tile grid without using a transparent tile? I would like to have an empty grid, and just place tiles where I would want platforms, since I'm making a platformer.
Last edited by Ryne on Sat Oct 23, 2010 3:55 am, edited 1 time in total.
@rynesaur
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: Empty space in tile grid?

Post by kikito »

Sure. It depends a lot on your code, though. Are you using the tile tutorial?

If so, then you must assign one of the 'numbers' of the map to the empty tile. Assuming that you assign them to 0, you can do this:

Code: Select all

function draw_map()
   for y=1, map_display_h do
      for x=1, map_display_w do                                                        
         if map[y+map_y][x+map_x] ~= 0 -- transparent tiles (0) are ignored
           love.graphics.draw(
              tile[map[y+map_y][x+map_x]],
              (x*tile_w)+map_offset_x,
              (y*tile_h)+map_offset_y )
         end
      end
   end
end

When I write def I mean function.
User avatar
Ryne
Party member
Posts: 444
Joined: Fri Jan 29, 2010 11:10 am

Re: Empty space in tile grid?

Post by Ryne »

Great!, thanks again :). I might as well ask here before posting another thread, but I would like to spawn a player sprite into a grid coordinate. I'm not sure if that is as easy as it sounds. For example. To draw an animated sprite at a screen coordinate I would use this code:

Code: Select all

anim:draw(100,329)
My first instinct was to try and place the unit into the grid using

Code: Select all

anim:draw(map[1], map[3])
or

Code: Select all

anim:draw(#map[1], #map[3])
but apparently neither of these work. I was also thinking of a more complicated way that would involve converting grid coordinates into screen coordinates but I'm unsure of how to go about doing it. Any Ideas?
@rynesaur
User avatar
zac352
Party member
Posts: 496
Joined: Sat Aug 28, 2010 8:13 pm
Location: In your head.
Contact:

Re: Empty space in tile grid?

Post by zac352 »

Ryne wrote:Great!, thanks again :). I might as well ask here before posting another thread, but I would like to spawn a player sprite into a grid coordinate. I'm not sure if that is as easy as it sounds. For example. To draw an animated sprite at a screen coordinate I would use this code:

Code: Select all

anim:draw(100,329)
My first instinct was to try and place the unit into the grid using

Code: Select all

anim:draw(map[1], map[3])
or

Code: Select all

anim:draw(#map[1], #map[3])
but apparently neither of these work. I was also thinking of a more complicated way that would involve converting grid coordinates into screen coordinates but I'm unsure of how to go about doing it. Any Ideas?
Shouldn't you multiply the position by the tilesize, then subtract the camera?
Hello, I am not dead.
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: Empty space in tile grid?

Post by kikito »

Usually a mapToWorld and a worldToMap function would be quite useful in general. I'm a bit surprised that the tutorial itself doesn't include that.

Coding real quick and without testing anything at all:

Code: Select all

function mapToWorld(x,y)
    return (x*tile_w)+map_offset_x, (y*tile_h)+map_offset_y )
end
function worldToMap(x,y)
    return math.floor((x - map_offset_x) / tile_w), math.floor((y - map_offset_y) / tile_y) 
end
Then you can use them like this:

Code: Select all

-- transform the coordinates of cell 1,4 into world coordinates
local x,y = mapToWorld(1,4)

-- get the indexes of the cell that holds the coordinates x=200, y=300
local cx,cy = worldToMap(200, 300)
You can actually use mapToWorld to simplify the drawing loop:

Code: Select all

function draw_map()
   for y=1, map_display_h do
      for x=1, map_display_w do                                                       
         if map[y+map_y][x+map_x] ~= 0 -- transparent tiles (0) are ignored
           love.graphics.draw( tile[map[y+map_y][x+map_x]], mapToWorld(x,y)) -- calculations are done inside mapToWorld
         end
      end
   end
end
Now, to answer Ryne's question, in order to draw an animation in cell {3,10} he could do:

Code: Select all

anim:draw(mapToWorld(3,10))
Again, I warn you that the code above is completely untested.

PS: "world", not "screen". In the future you might want to add scrolling.
When I write def I mean function.
User avatar
Ryne
Party member
Posts: 444
Joined: Fri Jan 29, 2010 11:10 am

Re: Empty space in tile grid?

Post by Ryne »

kikito wrote:Usually a mapToWorld and a worldToMap function would be quite useful in general. I'm a bit surprised that the tutorial itself doesn't include that.
Alright, that makes a lot of sense, thanks a lot for the help guys. I'll post my project once I get a little more finished. :megagrin:
@rynesaur
Post Reply

Who is online

Users browsing this forum: No registered users and 68 guests