From 27a1d0de613a360912d1e9f3a5db7ab044a0b450 Mon Sep 17 00:00:00 2001 From: Aki Date: Sat, 2 Sep 2017 21:16:30 +0200 Subject: Center_* changed to center.* (table) --- not/Camera.lua | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'not/Camera.lua') diff --git a/not/Camera.lua b/not/Camera.lua index aa4df5b..183a323 100644 --- a/not/Camera.lua +++ b/not/Camera.lua @@ -108,11 +108,11 @@ end -- Move follow function Camera:follow () local map = self.world.map - local sum_x,sum_y,i = map.center_x, map.center_y, 1 + local sum_x,sum_y,i = map.center.x, map.center.y, 1 for k,naut in pairs(self.world.Nauts) do local naut_x,naut_y = naut:getPosition() - if math.abs(naut_x - map.center_x) < map.width/2 and - math.abs(naut_y - map.center_y) < map.height/2 then + if math.abs(naut_x - map.center.x) < map.width/2 and + math.abs(naut_y - map.center.y) < map.height/2 then i = i + 1 sum_x = naut_x + sum_x sum_y = naut_y + sum_y -- cgit v1.1 From b4254d4281cae95d72c5c8ae12119494d97f1802 Mon Sep 17 00:00:00 2001 From: Aki Date: Thu, 7 Sep 2017 05:25:01 +0200 Subject: Removed tables for specific objects and replaced references with proper method calls --- not/Camera.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'not/Camera.lua') diff --git a/not/Camera.lua b/not/Camera.lua index 183a323..6e07372 100644 --- a/not/Camera.lua +++ b/not/Camera.lua @@ -109,7 +109,7 @@ end function Camera:follow () local map = self.world.map local sum_x,sum_y,i = map.center.x, map.center.y, 1 - for k,naut in pairs(self.world.Nauts) do + for k,naut in pairs(self.world:getNautsAll()) do local naut_x,naut_y = naut:getPosition() if math.abs(naut_x - map.center.x) < map.width/2 and math.abs(naut_y - map.center.y) < map.height/2 then -- cgit v1.1 From ca8dbe135034f7a72b6b40f6dfd7b772bd51cc39 Mon Sep 17 00:00:00 2001 From: Aki Date: Sat, 9 Sep 2017 06:17:37 +0200 Subject: Started Camera rework --- not/Camera.lua | 39 +++++++++++++++------------------------ 1 file changed, 15 insertions(+), 24 deletions(-) (limited to 'not/Camera.lua') diff --git a/not/Camera.lua b/not/Camera.lua index 6e07372..79ebd09 100644 --- a/not/Camera.lua +++ b/not/Camera.lua @@ -1,29 +1,20 @@ ---- `Camera` --- Used in drawing. -Camera = { - x = 0, - y = 0, - dest_x = 0, - dest_y = 0, - shake = 0, - timer = 0, - delay = 0, - origin_x = 0, - origin_y = 0, - shake_x = 0, - shake_y = 0, - world = --[[not.World]]nil, -} +--- Used in drawing other stuff in places. +Camera = require "not.Object":extends() --- Constructor of `Camera` function Camera:new (world) - local o = {} - setmetatable(o, self) - self.__index = self - o.world = world - o:setPosition(o:follow()) - o:setDestination(o:follow()) - return o + self.world = world + self.x = 0 + self.y = 0 + self.dest_y = 0 + self.dest_x = 0 + self.timer = 0 + self.delay = 0 + self.origin_x = 0 + self.origin_y = 0 + self.shake_x = 0 + self.shake_y = 0 + self:setPosition(self:follow()) + self:setDestination(self:follow()) end -- Drawing offsets -- cgit v1.1 From e6094cf0fddbd8b66426679d79020b06aacb600a Mon Sep 17 00:00:00 2001 From: Aki Date: Sun, 10 Sep 2017 19:47:12 +0200 Subject: Camera now uses love.graphics.translate --- not/Camera.lua | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) (limited to 'not/Camera.lua') diff --git a/not/Camera.lua b/not/Camera.lua index 79ebd09..cff5d0d 100644 --- a/not/Camera.lua +++ b/not/Camera.lua @@ -1,6 +1,7 @@ --- Used in drawing other stuff in places. Camera = require "not.Object":extends() +-- TODO: Camera would really make use of vec2s (other classes would use them too). function Camera:new (world) self.world = world self.x = 0 @@ -17,12 +18,15 @@ function Camera:new (world) self:setDestination(self:follow()) end --- Drawing offsets -function Camera:getOffsets () - return -self.x,-self.y +function Camera:translate () + love.graphics.push() + love.graphics.translate(-self.x*getScale(), -self.y*getScale()) +end + +function Camera:pop () + love.graphics.pop() end --- Position function Camera:setPosition (x, y) local x = x or 0 local y = y or 0 @@ -37,7 +41,6 @@ function Camera:getPositionScaled () return self.x*getScale(), self.y*getScale() end --- Destination function Camera:setDestination (x, y) local x = x or 0 local y = y or 0 @@ -48,14 +51,13 @@ function Camera:getDestination () return self.dest_x, self.dest_y end --- Translate points function Camera:translatePosition (x, y) local x = x or 0 local y = y or 0 return (x-self.x)*getScale(), (y-self.y)*getScale() end -function Camera:translatePoints(...) +function Camera:translatePoints (...) local a = {...} local r = {} local x,y = self:getOffsets() @@ -96,7 +98,6 @@ function Camera:startShake () self.origin_x, self.origin_y = self:getPosition() end --- Move follow function Camera:follow () local map = self.world.map local sum_x,sum_y,i = map.center.x, map.center.y, 1 @@ -114,7 +115,6 @@ function Camera:follow () return x,y end --- Update function Camera:update (dt) if self.timer > 0 then self.timer = self.timer - dt -- cgit v1.1 From f251032110f8c7ba337a6ee4196083005c146fbc Mon Sep 17 00:00:00 2001 From: Aki Date: Mon, 11 Sep 2017 01:39:18 +0200 Subject: New shaking implementation --- not/Camera.lua | 90 +++++++++++++++++++++++++++++++++------------------------- 1 file changed, 52 insertions(+), 38 deletions(-) (limited to 'not/Camera.lua') diff --git a/not/Camera.lua b/not/Camera.lua index cff5d0d..f2211fa 100644 --- a/not/Camera.lua +++ b/not/Camera.lua @@ -1,26 +1,36 @@ --- Used in drawing other stuff in places. Camera = require "not.Object":extends() +Camera.SHAKE_LENGTH = 0.8 +Camera.SHAKE_INTERVAL = 0.04 + -- TODO: Camera would really make use of vec2s (other classes would use them too). function Camera:new (world) self.world = world + self.x = 0 self.y = 0 self.dest_y = 0 self.dest_x = 0 - self.timer = 0 - self.delay = 0 self.origin_x = 0 self.origin_y = 0 - self.shake_x = 0 - self.shake_y = 0 + self:setPosition(self:follow()) self:setDestination(self:follow()) + + self.shakeTime = 0 + self.shakeInterval = 0 + self.shakeShift = { + theta = love.math.random() * 2, + radius = 0 + } end function Camera:translate () + local x, y = self:getPositionScaled() + local dx, dy = self:getShakeShift() love.graphics.push() - love.graphics.translate(-self.x*getScale(), -self.y*getScale()) + love.graphics.translate(-x - dx, -y - dy) end function Camera:pop () @@ -38,7 +48,8 @@ function Camera:getPosition () end function Camera:getPositionScaled () - return self.x*getScale(), self.y*getScale() + local scale = getScale() + return self.x * scale, self.y * scale end function Camera:setDestination (x, y) @@ -71,31 +82,43 @@ function Camera:translatePoints (...) return r end --- Shake it --- Really bad script, but for now it works -function Camera:shake () - if self.shake_x == 0 then - self.shake_x = math.random(-10, 10) * 2 - elseif self.shake_x > 0 then - self.shake_x = math.random(-10, -1) * 2 - elseif self.shake_x < 0 then - self.shake_x = math.random(10, 1) * 2 +function Camera:startShake () + self.shakeTime = Camera.SHAKE_LENGTH +end + +local +function limit (theta) + if theta > 2 then + return limitAngle(theta - 2) end - if self.shake_y == 0 then - self.shake_y = math.random(-10, 10) * 2 - elseif self.shake_y > 0 then - self.shake_y = math.random(-10, -1) * 2 - elseif self.shake_y < 0 then - self.shake_y = math.random(10, 1) * 2 + if theta < 0 then + return limitAngle(theta + 2) end - local x = self.origin_x + self.shake_x - local y = self.origin_y + self.shake_y - self:setDestination(x, y) + return theta end -function Camera:startShake () - self.timer = 0.3 - self.origin_x, self.origin_y = self:getPosition() +-- TODO: Magic numbers present in Camera's shake. +function Camera:shake (dt) + if self.shakeTime > 0 then + self.shakeTime = self.shakeTime - dt + if self.shakeInterval < 0 then + self.shakeShift.theta = self.shakeShift.theta - 1.3 + love.math.random() * 0.6 + self.shakeShift.radius = 80 * self.shakeTime + self.shakeInterval = Camera.SHAKE_INTERVAL + else + self.shakeShift.radius = self.shakeShift.radius * 0.66 + self.shakeInterval = self.shakeInterval - dt + end + if self.shakeTime < 0 then + self.shakeShift.radius = 0 + end + end +end + +function Camera:getShakeShift () + local radius = self.shakeShift.radius + local theta = self.shakeShift.theta * math.pi + return radius * math.cos(theta), radius * math.sin(theta) end function Camera:follow () @@ -116,17 +139,8 @@ function Camera:follow () end function Camera:update (dt) - if self.timer > 0 then - self.timer = self.timer - dt - if self.delay <= 0 then - self:shake() - self.delay = 0.02 - else - self.delay = self.delay - dt - end - else - self:setDestination(self:follow()) - end + self:shake(dt) + self:setDestination(self:follow()) local dx, dy = self:getDestination() dx = (dx - self.x) * 6 * dt dy = (dy - self.y) * 6 * dt -- cgit v1.1 From 6ed92013f8096057e4aad3f5a730e3586d9eb0e6 Mon Sep 17 00:00:00 2001 From: Aki Date: Mon, 11 Sep 2017 02:17:07 +0200 Subject: Hotfix for Camera's translatePoints --- not/Camera.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'not/Camera.lua') diff --git a/not/Camera.lua b/not/Camera.lua index f2211fa..8008a0d 100644 --- a/not/Camera.lua +++ b/not/Camera.lua @@ -71,7 +71,7 @@ end function Camera:translatePoints (...) local a = {...} local r = {} - local x,y = self:getOffsets() + local x,y = 0,0 for k,v in pairs(a) do if k%2 == 1 then table.insert(r, (v + x) * getScale()) -- cgit v1.1 From 9e2ec1aa5abb84d3e46c34ed407934ac54fce331 Mon Sep 17 00:00:00 2001 From: Aki Date: Mon, 11 Sep 2017 02:39:28 +0200 Subject: Some shake tweaking --- not/Camera.lua | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) (limited to 'not/Camera.lua') diff --git a/not/Camera.lua b/not/Camera.lua index 8008a0d..8e74a76 100644 --- a/not/Camera.lua +++ b/not/Camera.lua @@ -1,8 +1,8 @@ --- Used in drawing other stuff in places. Camera = require "not.Object":extends() -Camera.SHAKE_LENGTH = 0.8 -Camera.SHAKE_INTERVAL = 0.04 +Camera.SHAKE_LENGTH = 0.6 +Camera.SHAKE_INTERVAL = 0.03 -- TODO: Camera would really make use of vec2s (other classes would use them too). function Camera:new (world) @@ -28,7 +28,7 @@ end function Camera:translate () local x, y = self:getPositionScaled() - local dx, dy = self:getShakeShift() + local dx, dy = self:getShakeScaled() love.graphics.push() love.graphics.translate(-x - dx, -y - dy) end @@ -103,7 +103,7 @@ function Camera:shake (dt) self.shakeTime = self.shakeTime - dt if self.shakeInterval < 0 then self.shakeShift.theta = self.shakeShift.theta - 1.3 + love.math.random() * 0.6 - self.shakeShift.radius = 80 * self.shakeTime + self.shakeShift.radius = 70 * self.shakeTime self.shakeInterval = Camera.SHAKE_INTERVAL else self.shakeShift.radius = self.shakeShift.radius * 0.66 @@ -115,12 +115,18 @@ function Camera:shake (dt) end end -function Camera:getShakeShift () +function Camera:getShake () local radius = self.shakeShift.radius local theta = self.shakeShift.theta * math.pi return radius * math.cos(theta), radius * math.sin(theta) end +function Camera:getShakeScaled () + local x, y = self:getShake() + local scale = getScale() + return x * scale, y * scale +end + function Camera:follow () local map = self.world.map local sum_x,sum_y,i = map.center.x, map.center.y, 1 -- cgit v1.1 From 9ddacd0f6481661cdaeab8abf2000b45a296faee Mon Sep 17 00:00:00 2001 From: Aki Date: Mon, 11 Sep 2017 06:17:29 +0200 Subject: Camera methods have some funky names. New following implementation --- not/Camera.lua | 80 ++++++++++++++++++++++++++++------------------------------ 1 file changed, 39 insertions(+), 41 deletions(-) (limited to 'not/Camera.lua') diff --git a/not/Camera.lua b/not/Camera.lua index 8e74a76..76f36ed 100644 --- a/not/Camera.lua +++ b/not/Camera.lua @@ -5,19 +5,14 @@ Camera.SHAKE_LENGTH = 0.6 Camera.SHAKE_INTERVAL = 0.03 -- TODO: Camera would really make use of vec2s (other classes would use them too). -function Camera:new (world) +function Camera:new (x, y, world) self.world = world + self:setPosition(x, y) + self:resetSum() + self:initShake() +end - self.x = 0 - self.y = 0 - self.dest_y = 0 - self.dest_x = 0 - self.origin_x = 0 - self.origin_y = 0 - - self:setPosition(self:follow()) - self:setDestination(self:follow()) - +function Camera:initShake () self.shakeTime = 0 self.shakeInterval = 0 self.shakeShift = { @@ -30,7 +25,7 @@ function Camera:translate () local x, y = self:getPositionScaled() local dx, dy = self:getShakeScaled() love.graphics.push() - love.graphics.translate(-x - dx, -y - dy) + love.graphics.translate(160*getScale() - x - dx, 100*getScale() - y - dy) end function Camera:pop () @@ -52,16 +47,6 @@ function Camera:getPositionScaled () return self.x * scale, self.y * scale end -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 - function Camera:translatePosition (x, y) local x = x or 0 local y = y or 0 @@ -103,7 +88,7 @@ function Camera:shake (dt) self.shakeTime = self.shakeTime - dt if self.shakeInterval < 0 then self.shakeShift.theta = self.shakeShift.theta - 1.3 + love.math.random() * 0.6 - self.shakeShift.radius = 70 * self.shakeTime + self.shakeShift.radius = 50 * self.shakeTime self.shakeInterval = Camera.SHAKE_INTERVAL else self.shakeShift.radius = self.shakeShift.radius * 0.66 @@ -127,28 +112,41 @@ function Camera:getShakeScaled () return x * scale, y * scale end -function Camera:follow () +function Camera:resetSum () + self.sumX = 0 + self.sumY = 0 + self.sumI = 0 +end + +function Camera:sum (x, y) local map = self.world.map - local sum_x,sum_y,i = map.center.x, map.center.y, 1 - for k,naut in pairs(self.world:getNautsAll()) do - local naut_x,naut_y = naut:getPosition() - if math.abs(naut_x - map.center.x) < map.width/2 and - math.abs(naut_y - map.center.y) < map.height/2 then - i = i + 1 - sum_x = naut_x + sum_x - sum_y = naut_y + sum_y - end + if math.abs(x - map.center.x) < map.width/2 and + math.abs(y - map.center.y) < map.height/2 then + self.sumX = self.sumX + x + self.sumY = self.sumY + y + self.sumI = self.sumI + 1 + end +end + +function Camera:getSumPostion () + if self.sumI > 0 then + return self.sumX / self.sumI, self.sumY / self.sumI + end + return 0, 0 +end + +function Camera:step (dt) + local x, y = self:getSumPostion() + local dx, dy = (x - self.x), (y - self.y) + if math.abs(dx) > 0.4 or math.abs(dy) > 0.4 then + x = self.x + (x - self.x) * dt * 6 + y = self.y + (y - self.y) * dt * 6 end - local x = sum_x / i - love.graphics.getWidth()/getScale()/2 - local y = sum_y / i - love.graphics.getHeight()/getScale()/2 + 4*getScale() -- hotfix - return x,y + self:setPosition(x, y) end function Camera:update (dt) + self:step(dt) self:shake(dt) - self:setDestination(self:follow()) - local dx, dy = self:getDestination() - dx = (dx - self.x) * 6 * dt - dy = (dy - self.y) * 6 * dt - self:setPosition(self.x + dx, self.y + dy) + self:resetSum() end -- cgit v1.1 From 81ec1a6509b0f349c145dd1b4d40029d141cbd6e Mon Sep 17 00:00:00 2001 From: Aki Date: Mon, 11 Sep 2017 19:22:18 +0200 Subject: Debug drawing changed to use new Camera properly --- not/Camera.lua | 32 ++++++++++++++++++-------------- 1 file changed, 18 insertions(+), 14 deletions(-) (limited to 'not/Camera.lua') diff --git a/not/Camera.lua b/not/Camera.lua index 76f36ed..025e9e5 100644 --- a/not/Camera.lua +++ b/not/Camera.lua @@ -21,6 +21,7 @@ function Camera:initShake () } end +-- TODO: Even more magic numbers present in Camera. Translate method. function Camera:translate () local x, y = self:getPositionScaled() local dx, dy = self:getShakeScaled() @@ -47,22 +48,25 @@ function Camera:getPositionScaled () return self.x * scale, self.y * scale end -function Camera:translatePosition (x, y) - local x = x or 0 - local y = y or 0 - return (x-self.x)*getScale(), (y-self.y)*getScale() +-- TODO: Magic numbers present in camera's boundaries. +function Camera:getBoundaries () + local x, y = self:getPosition() + return x - 160, y - 100, x + 160, y + 100 end -function Camera:translatePoints (...) - local a = {...} - local r = {} - local x,y = 0,0 - for k,v in pairs(a) do - if k%2 == 1 then - table.insert(r, (v + x) * getScale()) - else - table.insert(r, (v + y) * getScale()) - end +function Camera:getBoundariesScaled () + local x, y = self:getPositionScaled() + local width, height = love.graphics.getDimensions() + width = width / 2 + height = height / 2 + return x - width, y - height, x + width, y + height +end + +-- TODO: Camera@scalePoints is left because PhysicalBody still uses it as love.graphics.scale is not used yet. +function Camera:scalePoints (...) + local a, r, scale = {...}, {}, getScale() + for _,v in pairs(a) do + table.insert(r, v * scale) end return r end -- cgit v1.1 From 4490b5b5fcaddbf55fb229caeb93c879fe254292 Mon Sep 17 00:00:00 2001 From: Aki Date: Mon, 11 Sep 2017 19:54:14 +0200 Subject: Layer displacement ratio added to new Camera and to World for tests --- not/Camera.lua | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) (limited to 'not/Camera.lua') diff --git a/not/Camera.lua b/not/Camera.lua index 025e9e5..4f57327 100644 --- a/not/Camera.lua +++ b/not/Camera.lua @@ -22,9 +22,15 @@ function Camera:initShake () end -- TODO: Even more magic numbers present in Camera. Translate method. -function Camera:translate () +function Camera:translate (ratio) local x, y = self:getPositionScaled() local dx, dy = self:getShakeScaled() + if ratio then + dx = dx * ratio + dy = dy * ratio + x = x * ratio + y = y * ratio + end love.graphics.push() love.graphics.translate(160*getScale() - x - dx, 100*getScale() - y - dy) end -- cgit v1.1 From 844a4a14e14ae427ba595f04b1a7d46372952669 Mon Sep 17 00:00:00 2001 From: Aki Date: Mon, 11 Sep 2017 19:58:15 +0200 Subject: It seems I wasn't working in 16:10 --- not/Camera.lua | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'not/Camera.lua') diff --git a/not/Camera.lua b/not/Camera.lua index 4f57327..1fa01d3 100644 --- a/not/Camera.lua +++ b/not/Camera.lua @@ -32,7 +32,7 @@ function Camera:translate (ratio) y = y * ratio end love.graphics.push() - love.graphics.translate(160*getScale() - x - dx, 100*getScale() - y - dy) + love.graphics.translate(160*getScale() - x - dx, 90*getScale() - y - dy) end function Camera:pop () @@ -57,7 +57,7 @@ end -- TODO: Magic numbers present in camera's boundaries. function Camera:getBoundaries () local x, y = self:getPosition() - return x - 160, y - 100, x + 160, y + 100 + return x - 160, y - 90, x + 160, y + 90 end function Camera:getBoundariesScaled () -- cgit v1.1 From 44bc9aa6fa62c1b23d1e98de8729c72132a88abc Mon Sep 17 00:00:00 2001 From: Aki Date: Tue, 12 Sep 2017 01:45:36 +0200 Subject: Proper Camera scaling introduction --- not/Camera.lua | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) (limited to 'not/Camera.lua') diff --git a/not/Camera.lua b/not/Camera.lua index 1fa01d3..c3aaae6 100644 --- a/not/Camera.lua +++ b/not/Camera.lua @@ -21,18 +21,26 @@ function Camera:initShake () } end +function Camera:push () + love.graphics.push() +end + +function Camera:scale (scale) + scale = scale or getScale() + love.graphics.scale(scale, scale) +end + -- TODO: Even more magic numbers present in Camera. Translate method. function Camera:translate (ratio) - local x, y = self:getPositionScaled() - local dx, dy = self:getShakeScaled() + local px, py = self:getPosition() + local dx, dy = self:getShake() if ratio then dx = dx * ratio dy = dy * ratio - x = x * ratio - y = y * ratio + px = px * ratio + py = py * ratio end - love.graphics.push() - love.graphics.translate(160*getScale() - x - dx, 90*getScale() - y - dy) + love.graphics.translate(160 - px - dx, 90 - py - dy) end function Camera:pop () -- cgit v1.1 From 343ad9fcafc062dc7ddf181992863d6137628ceb Mon Sep 17 00:00:00 2001 From: Aki Date: Tue, 12 Sep 2017 02:03:14 +0200 Subject: Removed obsolete scaled methods from Camera --- not/Camera.lua | 27 --------------------------- 1 file changed, 27 deletions(-) (limited to 'not/Camera.lua') diff --git a/not/Camera.lua b/not/Camera.lua index c3aaae6..c5449d3 100644 --- a/not/Camera.lua +++ b/not/Camera.lua @@ -57,33 +57,12 @@ function Camera:getPosition () return self.x, self.y end -function Camera:getPositionScaled () - local scale = getScale() - return self.x * scale, self.y * scale -end - -- TODO: Magic numbers present in camera's boundaries. function Camera:getBoundaries () local x, y = self:getPosition() return x - 160, y - 90, x + 160, y + 90 end -function Camera:getBoundariesScaled () - local x, y = self:getPositionScaled() - local width, height = love.graphics.getDimensions() - width = width / 2 - height = height / 2 - return x - width, y - height, x + width, y + height -end - --- TODO: Camera@scalePoints is left because PhysicalBody still uses it as love.graphics.scale is not used yet. -function Camera:scalePoints (...) - local a, r, scale = {...}, {}, getScale() - for _,v in pairs(a) do - table.insert(r, v * scale) - end - return r -end function Camera:startShake () self.shakeTime = Camera.SHAKE_LENGTH @@ -124,12 +103,6 @@ function Camera:getShake () return radius * math.cos(theta), radius * math.sin(theta) end -function Camera:getShakeScaled () - local x, y = self:getShake() - local scale = getScale() - return x * scale, y * scale -end - function Camera:resetSum () self.sumX = 0 self.sumY = 0 -- cgit v1.1 From 2a0cef0fba687e03fd322ac73c511bbb12b1e31c Mon Sep 17 00:00:00 2001 From: Aki Date: Wed, 13 Sep 2017 10:00:59 +0200 Subject: Fixed background bug on non-16:9 screens --- not/Camera.lua | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) (limited to 'not/Camera.lua') diff --git a/not/Camera.lua b/not/Camera.lua index c5449d3..402a32b 100644 --- a/not/Camera.lua +++ b/not/Camera.lua @@ -1,4 +1,5 @@ --- Used in drawing other stuff in places. +-- TODO: Support for real scale translations. Camera = require "not.Object":extends() Camera.SHAKE_LENGTH = 0.6 @@ -25,26 +26,29 @@ function Camera:push () love.graphics.push() end +-- TODO: self._scale usage is temporary, used for real scaling. function Camera:scale (scale) scale = scale or getScale() love.graphics.scale(scale, scale) + self._scale = scale end --- TODO: Even more magic numbers present in Camera. Translate method. function Camera:translate (ratio) local px, py = self:getPosition() local dx, dy = self:getShake() + local ox, oy = self:getHalfViewSize() if ratio then dx = dx * ratio dy = dy * ratio px = px * ratio py = py * ratio end - love.graphics.translate(160 - px - dx, 90 - py - dy) + love.graphics.translate(ox - px - dx, oy - py - dy) end function Camera:pop () love.graphics.pop() + self._scale = nil end function Camera:setPosition (x, y) @@ -57,12 +61,24 @@ function Camera:getPosition () return self.x, self.y end --- TODO: Magic numbers present in camera's boundaries. function Camera:getBoundaries () local x, y = self:getPosition() - return x - 160, y - 90, x + 160, y + 90 + local width, height = self:getHalfViewSize() + return x - width, y - height, x + width, y + height end +-- TODO: Review getViewSize of Camera. +function Camera:getViewSize () + local scale = self._scale or getScale() + local width = love.graphics.getWidth() / scale + local height = love.graphics.getHeight() / scale + return width, height +end + +function Camera:getHalfViewSize () + local width, height = self:getViewSize() + return width / 2, height / 2 +end function Camera:startShake () self.shakeTime = Camera.SHAKE_LENGTH -- cgit v1.1 From e8bd3dfd8031930fa3f0169300620e5a0400995d Mon Sep 17 00:00:00 2001 From: Aki Date: Wed, 13 Sep 2017 10:19:02 +0200 Subject: Another attempt to fix real scaling bug --- not/Camera.lua | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) (limited to 'not/Camera.lua') diff --git a/not/Camera.lua b/not/Camera.lua index 402a32b..b3b8128 100644 --- a/not/Camera.lua +++ b/not/Camera.lua @@ -26,11 +26,9 @@ function Camera:push () love.graphics.push() end --- TODO: self._scale usage is temporary, used for real scaling. function Camera:scale (scale) scale = scale or getScale() love.graphics.scale(scale, scale) - self._scale = scale end function Camera:translate (ratio) @@ -46,6 +44,20 @@ function Camera:translate (ratio) love.graphics.translate(ox - px - dx, oy - py - dy) end +-- TODO: TranslateReal is temporary. +function Camera:translateReal (ratio) + local px, py = self:getPosition() + local dx, dy = self:getShake() + local ox, oy = self:getHalfViewSize(getRealScale()) + if ratio then + dx = dx * ratio + dy = dy * ratio + px = px * ratio + py = py * ratio + end + love.graphics.translate(ox - px - dx, oy - py - dy) +end + function Camera:pop () love.graphics.pop() self._scale = nil @@ -68,15 +80,15 @@ function Camera:getBoundaries () end -- TODO: Review getViewSize of Camera. -function Camera:getViewSize () - local scale = self._scale or getScale() +function Camera:getViewSize (scale) + scale = scale or getScale() local width = love.graphics.getWidth() / scale local height = love.graphics.getHeight() / scale return width, height end -function Camera:getHalfViewSize () - local width, height = self:getViewSize() +function Camera:getHalfViewSize (scale) + local width, height = self:getViewSize(scale) return width / 2, height / 2 end -- cgit v1.1 From f34ec5f8f29ce07d69104e537e87ed357abaf786 Mon Sep 17 00:00:00 2001 From: Aki Date: Wed, 13 Sep 2017 12:50:46 +0200 Subject: Camera and Layers now properly handle different resolution ratios --- not/Camera.lua | 55 ++++++++++--------------------------------------------- 1 file changed, 10 insertions(+), 45 deletions(-) (limited to 'not/Camera.lua') diff --git a/not/Camera.lua b/not/Camera.lua index b3b8128..65395cb 100644 --- a/not/Camera.lua +++ b/not/Camera.lua @@ -1,5 +1,5 @@ --- Used in drawing other stuff in places. --- TODO: Support for real scale translations. +-- TODO: Camera is missing documentation on every important method. Camera = require "not.Object":extends() Camera.SHAKE_LENGTH = 0.6 @@ -26,41 +26,19 @@ function Camera:push () love.graphics.push() end -function Camera:scale (scale) - scale = scale or getScale() - love.graphics.scale(scale, scale) -end - -function Camera:translate (ratio) +function Camera:transform (scale, ratio, vw, vh) local px, py = self:getPosition() - local dx, dy = self:getShake() - local ox, oy = self:getHalfViewSize() - if ratio then - dx = dx * ratio - dy = dy * ratio - px = px * ratio - py = py * ratio - end - love.graphics.translate(ox - px - dx, oy - py - dy) -end + local sx, sy = self:getShake() + local dx, dy = (px + sx) * ratio, (py + sy) * ratio --- TODO: TranslateReal is temporary. -function Camera:translateReal (ratio) - local px, py = self:getPosition() - local dx, dy = self:getShake() - local ox, oy = self:getHalfViewSize(getRealScale()) - if ratio then - dx = dx * ratio - dy = dy * ratio - px = px * ratio - py = py * ratio - end - love.graphics.translate(ox - px - dx, oy - py - dy) + vw, vh = vw / scale / 2, vh / scale / 2 + + love.graphics.scale(scale, scale) + love.graphics.translate(vw - dx, vh - dy) end function Camera:pop () love.graphics.pop() - self._scale = nil end function Camera:setPosition (x, y) @@ -73,25 +51,12 @@ function Camera:getPosition () return self.x, self.y end -function Camera:getBoundaries () +function Camera:getBoundaries (scale, vw, vh) local x, y = self:getPosition() - local width, height = self:getHalfViewSize() + local width, height = vw / scale / 2, vh / scale / 2 return x - width, y - height, x + width, y + height end --- TODO: Review getViewSize of Camera. -function Camera:getViewSize (scale) - scale = scale or getScale() - local width = love.graphics.getWidth() / scale - local height = love.graphics.getHeight() / scale - return width, height -end - -function Camera:getHalfViewSize (scale) - local width, height = self:getViewSize(scale) - return width / 2, height / 2 -end - function Camera:startShake () self.shakeTime = Camera.SHAKE_LENGTH end -- cgit v1.1