menuchoose.lua 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. local g = love.graphics
  2. local function lerpAnimation(code, key, val)
  3. ctx.prevAnimationTransforms[code][key] = ctx.animationTransforms[code][key]
  4. ctx.animationTransforms[code][key] = math.lerp(ctx.animationTransforms[code][key] or val, val, math.min(10 * ls.tickrate, 1))
  5. end
  6. MenuChoose = class()
  7. function MenuChoose:init()
  8. self.geometry = setmetatable({}, {__index = function(t, k)
  9. return rawset(t, k, self.geometryFunctions[k]())[k]
  10. end})
  11. self.geometryFunctions = {
  12. colors = function()
  13. local u, v = ctx.u, ctx.v
  14. local ct = #config.player.colorOrder
  15. local width = .08 * v * 1.3
  16. local inc = width + .02 * v
  17. local x = u * .5 - (inc * (ct - 1) / 2)
  18. local res = {}
  19. for name, color in pairs(config.player.colors) do
  20. table.insert(res, {x - width / 2, v * .55, width, .08 * v})
  21. x = x + inc
  22. end
  23. return res
  24. end,
  25. back = function()
  26. local u, v = ctx.u, ctx.v
  27. return {u * .125, v * .715, u * .2, v * .1}
  28. end,
  29. next = function()
  30. local u, v = ctx.u, ctx.v
  31. return {u * .675, v * .715, u * .2, v * .1}
  32. end
  33. }
  34. self.active = false
  35. -- Initialize user
  36. self.user = table.copy(config.defaultUser)
  37. self.back = ctx.gooey:add(Button, 'menu.choose.back')
  38. self.back.geometry = function() return self.geometry.back end
  39. self.back:on('click', function() ctx:setPage('select', self.destination) end)
  40. self.back.text = 'Back'
  41. self.next = ctx.gooey:add(Button, 'menu.choose.next')
  42. self.next.geometry = function() return self.geometry.next end
  43. self.next:on('click', function() self:finished() end)
  44. self.next.text = 'Ready!'
  45. end
  46. function MenuChoose:activate(destination)
  47. self.destination = destination
  48. end
  49. function MenuChoose:update()
  50. self.active = ctx.page == 'choose'
  51. --[[if self.active then
  52. local mx, my = love.mouse.getPosition()
  53. local minions = self.geometry.minions
  54. for i = 1, #minions do
  55. local code = config.starters[i]
  56. if i == self.selectedMinion then
  57. lerpAnimation(code, 'scale', 1.25)
  58. else
  59. lerpAnimation(code, 'scale', .9)
  60. end
  61. end
  62. end]]
  63. end
  64. function MenuChoose:draw()
  65. if not self.active then return end
  66. local u, v = ctx.u, ctx.v
  67. g.setFont('mesmerize', .04 * v)
  68. g.setColor(0, 0, 0, 100)
  69. g.rectangle('fill', u * .125, v * .23, u * .75, v * .46)
  70. if #self.user.name == 0 then g.setColor(255, 0, 0)
  71. else g.setColor(255, 255, 255) end
  72. g.printShadow('Enter your name', u * .5, v * .31, true)
  73. g.setFont('mesmerize', .06 * v)
  74. g.setColor(255, 255, 255)
  75. g.printShadow(self.user.name, u * .5, v * .38, true)
  76. local fontHeight = g.getFont():getHeight()
  77. local lineX = u * .5 + g.getFont():getWidth(self.user.name) / 2 + 1
  78. g.line(lineX, v * .38 - fontHeight / 2, lineX, v * .38 + fontHeight / 2)
  79. g.printShadow('Pick your color', u * .5, v * .5, true)
  80. local colors = self.geometry.colors
  81. for i = 1, #colors do
  82. local name = config.player.colorOrder[i]
  83. local color = config.player.colors[name]
  84. g.setColor(color[1] * 255, color[2] * 255, color[3] * 255)
  85. g.rectangle('fill', unpack(colors[i]))
  86. if self.user.color == name then
  87. g.setColor(255, 255, 255)
  88. g.rectangle('line', unpack(colors[i]))
  89. end
  90. end
  91. g.setColor(255, 255, 255)
  92. ctx.animations.muju:draw(u * .2, v * .5)
  93. local color = self.user and self.user.color or 'purple'
  94. for _, slot in pairs({'robebottom', 'torso', 'front_upper_arm', 'rear_upper_arm', 'front_bracer', 'rear_bracer'}) do
  95. local slot = ctx.animations.muju.spine.skeleton:findSlot(slot)
  96. slot.r, slot.g, slot.b = unpack(config.player.colors[color])
  97. end
  98. self.back:draw()
  99. self.next:draw()
  100. end
  101. function MenuChoose:keypressed(key)
  102. if not self.active then return end
  103. if key == 'backspace' then
  104. self.user.name = self.user.name:sub(1, -2)
  105. ctx.sound:play('juju1', function(sound) sound:setPitch(.6) end)
  106. end
  107. end
  108. function MenuChoose:keyreleased(key)
  109. if not self.active then return end
  110. if key == 'escape' then
  111. ctx:setPage('select', self.destination)
  112. end
  113. return self.active
  114. end
  115. function MenuChoose:mousepressed(mx, my, b)
  116. if not self.active then return end
  117. if b == 'l' then
  118. local colors = self.geometry.colors
  119. for i = 1, #colors do
  120. if self.user.color ~= config.player.colorOrder[i] and math.inside(mx, my, unpack(colors[i])) then
  121. self.user.color = config.player.colorOrder[i]
  122. ctx.sound:play('juju1', function(sound) sound:setPitch(.75) end)
  123. end
  124. end
  125. end
  126. end
  127. function MenuChoose:textinput(char)
  128. if not self.active then return end
  129. if #self.user.name < 16 and char:match('%a*%d*') then
  130. self.user.name = self.user.name .. char
  131. ctx.sound:play('juju1', function(sound) sound:setPitch(.6) end)
  132. end
  133. end
  134. function MenuChoose:resize()
  135. table.clear(self.geometry)
  136. end
  137. function MenuChoose:finished()
  138. if #self.user.name == 0 then return end
  139. saveUser(self.user)
  140. ctx.user = self.user
  141. ctx:startGame({mode = 'campaign', biome = 'forest', tutorial = true, destination = self.destination})
  142. end