TileSet.lua 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. -- Tile
  2. local Tile = {class = "Tile"}
  3. Tile.__index = Tile
  4. function Tile.init(o)
  5. return setmetatable(o, Tile)
  6. end
  7. function Tile:draw(x, y, r, sx, sy, ox, oy)
  8. love.graphics.draw(self.tileset.image, self.quad, self.tileset.offsetX + x,
  9. self.tileset.offsetY + y, r, sx, sy, ox, oy)
  10. end
  11. local TileSet = {class = "TileSet"}
  12. TileSet.__index = TileSet
  13. function TileSet.init(ts)
  14. return setmetatable(ts, TileSet)
  15. end
  16. function TileSet:getTiles()
  17. local tiles = {}
  18. local id = self.firstgid
  19. local width, height = self.width, self.height
  20. local margin = self.margin
  21. local spacing = self.spacing
  22. local tilewidth, tileheight = self.tileWidth, self.tileHeight
  23. local advanceX, advanceY = tilewidth + spacing, self.tileHeight + spacing
  24. local endX = width - margin - tilewidth
  25. local endY = height - margin - tileheight
  26. local types, properties = self.tileTypes, self.tileProperties
  27. for y = margin, endY, advanceY do
  28. for x = margin, endX, advanceX do
  29. tiles[id] = Tile.init{
  30. id = id,
  31. type = types[id],
  32. width = tilewidth,
  33. height = tileheight,
  34. properties = properties[id],
  35. tileset = self,
  36. quad = love.graphics.newQuad(x, y, tilewidth, tileheight, width, height),
  37. }
  38. id = id + 1
  39. end
  40. end
  41. return tiles
  42. end
  43. return TileSet