Advertisement
joseleeph

Untitled

Feb 4th, 2021
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.92 KB | None | 0 0
  1. require 'Util'
  2. require 'Player'
  3. Map = Class{}
  4.  
  5. TILE_BRICK = 1
  6. --TILE_EMPTY = -1
  7. TILE_EMPTY = 4
  8.  
  9. CLOUD_LEFT = 6
  10. CLOUD_RIGHT = 7
  11.  
  12. BUSH_LEFT = 2
  13. BUSH_RIGHT = 3
  14.  
  15. MUSHROOM_TOP = 10
  16. MUSHROOM_BOTTOM = 11
  17.  
  18. JUMP_BLOCK = 5
  19.  
  20.  
  21. local SCROLL_SPEED = 62
  22.  
  23. function Map:init()
  24. self.spritesheet = love.graphics.newImage('graphics/spritesheet.png')
  25. self.tileSprites = generateQuads(self.spritesheet, 16, 16)
  26.  
  27. self.tileWidth = 16
  28. self.tileHeight = 16
  29. self.mapWidth = 30
  30. self.mapHeight = 28
  31.  
  32. self.tiles = {}
  33.  
  34. self.player = Player(self)
  35.  
  36. self.camX = 0
  37. self.camY = -3
  38.  
  39.  
  40. self.mapWidthPixels = self.mapWidth * self.tileWidth
  41. self.mapHeightPixels = self.mapHeight * self.tileHeight
  42.  
  43. for y = 1, self.mapHeight do
  44. for x = 1, self.mapWidth do
  45.  
  46. self:setTile(x, y, TILE_EMPTY)
  47. end
  48. end
  49.  
  50.  
  51.  
  52. local x = 1
  53. while x < self.mapWidth do
  54.  
  55.  
  56.  
  57. if x < self.mapWidth - 2 then
  58. if math.random(20) == 1 then
  59.  
  60.  
  61. local cloudStart = math.random(self.mapHeight / 2 - 6)
  62.  
  63. self:setTile(x, cloudStart, CLOUD_LEFT)
  64. self:setTile(x+1, cloudStart, CLOUD_RIGHT)
  65. end
  66. end
  67.  
  68.  
  69. if math.random(20) == 1 then
  70.  
  71. self:setTile(x, self.mapHeight / 2 - 2, MUSHROOM_TOP)
  72. self:setTile(x, self.mapHeight / 2 - 1, MUSHROOM_BOTTOM)
  73.  
  74.  
  75. for y = self.mapHeight / 2, self.mapHeight do
  76. self:setTile(x, y, TILE_BRICK)
  77. end
  78.  
  79. x = x + 1
  80.  
  81.  
  82. elseif math.random(10) == 1 and x < self.mapWidth - 3 then
  83. local bushLevel = self.mapHeight / 2 - 1
  84.  
  85.  
  86. self:setTile(x, bushLevel, BUSH_LEFT)
  87. for y = self.mapHeight / 2, self.mapHeight do
  88. self:setTile(x, y, TILE_BRICK)
  89. end
  90. x = x + 1
  91.  
  92. self:setTile(x, bushLevel, BUSH_RIGHT)
  93. for y = self.mapHeight / 2, self.mapHeight do
  94. self:setTile(x, y, TILE_BRICK)
  95. end
  96. x = x + 1
  97. elseif math.random(10) ~= 1 then -- ~=?
  98.  
  99.  
  100. for y = self.mapHeight / 2, self.mapHeight do
  101. self:setTile(x, y, TILE_BRICK)
  102. end
  103.  
  104.  
  105. if math.random(15) == 1 then
  106. self:setTile(x, self.mapHeight / 2 - 4, JUMP_BLOCK)
  107. end
  108.  
  109.  
  110. x = x + 1
  111. else
  112.  
  113. x = x + 2
  114. end
  115. end
  116. end
  117.  
  118.  
  119. function Map:setTile(x, y, tile)
  120. self.tiles[(y-1) * self.mapWidth + x] = tile
  121. end
  122.  
  123. function Map:getTile(x, y)
  124. return self.tiles[(y-1)*self.mapWidth + x]
  125. end
  126.  
  127. function Map:update(dt)
  128.  
  129. if love.keyboard.isDown('up') then
  130. self.camY = math.max(0, math.floor(self.camY + dt * -SCROLL_SPEED))
  131.  
  132. elseif love.keyboard.isDown('left') then
  133. self.camX = math.max(0, math.floor(self.camX + dt * -SCROLL_SPEED))
  134. elseif love.keyboard.isDown('down') then
  135. self.camY = math.min(self.mapHeightPixels - VIRTUAL_HEIGHT, math.floor(self.camY + dt * SCROLL_SPEED))
  136. elseif love.keyboard.isDown('right') then
  137. self.camX = math.min(self.mapWidthPixels - VIRTUAL_WIDTH, math.floor(self.camX + dt * SCROLL_SPEED))
  138.  
  139. end
  140.  
  141. self.player:update(dt)
  142. end
  143.  
  144. function Map:render()
  145. for y = 1, self.mapHeight do
  146. for x = 1, self.mapWidth do
  147. local tile = self.getTile(x, y)
  148. if tile ~= TILE_EMPTY then
  149. love.graphics.draw(self.spritesheet, self.sprites[tile]) -- his head is in the way and this was done between videos. is there a place where
  150. --love.graphics.draw(self.spritesheet, self.tileSprites[self:getTile(x, y)], (x-1) * self.tileWidth, (y - 1) * self.tileHeight)
  151. end
  152. end
  153. end
  154.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement