Skip to content

Instantly share code, notes, and snippets.

@KaffeDiem
Created March 25, 2021 14:42
Show Gist options
  • Save KaffeDiem/f3160c9d66db567604ec36f32bd4a260 to your computer and use it in GitHub Desktop.
Save KaffeDiem/f3160c9d66db567604ec36f32bd4a260 to your computer and use it in GitHub Desktop.
Isometric Tiles in Lua
-- Drawing the map as isometric tiles
self.tilesHovered = {} -- Reset the tilesHovered table every frame
for i = 1, #self.map do -- Loop trough rows
for j = 1, #self.map[i] do -- Loop through cols in the rows
if self.map[i][j] ~= 0 then -- If there is a tile to draw
local x =
self.x + -- Starting point
(j * ((self.tileWidth / 2) * self.scale)) - -- The width on rows
(i * ((self.tileWidth / 2) * self.scale)) -- The width on cols
local y =
self.y +
(i * ((self.tileHeight / 4) * self.scale)) + -- The height on rows
(j * ((self.tileHeight / 4) * self.scale)) -- The width on cols
-- Take the height map into account
local y = y - self.mapheight[i][j] * 8
-- Draw the tiles
love.graphics.draw(self.tilesheet, self.tiles[self.map[i][j]],
x, y,
0,
self.scale,
self.scale
)
if self.tmx > x and -- Hitbox between mouse and tile
self.tmx < x + self.tileWidth * self.scale and
self.tmy > y + self.tileHeight / 2 and
self.tmy < y + self.tileHeight * self.scale then
-- Add the tile to tiles hovered, if mouse is on top of it
table.insert(self.tilesHovered, {i, j, x, y})
end
end
end
end
@KaffeDiem
Copy link
Author

Scale is just some value used to scale the images in size (e.g. a 32x32px image being drawn as 64x64px)
Map and Mapheight should be tables with values 1 .. n as entries, where n are the amount of quads in self.tiles.
Line 25-30 is just collision between mouse and the tiles.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment