TileSet.lua 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. -- Copyright (c) 2011-2012 Casey Baxter
  2. -- See LICENSE file for details
  3. ---------------------------------------------------------------------------------------------------
  4. -- -= TileSet =-
  5. ---------------------------------------------------------------------------------------------------
  6. -- Setup
  7. local PATH = (...):gsub("[\\/]", ""):match(".+%.") or ''
  8. local ceil = math.ceil
  9. local Tile = require(PATH .. "Tile")
  10. local TileSet = {class = "TileSet"}
  11. TileSet.__index = TileSet
  12. ----------------------------------------------------------------------------------------------------
  13. -- Creates a new tileset.
  14. function TileSet:new(img, imgPath, name, tw, th, w, h, gid, space, marg, offx, offy, trans, tprop, ttype, prop)
  15. local ts = {}
  16. -- Public:
  17. ts.image = img -- The image of the tileset
  18. ts.imagePath = imgPath -- The path to the image file
  19. ts.name = name -- Name of the tilseset
  20. ts.tileWidth = tw -- The width of each tile in pixels
  21. ts.tileHeight = th -- The height of each tile in pixels
  22. ts.width = w -- The width of the tileset image in pixels
  23. ts.height = h -- The height of the tileset image in pixels
  24. ts.firstgid = gid -- The id of the first tile
  25. ts.spacing = space or 0 -- The spacing in pixels between each tile
  26. ts.margin = marg or 0 -- The margin in pixels surrounding the entire tile set.
  27. ts.trans = trans -- The transparency value. Only used when saving maps.
  28. ts.offsetX = offx -- The X offset.
  29. ts.offsetY = offy -- The Y offset.
  30. ts.tileProperties = tprop or {} -- Properties of contained tiles indexed by the tile's gid
  31. ts.tileTypes = ttype or {} -- Type of contained tiles indexed by the tile's gid
  32. ts.properties = prop or {} -- The properties of the tileset
  33. return setmetatable(ts, TileSet)
  34. end
  35. ----------------------------------------------------------------------------------------------------
  36. -- Returns the width in tiles
  37. function TileSet:tilesWide()
  38. return ceil( (self.width - self.margin*2 - self.spacing) /
  39. (self.tileWidth + self.spacing) )
  40. end
  41. ----------------------------------------------------------------------------------------------------
  42. -- Returns the height in tiles
  43. function TileSet:tilesHigh()
  44. return ceil( (self.height - self.margin*2 - self.spacing) /
  45. (self.tileHeight + self.spacing) )
  46. end
  47. ----------------------------------------------------------------------------------------------------
  48. -- Produces tiles from the settings and returns them in a table indexed by their id.
  49. -- These are cut out left-to-right, top-to-bottom.
  50. function TileSet:getTiles()
  51. local x,y = self.margin, self.margin
  52. local tiles = {}
  53. local quad = false
  54. local id = self.firstgid
  55. local imageWidth, imageHeight = self.image:getWidth(), self.image:getHeight()
  56. for i = 1, self:tilesHigh() do
  57. for j = 1, self:tilesWide() do
  58. quad = love.graphics.newQuad(x, y, self.tileWidth, self.tileHeight, imageWidth, imageHeight)
  59. tiles[id] = Tile:new(id, self, quad, self.tileWidth, self.tileHeight,
  60. self.tileProperties[id], self.tileTypes[id])
  61. x = x + self.tileWidth + self.spacing
  62. id = id + 1
  63. end
  64. x = self.margin
  65. y = y + self.tileHeight + self.spacing
  66. end
  67. return tiles
  68. end
  69. ----------------------------------------------------------------------------------------------------
  70. -- Return the TileSet class
  71. return TileSet