summaryrefslogtreecommitdiffhomepage
path: root/camera.lua
diff options
context:
space:
mode:
authorAki <nthirtyone@gmail.com>2016-05-18 22:22:24 +0200
committerAki <nthirtyone@gmail.com>2016-05-18 22:22:24 +0200
commit272ae9e4589d4b473641972aa1e7469deff76d1c (patch)
tree4a9d54e0c217f20e59d8912797a89045b7e3a6af /camera.lua
parent8a79b873c95bb155a70a460efd460dfc35b493bb (diff)
downloadroflnauts-272ae9e4589d4b473641972aa1e7469deff76d1c.zip
roflnauts-272ae9e4589d4b473641972aa1e7469deff76d1c.tar.gz
roflnauts-272ae9e4589d4b473641972aa1e7469deff76d1c.tar.bz2
Smoothened camera
Diffstat (limited to 'camera.lua')
-rw-r--r--camera.lua37
1 files changed, 30 insertions, 7 deletions
diff --git a/camera.lua b/camera.lua
index e2b2362..461080a 100644
--- a/camera.lua
+++ b/camera.lua
@@ -5,9 +5,10 @@
Camera = {
x = 0,
y = 0,
+ dest_x = 0,
+ dest_y = 0,
scale = 4,
world = nil, -- game world
- follow = nil
}
-- Constructor of `Camera`
@@ -15,7 +16,6 @@ function Camera:new (world)
local o = {}
setmetatable(o, self)
self.__index = self
- o.follow = {}
o.world = world
return o
end
@@ -32,15 +32,38 @@ function Camera:setPosition (x, y)
self.x, self.y = x, y
end
+-- Destination
+function Camera:setDestination (x, y)
+ local x = x or 0
+ local y = y or 0
+ self.dest_x, self.dest_y = x, y
+end
+
+function Camera:getDestination ()
+ return self.dest_x, self.dest_y
+end
+
-- Move follow
-function Camera:moveFollow ()
+function Camera:follow ()
local x,y,i = 105, 120, 1
for k,point in pairs(self.world.Nauts) do
- i = i + 1
- x = math.max(math.min(point.body:getX(),290),0) + x
- y = math.max(math.min(point.body:getY(),180),20) + y
+ if point.body:getX() > -20 and point.body:getX() < 310 and
+ point.body:getY() > -70 and point.body:getY() < 200 then
+ i = i + 1
+ x = point.body:getX() + x
+ y = point.body:getY() + y
+ end
end
x = x / i - 145
y = y / i - 90
- self:setPosition(x,y)
+ self:setDestination(x,y)
+end
+
+-- Update
+function Camera:update (dt)
+ self:follow()
+ local dx, dy = self:getDestination()
+ dx = (dx - self.x) * 8 * dt
+ dy = (dy - self.y) * 8 * dt
+ self:setPosition(self.x + dx, self.y + dy)
end \ No newline at end of file