Advertisement
joseleeph

Untitled

Jan 4th, 2021
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.09 KB | None | 0 0
  1. Map = Class{} -- error: attempt to class global 'Class' (a boolean value)
  2.  
  3. TILE_BRICK = 1
  4. TILE_EMPTY = 4
  5.  
  6. function Map:init()
  7. self.spritesheet = love.graphics.newImage('graphics/spritesheet.png') -- creating the map. we need a spritesheet to reference to map the tiles to quads. the function will point to a texture file and load it into memory. it is a texture object.
  8. self.tileWidth = 16
  9. self.tileheight = 16
  10. self.mapWidth = 30 -- tilewidth is just pixel size. the map width is how many tiles wide the map is
  11. self.mapHeight = 28
  12.  
  13. self.tiles = {} -- the actual tile map... we will iterate over these number, and draw the right texture with the right number down the line
  14.  
  15. self.tileSprites = generateQuads(self.spritesheet, self.tileWidth, self.tileHeight)
  16.  
  17. -- populate the tile map with the numbers. 1 is the brick that we want to use, and 4 is an empty tile which will represent the sky.
  18.  
  19. -- filling the map with empty tiles
  20. for y = 1, self.mapHeight/2 do -- iterate top to bottom through the map and go down to halfway through the map
  21. for x = 1, self.mapWidth do -- columns and rows
  22. self:setTile(x, y, TILE_EMPTY) -- this function is writted below
  23. end
  24. end
  25.  
  26. -- starts halfway down the map, populates with bricks
  27. for y = self.mapHeight/2, self.mapHeight do
  28. --start halfway down the map
  29. for x = 1, self.mapWidth/2 do
  30. self:setTile(x, y, TILE_BRICK) -- starts halfway down the map and populates it with bricks
  31. end
  32. end
  33. end
  34.  
  35. function Map:SetTile(x, y, tile) -- the tiles are numbers... a simple way to represent them
  36. self.tiles[(y-1)*self.mapWidth + x] = tile
  37. end
  38.  
  39.  
  40. function Map:getTile(x, y)
  41. return self.tiles[(y-1)*self.mapWidth + x] -- return the same value
  42. end
  43.  
  44. function Map:update(dt)
  45.  
  46. end
  47.  
  48. function Map:render()
  49. for y = 1, self.mapHeight do
  50. for x = 1, self.mapWidth do
  51. love.graphics.draw(self.spritesheet, self.tileSprites[self:getTile(x, y)],
  52. (x-1)*self.tileWidth, (y-1)*self.tileHeight)
  53. end
  54. end
  55. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement