rattle.lua 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. local rattle = {}
  2. local controllers = require 'app/controllers'
  3. local vec3 = require('cpml').vec3
  4. rattle.model = lovr.graphics.newModel('art/rattle.obj')
  5. rattle.model:setMaterial(lovr.graphics.newMaterial('art/rattle_DIFF.png'))
  6. rattle.keyblade = lovr.graphics.newModel('art/keyblade.obj')
  7. function rattle:init()
  8. self.lastPosition = nil
  9. self.lastVelocity = nil
  10. self.shake = 0
  11. end
  12. function rattle:update(dt)
  13. local controller = controllers.list[1]
  14. local levels = { require('app/cry'), require('app/sleep'), require('app/play') }
  15. if controller and not _.all(levels, 'won') then
  16. local pos = vec3(controller:getPosition())
  17. if self.lastPosition then
  18. local velocity = pos - self.lastPosition
  19. if self.lastVelocity then
  20. local acceleration = (velocity - self.lastVelocity):len()
  21. self.shake = _.lerp(self.shake, acceleration, math.min(16 * dt, 1))
  22. self.isShaking = self.shake > .006
  23. if self.isShaking then
  24. controller:vibrate(math.min((self.shake - .006) / 4, .0035))
  25. end
  26. end
  27. self.lastVelocity = velocity
  28. end
  29. self.lastPosition = pos
  30. end
  31. end
  32. function rattle:draw()
  33. local controller = controllers.list[1]
  34. local levels = { require('app/cry'), require('app/sleep'), require('app/play') }
  35. if controller then
  36. local x, y, z = controller:getPosition()
  37. local angle, ax, ay, az = controller:getOrientation()
  38. lovr.graphics.setColor(1, 1, 1)
  39. if _.all(levels, 'won') then
  40. -- TODO Draw keyblade
  41. self.keyblade:draw(x, y, z, .75, angle, ax, ay, az)
  42. else
  43. self.model:draw(x, y, z, .01 + self.shake * .025, angle, ax, ay, az)
  44. end
  45. end
  46. end
  47. return rattle