map.lua 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. Map = class()
  2. function Map:init()
  3. self.width = self.width or love.graphics.getWidth()
  4. self.height = self.height or 900
  5. self.obstacles = self.obstacles or {}
  6. self.ground = {}
  7. self.ground.width = self.width
  8. self.ground.height = self.groundHeight or 100
  9. self.ground.body = love.physics.newBody(ctx.world, self.width / 2, self.height - self.ground.height / 2, 'static')
  10. self.ground.shape = love.physics.newRectangleShape(self.width, self.ground.height)
  11. self.ground.fixture = love.physics.newFixture(self.ground.body, self.ground.shape)
  12. self.ground.fixture:setCategory(ctx.categories.ground)
  13. self.ground.body:setUserData(self)
  14. self.clouds = {}
  15. local cloudCount = isa(ctx, Game) and 8 or 2
  16. for i = 1, cloudCount do
  17. table.insert(self.clouds, {
  18. x = love.math.random(100, self.width),
  19. y = love.math.random(0, 450),
  20. height = love.math.random(80, 150),
  21. speed = love.math.random(10, 20),
  22. image = data.media.graphics['cloud' .. love.math.random(1, 3)],
  23. alpha = love.math.random(180, 230)
  24. })
  25. end
  26. self.cloudTimer = 15
  27. if ctx.view then
  28. ctx.view.xmax = self.width
  29. ctx.view.ymax = self.height
  30. ctx.event:emit('view.register', {object = self})
  31. end
  32. end
  33. function Map:update()
  34. self.cloudTimer = timer.rot(self.cloudTimer, function()
  35. self:createCloud()
  36. return love.math.random(10, 25)
  37. end)
  38. local i = #self.clouds
  39. while i >= 1 do
  40. local cloud = self.clouds[i]
  41. if not cloud then break end
  42. cloud.x = cloud.x - cloud.speed * ls.tickrate
  43. if cloud.x < -100 then
  44. table.remove(self.clouds, i)
  45. else
  46. i = i - 1
  47. end
  48. end
  49. table.each(self.obstacles, function(obstacle)
  50. if ((love.keyboard.isDown('down') or (joystick and (joystick:getGamepadAxis('lefty') > .5 or joystick:isGamepadDown('dpdown')))) and ctx.pigeon.downDirty > 0) or ctx.pigeon.body:getY() + ctx.pigeon.shapeSize / 2 > obstacle.body:getY() - obstacle.height / 2 then
  51. obstacle.fixture:setCategory(ctx.categories.oneWayPlatform)
  52. else
  53. obstacle.fixture:setCategory(ctx.categories.ground)
  54. end
  55. end)
  56. end
  57. function Map:draw()
  58. local g = love.graphics
  59. local viewx = ctx.view and ctx.view.x or 0
  60. local pigeonx = ctx.pigeon and ctx.pigeon.body:getX() or 0
  61. local function drawGrass(obstacle)
  62. local x, y = obstacle.body:getPosition()
  63. local padding = 5
  64. local x1, x2 = x - obstacle.width / 2 - padding, x + obstacle.width / 2 + padding
  65. local image = data.media.graphics[ctx.map.name].grassLeft
  66. local scale = 32 / image:getHeight()
  67. local xx = x1 + image:getWidth() * scale
  68. g.setColor(255, 255, 255)
  69. while xx < x2 do
  70. local image = data.media.graphics[ctx.map.name]['grassMid' .. love.math.random(1, 2)]
  71. g.draw(image, math.min(xx, x2 - image:getWidth() * scale * 2), y - obstacle.height / 2 - padding, 0, scale, scale)
  72. xx = xx + image:getWidth() * scale
  73. end
  74. local image = data.media.graphics[ctx.map.name].grassLeft
  75. local scale = 32 / image:getHeight()
  76. g.setColor(255, 255, 255)
  77. g.draw(image, x1, y - obstacle.height / 2 - padding, 0, scale, scale)
  78. local image = data.media.graphics[ctx.map.name].grassRight
  79. g.draw(image, x2, y - obstacle.height / 2 - padding, 0, scale, scale, image:getWidth())
  80. end
  81. -- Fake yellow sky
  82. g.setColor(254, 255, 113)
  83. g.rectangle('fill', 0, 0, self.width, self.height)
  84. g.setColor(255, 255, 255)
  85. local image = data.media.graphics.dinoland.background.sky
  86. local scale = (600) / image:getHeight()
  87. g.draw(image, viewx, self.height, 0, scale, scale, 0, image:getHeight())
  88. local inc = image:getWidth() * scale
  89. for n = 4, 1, -1 do
  90. for x = -500, self.width, inc * 2 do
  91. image = data.media.graphics[ctx.map.name].background.left
  92. g.draw(image, x + pigeonx / 2, self.height, 0, scale, scale, 0, image:getHeight())
  93. image = data.media.graphics[ctx.map.name].background.right
  94. g.draw(image, x + inc + pigeonx / 2, self.height, 0, scale, scale, 0, image:getHeight())
  95. end
  96. end
  97. local groundColor
  98. if ctx.map.name == 'dinoland' then
  99. groundColor = {136, 87, 44}
  100. else
  101. groundColor = {136, 87, 44}
  102. end
  103. g.setColor(groundColor)
  104. physics.draw('fill', self.ground)
  105. love.math.setRandomSeed(1)
  106. drawGrass(self.ground)
  107. for i = 1, #self.obstacles do
  108. local obstacle = self.obstacles[i]
  109. love.math.setRandomSeed(obstacle.body:getX() + obstacle.width)
  110. g.setColor(groundColor)
  111. physics.draw('fill', obstacle)
  112. drawGrass(obstacle)
  113. end
  114. love.math.setRandomSeed(love.timer.getTime())
  115. g.setColor(255, 255, 255)
  116. table.each(self.decorations, function(d)
  117. local scale = d.height / d.image:getHeight()
  118. g.draw(d.image, d.x, d.y + 8, 0, scale * d.direction, scale, d.image:getWidth() / 2, d.image:getHeight())
  119. end)
  120. table.each(self.clouds, function(cloud)
  121. g.setColor(255, 255, 255, cloud.alpha)
  122. local w, h = cloud.image:getDimensions()
  123. local scale = cloud.height / h
  124. g.draw(cloud.image, cloud.x + pigeonx / 4, cloud.y, 0, scale, scale, w / 2, h / 2)
  125. end)
  126. end
  127. function Map:getMaxX()
  128. for i = 1, #self.zones do
  129. if self.zones[i].active then
  130. return self.zones[i].x
  131. end
  132. end
  133. return self.width
  134. end
  135. function Map:createCloud()
  136. table.insert(self.clouds, {
  137. x = self.width + 100,
  138. y = love.math.random(0, 450),
  139. height = love.math.random(80, 150),
  140. speed = love.math.random(10, 20),
  141. image = data.media.graphics['cloud' .. love.math.random(1, 3)],
  142. alpha = love.math.random(180, 230)
  143. })
  144. end