Jelajahi Sumber

Screenshake;

bjorn 11 tahun lalu
induk
melakukan
06774b9bbe
4 mengubah file dengan 16 tambahan dan 0 penghapusan
  1. 1 0
      data/spell/dagger.lua
  2. 1 0
      data/spell/shotgun.lua
  3. 13 0
      lib/core/view.lua
  4. 1 0
      lib/playermain.lua

+ 1 - 0
data/spell/dagger.lua

@@ -23,6 +23,7 @@ function Dagger:activate(owner)
   end
   
   ctx.event:emit('sound.play', {sound = backstab and 'backstab' or 'slash', x = self.x, y = self.y})
+  if backstab and ctx.view then ctx.view:screenshake(25) end
 end
 
 function Dagger:update(owner)

+ 1 - 0
data/spell/shotgun.lua

@@ -89,6 +89,7 @@ Shotgun.activate = function(self)
   end
 
   ctx.event:emit('sound.play', {sound = 'shotgun', x = self.x, y = self.y})
+  if ctx.view then ctx.view:screenshake(5) end
 end
 
 Shotgun.update = function(self)

+ 13 - 0
lib/core/view.lua

@@ -32,6 +32,8 @@ function View:init()
   self.prevy = 0
   self.prevscale = self.scale
 
+  self.shake = 0
+
   self:register(data.media.shaders.motionBlur, 'shader')
   self:register(data.media.shaders.reddify, 'shader')
 end
@@ -44,6 +46,8 @@ function View:update()
   self:follow()
   self:contain()
 
+  self.shake = math.lerp(self.shake, 0, 8 * tickRate)
+
   table.sort(self.draws, function(a, b)
     return a.depth > b.depth
   end)
@@ -51,6 +55,10 @@ end
 
 function View:draw()
   local x, y, s = unpack(table.interpolate({self.prevx, self.prevy, self.prevscale}, {self.x, self.y, self.scale}, tickDelta / tickRate))
+  local shakex = 1 - (2 * love.math.noise(self.shake + x + tickDelta))
+  local shakey = 1 - (2 * love.math.noise(self.shake + y + tickDelta))
+  x = x + (shakex * self.shake)
+  y = y + (shakey * self.shake)
   local w, h = g.getDimensions()
 
   g.push()
@@ -185,4 +193,9 @@ end
 
 function View:frameMouseY()
   return love.mouse.getY() - self.frame.y
+end
+
+function View:screenshake(amount)
+  if self.shake > amount then self.shake = self.shake + (amount / 2) end
+  self.shake = amount
 end

+ 1 - 0
lib/playermain.lua

@@ -125,4 +125,5 @@ end
 function PlayerMain:die()
   if self.heartbeatSound then self.heartbeatSound:pause() end
   Player.die(self)
+  ctx.view:screenshake(20)
 end