diff options
author | Aki <nthirtyone@gmail.com> | 2016-05-19 21:38:17 +0200 |
---|---|---|
committer | Aki <nthirtyone@gmail.com> | 2016-05-19 21:38:17 +0200 |
commit | ea6a6856bfdd98524c2ec4cf52e5c926a31f9697 (patch) | |
tree | 67fed35005d0d3304f5b6c80e76f764d7205d33a | |
parent | 67ff1c220df722d6ab6a0396f84b269d783e5d88 (diff) | |
download | roflnauts-ea6a6856bfdd98524c2ec4cf52e5c926a31f9697.zip roflnauts-ea6a6856bfdd98524c2ec4cf52e5c926a31f9697.tar.gz roflnauts-ea6a6856bfdd98524c2ec4cf52e5c926a31f9697.tar.bz2 |
Shake camera
-rw-r--r-- | camera.lua | 52 |
1 files changed, 50 insertions, 2 deletions
@@ -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 |