sleep.lua 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. local sleep = {}
  2. local controllers = require('app/controllers')
  3. local rattle = require('app/rattle')
  4. local vec3 = require('cpml').vec3
  5. local drawBlock = require('app/block')
  6. sleep.won = false
  7. function sleep:init()
  8. self.skybox = g.newTexture(
  9. 'art/skyboxes/gg_ft.jpg',
  10. 'art/skyboxes/gg_bk.jpg',
  11. 'art/skyboxes/gg_up.jpg',
  12. 'art/skyboxes/gg_dn.jpg',
  13. 'art/skyboxes/gg_rt.jpg',
  14. 'art/skyboxes/gg_lf.jpg'
  15. )
  16. local w, d = lovr.headset.getBoundsDimensions()
  17. self.floor = g.newMesh({{ -w, 0, -d }, { -w, 0, d }, { w, 0, -d }, { w, 0, d }}, 'strip')
  18. self.block = {}
  19. self.block.maxY = 6
  20. self.block.position = vec3(0, self.block.maxY, 0)
  21. self.block.size = .5
  22. self.transitionFactor = 0
  23. rattle:init()
  24. end
  25. function sleep:update(dt)
  26. rattle:update(dt)
  27. -- Logic
  28. if rattle.isShaking then
  29. local x, y, z = lovr.headset.getPosition()
  30. local factor = (1 - _.clamp(y / 2.2, 0, 1)) ^ 2
  31. self.block.position.y = math.max(self.block.position.y - dt * factor * 2, .5)
  32. else
  33. self.block.position.y = math.min(self.block.position.y + dt * .5, self.block.maxY)
  34. end
  35. -- Win
  36. local controller = controllers.list[1]
  37. local trigger = controller and controller:getAxis('trigger')
  38. local dist = controller and vec3(controller:getPosition()):dist(self.block.position)
  39. if controller and trigger > .5 and dist < self.block.size then
  40. self.transitionFactor = math.min(self.transitionFactor + dt, 1)
  41. if self.transitionFactor > 0 then
  42. controller:vibrate(self.transitionFactor^2 * .0035)
  43. end
  44. if self.transitionFactor >= 1 then
  45. self.won = true
  46. local menu = require 'app/menu'
  47. setState(menu)
  48. end
  49. else
  50. self.transitionFactor = math.max(self.transitionFactor - dt, 0)
  51. end
  52. end
  53. function sleep:draw()
  54. local a, rx, ry, rz = lovr.headset.getOrientation()
  55. g.setColor(1, 1, 1)
  56. g.skybox(self.skybox, -a, rx, ry, rz)
  57. rattle:draw()
  58. g.setColor(1, 1, 1, 80 / 255)
  59. self.floor:draw()
  60. local x, y, z = self.block.position:unpack()
  61. g.push()
  62. g.translate(x, y, z)
  63. drawBlock('e')
  64. g.pop()
  65. drawTransition(self.transitionFactor)
  66. end
  67. return sleep