diff options
author | Aki <nthirtyone@gmail.com> | 2016-05-13 15:00:13 +0200 |
---|---|---|
committer | Aki <nthirtyone@gmail.com> | 2016-05-13 15:00:13 +0200 |
commit | 0506e2dd9cb9641486bbfc7908c0399b26e1ce3c (patch) | |
tree | 1d673944ed31946959f4b08c9c412cc8f2df12d5 | |
parent | 7d4251e3a9c08b89d25ef724270752f36649f765 (diff) | |
download | roflnauts-0506e2dd9cb9641486bbfc7908c0399b26e1ce3c.zip roflnauts-0506e2dd9cb9641486bbfc7908c0399b26e1ce3c.tar.gz roflnauts-0506e2dd9cb9641486bbfc7908c0399b26e1ce3c.tar.bz2 |
Basic camera added
-rw-r--r-- | camera.lua | 51 | ||||
-rw-r--r-- | ground.lua | 2 | ||||
-rw-r--r-- | main.lua | 12 | ||||
-rw-r--r-- | player.lua | 2 |
4 files changed, 62 insertions, 5 deletions
diff --git a/camera.lua b/camera.lua new file mode 100644 index 0000000..793b0d1 --- /dev/null +++ b/camera.lua @@ -0,0 +1,51 @@ +-- `Camera` +-- Used in drawing. + +-- Metatable of `Camera` +Camera = { + x = 0, + y = 0, + follow = nil +} + +-- Constructor of `Camera` +function Camera:new () + local o = {} + setmetatable(o, self) + self.__index = self + o.follow = {} + return o +end + +-- Drawing offsets +function Camera:getOffsets () + return -self.x,-self.y +end + +-- Position +function Camera:setPosition (x, y) + local x = x or 0 + local y = y or 0 + self.x, self.y = x, y +end + +-- Add follow position +function Camera:addFollow (x, y, w) + local w = 1 + if x ~= nil and y ~= nil then + table.insert(self.follow, {["x"] = x, ["y"] = y, ["w"] = w}) + end +end + +-- Move follow +function Camera:moveFollow () + local x,y,i = 145, 90, 1 + for k,point in pairs(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),0) + y + end + x = x / i - 145 + y = y / i - 90 + self:setPosition(x,y) +end
\ No newline at end of file @@ -34,7 +34,7 @@ function Ground:draw (offset_x, offset_y, debug) local debug = debug or false -- sprite draw love.graphics.setColor(255,255,255,255) - love.graphics.draw(self.sprite, self.body:getX()-math.ceil(self.sprite:getWidth()/2), self.body:getY()) + love.graphics.draw(self.sprite, self.body:getX()-math.ceil(self.sprite:getWidth()/2)+offset_x, self.body:getY()+offset_y) -- debug draw if debug then love.graphics.setColor(220, 220, 220, 100) @@ -3,6 +3,7 @@ require "ground" require "player" +require "camera" debug = false @@ -15,7 +16,7 @@ function love.load () love.physics.setMeter(64) world = love.physics.newWorld(0, 9.81*64, true) world:setCallbacks(beginContact, endContact, preSolve, postSolve) - + -- Platforms (`Ground`) Platforms = {} table.insert(Platforms, Ground:new(world, 290/2, 180/2, {-91,0, 90,0, 90,10, 5,76, -5,76, -91,10}, "assets/platform_big.png")) @@ -36,11 +37,15 @@ function love.load () Nauts[2].key_down = "s" Nauts[2].key_jump = "h" Nauts[2].key_hit = "g" + + -- Camera + camera = Camera:new() end function love.update (dt) -- Put world in motion! world:update(dt) + camera:moveFollow() -- Players for k,naut in pairs(Nauts) do naut:update(dt) @@ -73,14 +78,15 @@ function love.draw () love.graphics.setColor(179, 82, 80, 255) love.graphics.rectangle("fill", 0, 160, love.graphics.getWidth(), 40) + local offset_x, offset_y = camera:getOffsets() -- Draw ground for k,platform in pairs(Platforms) do - platform:draw(0, 0, debug) + platform:draw(offset_x, offset_y, debug) end -- Draw player for k,naut in pairs(Nauts) do - naut:draw(0, 0, debug) + naut:draw(offset_x, offset_y, debug) end end @@ -192,7 +192,7 @@ function Player:draw (offset_x, offset_y, debug) local debug = debug or false -- sprite draw love.graphics.setColor(255,255,255,255) - love.graphics.draw(self.sprite, self.current[self.frame], self.body:getX(), self.body:getY(), self.rotate, self.facing, 1, 12, 15) + love.graphics.draw(self.sprite, self.current[self.frame], self.body:getX()+offset_x, self.body:getY()+offset_y, self.rotate, self.facing, 1, 12, 15) -- debug draw if debug then love.graphics.setColor(50, 255, 50, 100) |