summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rw-r--r--camera.lua37
-rw-r--r--world.lua2
2 files changed, 31 insertions, 8 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
diff --git a/world.lua b/world.lua
index 49848d9..9b5569f 100644
--- a/world.lua
+++ b/world.lua
@@ -94,7 +94,7 @@ function World:update(dt)
-- Physical world
self.world:update(dt)
-- Camera
- self.camera:moveFollow()
+ self.camera:update(dt)
-- Nauts
for _,naut in pairs(self.Nauts) do
naut:update(dt)