Advertisement
alestane

Including Construction

Jan 1st, 2012
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.47 KB | None | 0 0
  1. local sprite = require "sprite"
  2. local loadMap = require "loadMap"
  3. --[[
  4. Loads a map layer with sprite data from a text file
  5. name is the file name to load from
  6. xOrigin and yOrigin describe the square in the map data to place in 1, 1 of the display field
  7. --]]
  8. local function loadMapFile(self, name, xOrigin, yOrigin)
  9.     self.data = loadMap(name, self.terrain.mapping)
  10. end
  11.  
  12. local function createMap(self, size, columns, rows)
  13.     local map = display.newGroup()
  14.     map.terrain = self
  15.     map.tiles = {}
  16.     for row = 0, rows + 1 do
  17.         map.tiles[row] = {}
  18.         for column=0, columns + 1 do
  19.             local tile = sprite.newSprite(self)
  20.             tile:setReferencePoint(display.BottomLeftReferencePoint)
  21.             map:insert(tile)
  22.             tile.xScale, tile.yScale = size / tile.width, size / tile.height
  23.             tile.x, tile.y = column * size, row * size
  24.             map.tiles[row][column] = tile
  25.         end
  26.     end
  27.     map.load = loadMapFile
  28.     return map
  29. end
  30.  
  31. return function(spritesheet, size, kinds, mapping)
  32.     if not mapping then
  33.         mapping = {}
  34.         for _, kind in ipairs(kinds) do
  35.             mapping[kind:sub(1, 1):upper()] = kind
  36.         end
  37.     end
  38.     local terrain = sprite.newSpriteSet(sprite.newSpriteSheet(spritesheet,  size, size), 1, 1)
  39.     for i, kind in ipairs(kinds) do
  40.         sprite.add(terrain, kind, i + 1, 1, 1, 1)
  41.     end
  42.     terrain.create = createMap
  43.     terrain.mapping = mapping
  44.     return terrain
  45. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement