hud.lua 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. local Hud = class()
  2. Hud.mouseSize = 30
  3. function Hud:init()
  4. local s = self.mouseSize
  5. self.mouseCurve = love.math.newBezierCurve(
  6. s, 1,
  7. s, -s,
  8. 1, -s
  9. )
  10. self.smallFont = love.graphics.newFont('font/pfArmaFive.ttf', 16)
  11. self.bigFont = love.graphics.newFont('font/pfArmaFive.ttf', 32)
  12. self.pointer = love.mouse.getSystemCursor('hand')
  13. self.tutorial = firstGame
  14. self.dead = false
  15. self.tutorialFactor = 0
  16. self.deadFactor = 0
  17. self.mx, self.my = 0, 0
  18. self.buttonGeometry = {tutorial = {}, dead = {}}
  19. self.buttonActive = {tutorial = {}, dead = {}}
  20. end
  21. function Hud:update(dt)
  22. local buttonKey = self.tutorial and 'tutorial' or (self.dead and 'dead' or nil)
  23. if self.tutorial then
  24. local sin, cos = math.sin(time * 3), math.cos(time * 3)
  25. local rate = 5
  26. self.mx = math.lerp(self.mx, math.sign(cos) * 10, math.min(rate * dt, 1))
  27. self.my = math.lerp(self.my, math.sign(sin) * 10, math.min(rate * dt, 1))
  28. end
  29. local hover = false
  30. for key, geo in pairs(self.buttonGeometry[buttonKey] or {}) do
  31. if math.inside(love.mouse.getX(), love.mouse.getY(), unpack(geo)) then
  32. hover = true
  33. love.mouse.setCursor(self.pointer)
  34. self.buttonActive[buttonKey][key] = love.mouse.isDown('l')
  35. break
  36. else
  37. self.buttonActive[buttonKey][key] = false
  38. end
  39. end
  40. if not hover then love.mouse.setCursor() end
  41. self.tutorialFactor = math.lerp(self.tutorialFactor, self.tutorial and 1 or 0, math.min(10 * dt, 1))
  42. self.deadFactor = math.lerp(self.deadFactor, self.dead and 1 or 0, math.min(10 * dt, 1))
  43. end
  44. function Hud:draw()
  45. if self.tutorialFactor > .01 then
  46. local y = 200 - g.getHeight() * (1 - self.tutorialFactor)
  47. self:drawMouse(200 + self.mx, y + self.my, 0, 0, true)
  48. self:drawMouse(400, y, math.abs(math.cos(time * 1.5)), math.abs(math.sin(time * 1.5)))
  49. self:drawFakeJellyfish(600, y)
  50. for x = 600 - 30, 600 + 30, 20 do
  51. local y = y + 10
  52. if x == 600 - 30 or x == 600 + 30 then
  53. y = y + 5
  54. end
  55. g.setLineWidth(2)
  56. g.setColor(255, 80, 80)
  57. g.circle('line', x, y + 50, 5, 20)
  58. end
  59. g.setColor(255, 255, 255)
  60. g.setFont(self.smallFont)
  61. g.printf('Steer with mouse', 200 - 80, y + 100, 160, 'center')
  62. g.printf('Alternate mouse buttons to move', 400 - 80, y + 100, 160, 'center')
  63. g.printf('Pop bubbles with tips of tentacles', 600 - 80, y + 100, 160, 'center')
  64. self:drawButton('Play', g.getWidth() / 2 - self.smallFont:getWidth('Play') / 2, y + 220, 'tutorial')
  65. g.printf('Don\'t let bubbles float away!', 0, y + 320, g.getWidth(), 'center')
  66. g.setFont(self.bigFont)
  67. g.printf('Jelly Pop', 0, y - 140, g.getWidth(), 'center')
  68. end
  69. if self.deadFactor > .01 then
  70. local x = 220
  71. local y = 300 - g.getHeight() * (1 - self.deadFactor) - self.smallFont:getHeight() / 2
  72. self:drawFakeJellyfish(x, y)
  73. g.setColor(200, 200, 200)
  74. g.setLineWidth(3)
  75. g.line(x - 24, y - 16, x - 16, y - 8)
  76. g.line(x - 24, y - 8, x - 16, y - 16)
  77. g.line(x + 16, y - 16, x + 24, y - 8)
  78. g.line(x + 16, y - 8, x + 24, y - 16)
  79. g.setColor(255, 255, 255)
  80. g.setFont(self.smallFont)
  81. g.print('Game over', x + 100, y - 50)
  82. g.print('You popped ' .. bubbles.popped .. ' bubbles', x + 100, y - 10)
  83. self:drawButton('Restart', x + 100, y + 40, 'dead')
  84. self:drawButton('Quit', x + 200, y + 40, 'dead')
  85. end
  86. end
  87. function Hud:mousereleased(x, y, b)
  88. if self.tutorial then
  89. if math.inside(x, y, unpack(self.buttonGeometry.tutorial.Play)) then
  90. self.tutorial = false
  91. end
  92. elseif self.dead then
  93. if math.inside(x, y, unpack(self.buttonGeometry.dead.Restart)) then
  94. if soundscape and soundscape:isPlaying() then soundscape:stop() end
  95. love.load()
  96. hud.deadFactor = 1
  97. love.mouse.setCursor()
  98. elseif math.inside(x, y, unpack(self.buttonGeometry.dead.Quit)) then
  99. love.event.quit()
  100. end
  101. end
  102. end
  103. function Hud:drawMouse(x, y, l, r, c)
  104. local s = self.mouseSize
  105. self.mouseCurve:translate(x, y)
  106. local points = self.mouseCurve:render(5)
  107. g.setLineWidth(3)
  108. local leftButtonPoints = table.copy(points)
  109. local rightButtonPoints = {}
  110. for i = #points, 1, -2 do
  111. local x, y = 2 * x - points[i - 1], points[i]
  112. table.insert(rightButtonPoints, x)
  113. table.insert(rightButtonPoints, y)
  114. table.insert(points, x)
  115. table.insert(points, y)
  116. end
  117. -- Woah dude
  118. for i = 1, #points, 2 do
  119. table.insert(points, 2 * x - points[i])
  120. table.insert(points, s * 1.35 + 2 * y - points[i + 1])
  121. end
  122. table.insert(leftButtonPoints, x)
  123. table.insert(leftButtonPoints, y)
  124. table.insert(rightButtonPoints, x)
  125. table.insert(rightButtonPoints, y)
  126. g.setColor(255, 80, 80, 255 * math.clamp(l or 0, 0, 1))
  127. g.polygon('fill', leftButtonPoints)
  128. g.setColor(255, 80, 80, 255 * math.clamp(r or 0, 0, 1))
  129. g.polygon('fill', rightButtonPoints)
  130. g.setColor(200, 200, 200)
  131. g.polygon('line', points)
  132. g.line(x - s, y, x + s, y)
  133. g.line(x, y - s, x, y)
  134. if c then
  135. g.setColor(255, 80, 80)
  136. g.setPointSize(10)
  137. g.point(x, y + (s + s * 1.35) / 2)
  138. local factor = (time * 1.5 / math.pi) % 1
  139. g.setPointSize(10 + 20 * factor)
  140. g.setColor(255, 80, 80, 255 * (1 - factor))
  141. g.point(x, y + (s + s * 1.35) / 2)
  142. g.setPointSize(1)
  143. end
  144. g.setLineWidth(1)
  145. self.mouseCurve:translate(-x, -y)
  146. end
  147. function Hud:drawButton(text, ox, oy, group)
  148. local w, h = self.smallFont:getWidth(text) + 16, self.smallFont:getHeight() + 16
  149. g.setLineWidth(2)
  150. g.setColor(70, 70, 90)
  151. g.rectangle('fill', ox - 10, oy - 6, w, h)
  152. g.setColor(0, 0, 0)
  153. if self.buttonActive[group][text] then
  154. g.line(ox - 10, oy - 6, ox - 10, oy - 6 + h)
  155. g.line(ox - 10, oy - 6, ox - 10 + w, oy - 6)
  156. else
  157. g.line(ox - 10, oy - 6 + h, ox - 10 + w, oy - 6 + h)
  158. g.line(ox - 10 + w, oy - 6, ox - 10 + w, oy - 6 + h)
  159. end
  160. g.setColor(255, 255, 255)
  161. g.print(text, ox, oy)
  162. self.buttonGeometry[group][text] = {ox - 10, oy - 6, w, h}
  163. end
  164. function Hud:drawFakeJellyfish(x, y)
  165. g.setColor(jellyfish.color)
  166. g.setLineWidth(4)
  167. for xx = x - 30, x + 30, 20 do
  168. local y = y + 10
  169. if xx == x - 30 or xx == x + 30 then
  170. y = y + 5
  171. end
  172. g.line(xx, y, xx, y + 50)
  173. end
  174. g.push()
  175. g.translate(x - jellyfish.x, y - 10 - jellyfish.y)
  176. jellyfish.direction = -math.pi / 2
  177. jellyfish:draw(true)
  178. g.pop()
  179. end
  180. return Hud