play.lua 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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/bluecloud_ft.jpg',
  10. 'art/skyboxes/bluecloud_bk.jpg',
  11. 'art/skyboxes/bluecloud_up.jpg',
  12. 'art/skyboxes/bluecloud_dn.jpg',
  13. 'art/skyboxes/bluecloud_rt.jpg',
  14. 'art/skyboxes/bluecloud_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.blocks = {}
  19. for i = 1, 4 do
  20. local position
  21. if i == 1 then
  22. position = { -1, 1.5, 0 }
  23. elseif i == 2 then
  24. position = { 1, 1.5, 0 }
  25. elseif i == 3 then
  26. position = { 0, 1.5, -1 }
  27. else
  28. position = { 0, 1.5, 1 }
  29. end
  30. local block = {}
  31. block.maxY = 6
  32. block.position = vec3(unpack(position))
  33. block.size = .5
  34. block.angle = 1
  35. block.win = i == 4
  36. self.blocks[i] = block
  37. end
  38. self.transitionFactor = 0
  39. rattle:init()
  40. end
  41. function sleep:update(dt)
  42. rattle:update(dt)
  43. -- Logic
  44. if rattle.isShaking then
  45. _.each(self.blocks, function(block)
  46. block.angle = block.angle + dt
  47. end)
  48. end
  49. -- Win
  50. local controller = controllers.list[1]
  51. local trigger = controller and controller:getAxis('trigger')
  52. local isWinning = false
  53. _.each(self.blocks, function(block)
  54. local dist = controller and vec3(controller:getPosition()):dist(block.position)
  55. if controller and trigger > .5 and block.win and dist < .5 then
  56. isWinning = true
  57. end
  58. end)
  59. if controller and isWinning then
  60. self.transitionFactor = math.min(self.transitionFactor + dt, 1)
  61. if self.transitionFactor > 0 then
  62. controller:vibrate(self.transitionFactor^2 * .0035)
  63. end
  64. if self.transitionFactor >= 1 then
  65. self.won = true
  66. local menu = require 'app/menu'
  67. setState(menu)
  68. end
  69. else
  70. self.transitionFactor = math.max(self.transitionFactor - dt, 0)
  71. end
  72. end
  73. function sleep:draw()
  74. local a, rx, ry, rz = lovr.headset.getOrientation()
  75. g.setColor(1, 1, 1)
  76. g.skybox(self.skybox, -a, rx, ry, rz)
  77. rattle:draw()
  78. g.setColor(1, 1, 1, 80 / 255)
  79. self.floor:draw()
  80. _.each(self.blocks, function(block)
  81. local x, y, z = block.position:unpack()
  82. g.push()
  83. g.translate(x, y, z)
  84. g.rotate(block.angle, 0, 1, 0)
  85. drawBlock('y')
  86. g.pop()
  87. end)
  88. drawTransition(self.transitionFactor)
  89. end
  90. return sleep