cry.lua 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. local cry = {}
  2. local controllers = require 'app/controllers'
  3. local rattle = require 'app/rattle'
  4. local vec3 = require('cpml').vec3
  5. local quat = require('cpml').quat
  6. local drawBlock = require('app/block')
  7. cry.won = false
  8. function cry:init()
  9. self.skybox = g.newTexture(
  10. 'art/skyboxes/sea_ft.jpg',
  11. 'art/skyboxes/sea_bk.jpg',
  12. 'art/skyboxes/sea_up.jpg',
  13. 'art/skyboxes/sea_dn.jpg',
  14. 'art/skyboxes/sea_rt.jpg',
  15. 'art/skyboxes/sea_lf.jpg'
  16. )
  17. local w, d = lovr.headset.getBoundsDimensions()
  18. self.floor = g.newMesh({{ -w, 0, -d }, { -w, 0, d }, { w, 0, -d }, { w, 0, d }}, 'strip')
  19. self.block = {}
  20. self.block.position = vec3(0, 1, -4)
  21. self.block.size = .5
  22. self.transitionFactor = 0
  23. rattle:init()
  24. end
  25. function cry:update(dt)
  26. rattle:update(dt)
  27. self.block.position.x = math.sin(lovr.timer.getTime()) * self.block.position.z / 5
  28. if rattle.isShaking then
  29. local blockDirection = self.block.position - vec3(lovr.headset.getPosition())
  30. local a, rx, ry, rz = lovr.headset.getOrientation()
  31. local playerDirection = quat.from_angle_axis(a, vec3(rx, ry, rz)) * vec3.unit_z
  32. local factor = vec3.dot(blockDirection, playerDirection)
  33. self.block.position.z = _.clamp(self.block.position.z + factor * dt * .5, -5, .2)
  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) or math.huge
  39. if controller and trigger > .9 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 cry: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('k')
  64. g.pop()
  65. drawTransition(self.transitionFactor)
  66. end
  67. return cry