summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rw-r--r--camera.lua52
1 files changed, 50 insertions, 2 deletions
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