diff options
Diffstat (limited to 'camera.lua')
-rw-r--r-- | camera.lua | 37 |
1 files changed, 30 insertions, 7 deletions
@@ -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 |