Advertisement
gcgsauce

Untitled

Jul 6th, 2019
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.96 KB | None | 0 0
  1. require "util"
  2. require "constants"
  3.  
  4. Map = Object:extend()
  5.  
  6. function Map:new(map_data) --put in map data to get information about the map (table containing data about the map).
  7.  
  8. if map_data then -- copies all of the data from map_data and makes it a member of the "map" data table
  9. for k, v in pairs(map_data) do
  10. self[k] = v
  11. end
  12. end
  13. self.image = love.graphics.newImage(self.tilesets[1].image:sub(4))
  14. self.camX = 0 --the coordinates of the current view of the screen relative to the map. this is the "camera"
  15. self.camY = 0 --move camera only 1 pixel per second
  16.  
  17. input:bind('right', 'PAN_RIGHT')
  18. input:bind('left', 'PAN_LEFT')
  19. input:bind('down', 'PAN_DOWN')
  20. input:bind('up', 'PAN_UP')
  21. end
  22.  
  23. function Map:update(dt) --moves the map 1 pixel in any direction
  24. if input:down('PAN_RIGHT') then
  25. self.camX = self.camX + 2
  26. print(self.camX)
  27. elseif input:down('PAN_LEFT') then
  28. self.camX = self.camX - 2
  29. end
  30.  
  31. if input:down('PAN_UP') then
  32. self.camY = self.camY - 2
  33. elseif input:down('PAN_DOWN') then
  34. self.camY = self.camY + 2
  35. end
  36. end
  37.  
  38. function Map:getTileQuad(x, y) -- x, y is the coordinates in tiles, return the number of the quad associated with this tile
  39. return self.layers[1].data[self.width*(y-1) + x] -- account for array starting at 1
  40. end
  41.  
  42. function Map:getTileCoordinates(x, y) --x,y is the coordinate in tiles, return the coordinates along the x and y axis of the screen
  43. return self.tilewidth*(x-1), self.tileheight*(y-1)
  44. end
  45.  
  46. function Map:getTilePoint(x, y) --from coords of tile on x,y axis get the point in terms of number of tiles across each axis
  47. local xCoords = nil
  48. local yCoords = nil
  49.  
  50. if x/self.tilewidth == self.width then xCoords = x/self.tilewidth else xCoords = math.floor(x/self.tilewidth)+1 end
  51. if y/self.tileheight == self.height then yCoords = y/self.tileheight else yCoords = math.floor(y/self.tileheight)+1 end
  52. return xCoords, yCoords
  53. end
  54.  
  55. function Map:draw()
  56.  
  57. --get the position in terms of tiles with respect to the whole map of the top left portion of the camera, then render a full screen from there
  58.  
  59. local xTop, yTop = self:getTilePoint(self.camX, self.camY)
  60. local xBot, yBot = self:getTilePoint(self.camX + gw, self.camY + gh)
  61. local xCoord, yCoord = self:getTileCoordinates(xTop, yTop)
  62. local xDiff = self.camX - xCoord --the coordinates may start rendering the top left tile from the middle
  63. local yDiff = self.camY - yCoord
  64.  
  65. --only render the map that can be seen from the viewport, not the entire map
  66.  
  67. for y = yTop, yBot do
  68. for x = xTop, xBot do
  69. local a, b, c, d = unpack(self.quads[self:getTileQuad(x, y)])
  70. local quad = love.graphics.newQuad(a, b, c, d, self.image:getDimensions())
  71. local z, v = self:getTileCoordinates(x-xTop+1, y-yTop+1)
  72. love.graphics.draw(self.image, quad, z - xDiff, v - yDiff)
  73. end
  74. end
  75. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement