1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283 |
- -- Copyright (c) 2011-2012 Casey Baxter
- -- See LICENSE file for details
- ---------------------------------------------------------------------------------------------------
- -- -= TileSet =-
- ---------------------------------------------------------------------------------------------------
- -- Setup
- local PATH = (...):gsub("[\\/]", ""):match(".+%.") or ''
- local ceil = math.ceil
- local Tile = require(PATH .. "Tile")
- local TileSet = {class = "TileSet"}
- TileSet.__index = TileSet
- ----------------------------------------------------------------------------------------------------
- -- Creates a new tileset.
- function TileSet:new(img, imgPath, name, tw, th, w, h, gid, space, marg, offx, offy, trans, tprop, ttype, prop)
- local ts = {}
-
- -- Public:
- ts.image = img -- The image of the tileset
- ts.imagePath = imgPath -- The path to the image file
- ts.name = name -- Name of the tilseset
- ts.tileWidth = tw -- The width of each tile in pixels
- ts.tileHeight = th -- The height of each tile in pixels
- ts.width = w -- The width of the tileset image in pixels
- ts.height = h -- The height of the tileset image in pixels
- ts.firstgid = gid -- The id of the first tile
- ts.spacing = space or 0 -- The spacing in pixels between each tile
- ts.margin = marg or 0 -- The margin in pixels surrounding the entire tile set.
- ts.trans = trans -- The transparency value. Only used when saving maps.
- ts.offsetX = offx -- The X offset.
- ts.offsetY = offy -- The Y offset.
- ts.tileProperties = tprop or {} -- Properties of contained tiles indexed by the tile's gid
- ts.tileTypes = ttype or {} -- Type of contained tiles indexed by the tile's gid
- ts.properties = prop or {} -- The properties of the tileset
- return setmetatable(ts, TileSet)
- end
- ----------------------------------------------------------------------------------------------------
- -- Returns the width in tiles
- function TileSet:tilesWide()
- return ceil( (self.width - self.margin*2 - self.spacing) /
- (self.tileWidth + self.spacing) )
- end
- ----------------------------------------------------------------------------------------------------
- -- Returns the height in tiles
- function TileSet:tilesHigh()
- return ceil( (self.height - self.margin*2 - self.spacing) /
- (self.tileHeight + self.spacing) )
- end
- ----------------------------------------------------------------------------------------------------
- -- Produces tiles from the settings and returns them in a table indexed by their id.
- -- These are cut out left-to-right, top-to-bottom.
- function TileSet:getTiles()
- local x,y = self.margin, self.margin
- local tiles = {}
- local quad = false
- local id = self.firstgid
- local imageWidth, imageHeight = self.image:getWidth(), self.image:getHeight()
- for i = 1, self:tilesHigh() do
- for j = 1, self:tilesWide() do
- quad = love.graphics.newQuad(x, y, self.tileWidth, self.tileHeight, imageWidth, imageHeight)
- tiles[id] = Tile:new(id, self, quad, self.tileWidth, self.tileHeight,
- self.tileProperties[id], self.tileTypes[id])
- x = x + self.tileWidth + self.spacing
- id = id + 1
- end
- x = self.margin
- y = y + self.tileHeight + self.spacing
- end
-
- return tiles
- end
- ----------------------------------------------------------------------------------------------------
- -- Return the TileSet class
- return TileSet
|