Compare commits

..

No commits in common. "9c070e161fcc69002342e2cbec2561f069d67ab2" and "61b8aa883b686f23601b152c895f6b5b8a5d2c9a" have entirely different histories.

3 changed files with 13 additions and 80 deletions

View File

@ -1,38 +1,36 @@
Camera = {
pos = Point:new(0, 0),
width = 0,
height = 0,
speed = 4,
pos = {x = 0, y = 0},
width = 0,
height = 0
}
function Camera:followPlayer(player)
-- make sure we have the Point metatable self:moveTowards(pos)
local pos = Point.copy(player.pos)
local pos = player.pos
local room = player:getCollidingAt(pos.x,pos.y,LoadedObjects.Rooms)
self:moveTowards(self:confineTo(room, pos))
self:positionCenterAt(pos.x, pos.y)
self:confineTo(room)
end
function Camera:confineTo(box, pos)
function Camera:confineTo(box)
if box == nil then
--frameDebug("not in a room")
return pos
return
end
--frameDebug("in a room")
local w = self.width/game.scale
local h = self.height/game.scale
local npos = pos - self:centerOffset()
-- bottom edge
npos.y = math.min(npos.y+h, box.to.y)-h
self.pos.y = math.min(self.pos.y+h, box.to.y)-h
-- right edge
npos.x = math.min(npos.x+w, box.to.x)-w
self.pos.x = math.min(self.pos.x+w, box.to.x)-w
-- top edge
npos.y = math.max(npos.y, box.from.y)
self.pos.y = math.max(self.pos.y, box.from.y)
-- left edge
npos.x = math.max(npos.x, box.from.x)
return npos + self:centerOffset()
self.pos.x = math.max(self.pos.x, box.from.x)
end
function Camera:confineToLevel()
@ -40,32 +38,6 @@ function Camera:confineToLevel()
self.pos.y = math.max(0,math.min(self.pos.y,LevelData.Height-self.height/game.scale))
end
function Camera:moveTowards(pt)
--local pt = Point:new(x,y)
local diff = pt - self:center()
local dist = diff:abs()
local npos
if dist < self.speed then
npos = pt
else
frameDebug("camera at speed limit")
npos = self:center() + diff * (self.speed/dist)
frameDebug("dist = "..dist..", npos = "..tostring(npos))
end
self:positionCenterAt(npos.x, npos.y)
end
function Camera:size()
return Point:new(self.width, self.height)
end
function Camera:centerOffset()
return self:size()/game.scale/2
end
function Camera:center()
return self.pos + self:centerOffset()
end
function Camera:positionCenterAt(x,y)
self.pos.x = x-self.width/game.scale/2
self.pos.y = y-self.height/game.scale/2
@ -76,4 +48,3 @@ function Camera:positionAt(x,y)
self.pos.x = math.floor((x/self.width)*self.width)
self.pos.y = math.floor((y/self.height)*self.height)
end

View File

@ -1,37 +0,0 @@
Point = {}
Point.__index = Point
function Point:new(x, y)
local o = { x = x or 0, y = y or 0 }
setmetatable(o, self)
return o
end
function Point:__add(other)
return Point:new(self.x+other.x, self.y+other.y)
end
function Point:__sub(other)
return Point:new(self.x-other.x, self.y-other.y)
end
function Point:__mul(n)
return Point:new(self.x*n, self.y*n)
end
function Point:__div(n)
return Point:new(self.x/n, self.y/n)
end
-- absolute value, or the distance from the origin
function Point:abs()
return math.sqrt(self.x ^ 2 + self.y ^ 2)
end
function Point:__tostring()
return "("..self.x..","..self.y..")"
end
function Point:copy()
return Point:new(self.x, self.y)
end

View File

@ -13,7 +13,6 @@ require "code/hex"
require "code/in_out"
-- classes
require "code/point"
require "code/objects"
require "code/level"
require "code/camera"