From ea6a6856bfdd98524c2ec4cf52e5c926a31f9697 Mon Sep 17 00:00:00 2001 From: Aki Date: Thu, 19 May 2016 21:38:17 +0200 Subject: Shake camera --- camera.lua | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 50 insertions(+), 2 deletions(-) (limited to 'camera.lua') diff --git a/camera.lua b/camera.lua index adc1041..0804860 100644 --- a/camera.lua +++ b/camera.lua @@ -8,6 +8,13 @@ Camera = { dest_x = 0, dest_y = 0, scale = 4, + shake = 0, + timer = 0, + delay = 0, + origin_x = 0, + origin_y = 0, + shake_x = 0, + shake_y = 0, world = nil, -- game world } @@ -32,6 +39,10 @@ function Camera:setPosition (x, y) self.x, self.y = x, y end +function Camera:getPosition () + return self.x, self.y +end + -- Destination function Camera:setDestination (x, y) local x = x or 0 @@ -43,6 +54,33 @@ function Camera:getDestination () return self.dest_x, self.dest_y end +-- Shake it +-- Really bad script, but for now it works +function Camera:shake () + if self.shake_x == 0 then + self.shake_x = math.random(-10, 10) * 2 + elseif self.shake_x > 0 then + self.shake_x = math.random(-10, -1) * 2 + elseif self.shake_x < 0 then + self.shake_x = math.random(10, 1) * 2 + end + if self.shake_y == 0 then + self.shake_y = math.random(-10, 10) * 2 + elseif self.shake_y > 0 then + self.shake_y = math.random(-10, -1) * 2 + elseif self.shake_y < 0 then + self.shake_y = math.random(10, 1) * 2 + end + local x = self.origin_x + self.shake_x + local y = self.origin_y + self.shake_y + self:setDestination(x, y) +end + +function Camera:startShake () + self.timer = 0.3 + self.origin_x, self.origin_y = self:getPosition() +end + -- Move follow function Camera:follow () local x,y,i = 145, 90, 1 @@ -61,9 +99,19 @@ end -- Update function Camera:update (dt) - self:follow() + if self.timer > 0 then + self.timer = self.timer - dt + if self.delay <= 0 then + self:shake() + self.delay = 0.02 + else + self.delay = self.delay - dt + end + else + self:follow() + end local dx, dy = self:getDestination() dx = (dx - self.x) * 6 * dt dy = (dy - self.y) * 6 * dt self:setPosition(self.x + dx, self.y + dy) -end \ No newline at end of file +end -- cgit v1.1