summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rw-r--r--camera.lua51
-rw-r--r--ground.lua2
-rw-r--r--main.lua12
-rw-r--r--player.lua2
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
diff --git a/ground.lua b/ground.lua
index 9f695c8..23b4dcf 100644
--- a/ground.lua
+++ b/ground.lua
@@ -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)
diff --git a/main.lua b/main.lua
index d5b4d1a..3a895bf 100644
--- a/main.lua
+++ b/main.lua
@@ -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
diff --git a/player.lua b/player.lua
index 4d6f7ec..958ea8a 100644
--- a/player.lua
+++ b/player.lua
@@ -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)