From e648705e5be4bac5a438f935d163d63d32e560c7 Mon Sep 17 00:00:00 2001 From: binarycat Date: Fri, 11 Mar 2022 13:16:45 -0500 Subject: [PATCH 1/5] added Point class --- code/point.lua | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 code/point.lua diff --git a/code/point.lua b/code/point.lua new file mode 100644 index 0000000..307566b --- /dev/null +++ b/code/point.lua @@ -0,0 +1,30 @@ +Point = {} +Point.__index = Point + +function Point:new(x, y) + local o = { x = x, y = y } + 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 + From c0af34fd764941d4f59de55c3d5bc93493b17c04 Mon Sep 17 00:00:00 2001 From: binarycat Date: Fri, 11 Mar 2022 14:07:33 -0500 Subject: [PATCH 2/5] add Point.__tostring --- code/point.lua | 3 +++ 1 file changed, 3 insertions(+) diff --git a/code/point.lua b/code/point.lua index 307566b..aef9aa0 100644 --- a/code/point.lua +++ b/code/point.lua @@ -28,3 +28,6 @@ function Point:abs() return math.sqrt(self.x ^ 2 + self.y ^ 2) end +function Point:__tostring() + return "("..self.x..","..self.y..")" +end From 644b1a4828fac2936afb109b0cf0d31d1c0e6669 Mon Sep 17 00:00:00 2001 From: binarycat Date: Fri, 11 Mar 2022 14:07:54 -0500 Subject: [PATCH 3/5] require point.lua --- code/require.lua | 1 + 1 file changed, 1 insertion(+) diff --git a/code/require.lua b/code/require.lua index 475343e..92a8a45 100644 --- a/code/require.lua +++ b/code/require.lua @@ -13,6 +13,7 @@ require "code/hex" require "code/in_out" -- classes +require "code/point" require "code/objects" require "code/level" require "code/camera" From 362c7ea52debc32942d006b004a35ba07222c0f7 Mon Sep 17 00:00:00 2001 From: binarycat Date: Fri, 11 Mar 2022 14:10:18 -0500 Subject: [PATCH 4/5] add Point.copy --- code/point.lua | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/code/point.lua b/code/point.lua index aef9aa0..3c56630 100644 --- a/code/point.lua +++ b/code/point.lua @@ -31,3 +31,7 @@ end function Point:__tostring() return "("..self.x..","..self.y..")" end + +function Point:copy() + return Point:new(self.x, self.y) +end From 4ae674f0a7405a5b19b6234b29a32ddb6c30a4af Mon Sep 17 00:00:00 2001 From: binarycat Date: Fri, 11 Mar 2022 14:49:58 -0500 Subject: [PATCH 5/5] smooth camera --- code/camera.lua | 55 +++++++++++++++++++++++++++++++++++++------------ code/point.lua | 2 +- 2 files changed, 43 insertions(+), 14 deletions(-) diff --git a/code/camera.lua b/code/camera.lua index 4528851..35f3a2d 100644 --- a/code/camera.lua +++ b/code/camera.lua @@ -1,36 +1,38 @@ Camera = { - pos = {x = 0, y = 0}, - width = 0, - height = 0 + pos = Point:new(0, 0), + width = 0, + height = 0, + speed = 4, } function Camera:followPlayer(player) - local pos = player.pos + -- make sure we have the Point metatable self:moveTowards(pos) + local pos = Point.copy(player.pos) local room = player:getCollidingAt(pos.x,pos.y,LoadedObjects.Rooms) - self:positionCenterAt(pos.x, pos.y) - - self:confineTo(room) + self:moveTowards(self:confineTo(room, pos)) end -function Camera:confineTo(box) +function Camera:confineTo(box, pos) if box == nil then --frameDebug("not in a room") - return + return pos end --frameDebug("in a room") local w = self.width/game.scale local h = self.height/game.scale + local npos = pos - self:centerOffset() -- bottom edge - self.pos.y = math.min(self.pos.y+h, box.to.y)-h + npos.y = math.min(npos.y+h, box.to.y)-h -- right edge - self.pos.x = math.min(self.pos.x+w, box.to.x)-w + npos.x = math.min(npos.x+w, box.to.x)-w -- top edge - self.pos.y = math.max(self.pos.y, box.from.y) + npos.y = math.max(npos.y, box.from.y) -- left edge - self.pos.x = math.max(self.pos.x, box.from.x) + npos.x = math.max(npos.x, box.from.x) + return npos + self:centerOffset() end function Camera:confineToLevel() @@ -38,6 +40,32 @@ 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 @@ -48,3 +76,4 @@ 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 + diff --git a/code/point.lua b/code/point.lua index 3c56630..a05ddbd 100644 --- a/code/point.lua +++ b/code/point.lua @@ -2,7 +2,7 @@ Point = {} Point.__index = Point function Point:new(x, y) - local o = { x = x, y = y } + local o = { x = x or 0, y = y or 0 } setmetatable(o, self) return o end