Преглед на файлове

Sleep puzzle maybe done;

bjorn преди 9 години
родител
ревизия
c9512457d6
променени са 2 файла, в които са добавени 44 реда и са изтрити 2 реда
  1. 2 1
      app/rattle.lua
  2. 42 1
      app/sleep.lua

+ 2 - 1
app/rattle.lua

@@ -16,7 +16,8 @@ function rattle:update(dt)
     if self.lastPosition then
       local delta = pos - self.lastPosition
       self.shake = _.lerp(self.shake, delta:len() * 2, 6 * dt)
-      if self.shake > .035 then
+      self.isShaking = self.shake > .035
+      if self.isShaking then
         controller:vibrate(math.min((self.shake - .035) / 15, .003))
       end
     end

+ 42 - 1
app/sleep.lua

@@ -1,4 +1,7 @@
 local sleep = {}
+local controllers = require('app/controllers')
+local rattle = require('app/rattle')
+local vec3 = require('lib/cpml').vec3
 
 function sleep:init()
   self.skybox = g.newSkybox(
@@ -12,11 +15,43 @@ function sleep:init()
 
   self.floor = g.newBuffer(lovr.headset.getBoundsGeometry())
 
+  self.block = {}
+  self.block.maxY = 5
+  self.block.position = vec3(0, self.block.maxY, 0)
+  self.block.size = .2
+
   self.transitionFactor = 0
 end
 
 function sleep:update(dt)
-  --
+  rattle:update(dt)
+  if rattle.isShaking then
+    local shake = _.clamp((rattle.shake - .035) * 5, 0, 1)
+    local x, y, z = lovr.headset.getPosition()
+    local factor = (1 - _.clamp(y / 2, 0, 1)) ^ 5
+    self.block.position.y = math.max(self.block.position.y - dt * shake * factor, 1)
+  else
+    self.block.position.y = math.min(self.block.position.y + dt * .2, self.block.maxY)
+  end
+
+  local controller = controllers.list[1]
+  local trigger = controller and controller:getAxis('trigger')
+  local dist = controller and vec3(controller:getPosition()):dist(self.block.position)
+  if controller and trigger > .9 and dist < self.block.size / 2 then
+    self.transitionFactor = math.min(self.transitionFactor + dt, 1)
+
+    if self.transitionFactor > 0 then
+      controller:vibrate(self.transitionFactor^2 * .0035)
+    end
+
+    if self.transitionFactor >= 1 then
+      -- YOU WIN
+      local menu = require 'app/menu'
+      setState(menu)
+    end
+  else
+    self.transitionFactor = math.max(self.transitionFactor - dt, 0)
+  end
 end
 
 function sleep:draw()
@@ -24,8 +59,14 @@ function sleep:draw()
   g.setColor(255, 255, 255)
   self.skybox:draw(a, rx, ry, rz)
 
+  rattle:draw()
+
   g.setColor(255, 255, 255, 80)
   self.floor:draw()
+
+  local x, y, z = self.block.position:unpack()
+  g.setColor(128, 0, 255)
+  g.cube('fill', x, y, z, self.block.size)
 end
 
 return sleep