juju.lua 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. local juju = lib.object.create():include(lib.entity)
  2. juju.config = {
  3. radius = 15,
  4. gravity = 1000,
  5. maxBounces = 2,
  6. shape = 'circle'
  7. }
  8. function juju:init()
  9. self.position = {
  10. x = nil,
  11. y = nil
  12. }
  13. self.velocity = {
  14. x = love.math.randomNormal(40),
  15. y = love.math.randomNormal(40),
  16. z = -500
  17. }
  18. self.bounces = 0
  19. end
  20. function juju:bind()
  21. self.position.z = -1
  22. return {
  23. love.update
  24. :filter(function()
  25. return self.velocity.z == 0
  26. end)
  27. :subscribe(function()
  28. local closest = self:closest('minion')
  29. if closest and self:distanceTo(closest) <= self.config.radius + closest.config.radius then
  30. local muju = app.context.objects.muju
  31. muju.juju = muju.juju + 1
  32. self:unbind()
  33. app.context:removeObject(self)
  34. end
  35. end),
  36. love.update
  37. :subscribe(function()
  38. self.position.x = self.position.x + self.velocity.x * lib.tick.rate
  39. self.position.y = self.position.y + self.velocity.y * lib.tick.rate
  40. self.position.z = self.position.z + self.velocity.z * lib.tick.rate
  41. if self.position.z < 0 then
  42. self.velocity.z = self.velocity.z + self.config.gravity * lib.tick.rate
  43. end
  44. if self.velocity.z == 0 then
  45. self.velocity.x = util.lerp(self.velocity.x, 0, lib.tick.getLerpFactor(.1))
  46. self.velocity.y = util.lerp(self.velocity.y, 0, lib.tick.getLerpFactor(.1))
  47. end
  48. if self.position.z > 0 then
  49. self.velocity.x = self.velocity.x * .7
  50. self.velocity.y = self.velocity.y * .7
  51. if self.bounces < self.config.maxBounces then
  52. self.bounces = self.bounces + 1
  53. self.velocity.z = math.abs(self.velocity.z) * -.5
  54. self.position.z = -1
  55. else
  56. self.velocity.z = 0
  57. self.position.z = 0
  58. end
  59. end
  60. if self:isEscaped() then
  61. if self.position.y < 0 or self.position.y + self.config.radius > app.context.scene.height then
  62. self.velocity.y = -self.velocity.y
  63. else
  64. self.velocity.x = -self.velocity.x
  65. end
  66. end
  67. end),
  68. app.context.view.draw:subscribe(self:wrap(self.draw))
  69. }
  70. end
  71. function juju:draw()
  72. local image = app.art.shadow
  73. local scale = 80 / image:getWidth()
  74. g.white(50)
  75. g.draw(image, self.position.x, self.position.y, 0, scale, scale / 2, image:getWidth() / 2, image:getHeight() / 2)
  76. self:drawRing(80, 200, 80)
  77. local image = app.art.juju
  78. local scale = (self.config.radius * 2) / image:getWidth()
  79. local angle = math.sin((lib.tick.index + lib.tick.accum) / 20) / 5
  80. local offset = image:getHeight() * scale / 2
  81. g.white()
  82. g.draw(image, self.position.x, self.position.y - offset + self.position.z / 2, angle, scale, scale, image:getWidth() / 2, image:getHeight() / 2)
  83. return -self.position.y - 1
  84. end
  85. return juju