rattle.lua 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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. function rattle:init()
  7. self.lastPosition = nil
  8. self.lastVelocity = nil
  9. self.shake = 0
  10. end
  11. function rattle:update(dt)
  12. local controller = controllers.list[1]
  13. if controller then
  14. local pos = vec3(controller:getPosition())
  15. if self.lastPosition then
  16. local velocity = pos - self.lastPosition
  17. if self.lastVelocity then
  18. local acceleration = (velocity - self.lastVelocity):len()
  19. self.shake = _.lerp(self.shake, acceleration, math.min(16 * dt, 1))
  20. self.isShaking = self.shake > .006
  21. if self.isShaking then
  22. controller:vibrate(math.min((self.shake - .006) / 4, .0035))
  23. end
  24. end
  25. self.lastVelocity = velocity
  26. end
  27. self.lastPosition = pos
  28. end
  29. end
  30. function rattle:draw()
  31. local controller = controllers.list[1]
  32. if controller then
  33. local x, y, z = controller:getPosition()
  34. local angle, ax, ay, az = controller:getOrientation()
  35. lovr.graphics.setColor(1, 1, 1)
  36. self.model:draw(x, y, z, .01 + self.shake * .025, angle, ax, ay, az)
  37. end
  38. end
  39. return rattle