Merge branch 'fix-cameras' of game-projects/gamecore into master

This commit is contained in:
Kazhnuz 2019-07-15 10:17:21 +02:00 committed by Gitea
commit 26ff29b8aa
8 changed files with 215 additions and 398 deletions

View file

@ -31,6 +31,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- **examples:** Test all player number and camera types - **examples:** Test all player number and camera types
- **core.screen:** Add a way to get coordinate and scale
### Changed ### Changed
- **world2D:** Use a list for bodies (hitboxes, etc) and one other for actors - **world2D:** Use a list for bodies (hitboxes, etc) and one other for actors
@ -47,18 +49,26 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- **examples:** Rename "movable" to "topdown" - **examples:** Rename "movable" to "topdown"
- **camera:** Brand new camera system based on canvases instead of just transforms
### Fixed ### Fixed
- **world:** Remove a forgotten camera debug function - **world:** Remove a forgotten camera debug function
- **examples:** Add missing translations - **examples:** Add missing translations
- **camera:** Fix onscreen coordinates by using new core.screen func
- **camera:** Fix camera's drawing in other resolution mode
### Removed ### Removed
- **actor:** Remove all function related to XGravity - **actor:** Remove all function related to XGravity
- **examples:** Remove basic examples - **examples:** Remove basic examples
- **libs:** Remove hump.camera
## 0.5.1 - 2019-06-21 ## 0.5.1 - 2019-06-21
### Fixed ### Fixed

View file

@ -87,6 +87,14 @@ function CScreen.project(x, y)
return math.floor((x - tx) / fsv), math.floor((y - ty) / fsv) return math.floor((x - tx) / fsv), math.floor((y - ty) / fsv)
end end
function CScreen.getScale()
return fsv
end
function CScreen.getScreenCoordinate(x, y)
return math.floor((x + tx) * fsv), math.floor((y + ty) * fsv)
end
-- Change letterbox color -- Change letterbox color
function CScreen.setColor(r, g, b, a) function CScreen.setColor(r, g, b, a)
cr = r cr = r

View file

@ -40,7 +40,7 @@ function Box3D:new(owner, w, h, d)
self.texture = {} self.texture = {}
self:setTopTexture() self:setTopTexture()
self:setBottomTexture() self:setBottomTexture()
self.texture.shadows = nil self.texture.shadows = love.graphics.newCanvas(self.w, self.h)
self:register() self:register()
end end
@ -123,19 +123,14 @@ end
function Box3D:redrawShadowCanvas() function Box3D:redrawShadowCanvas()
if (self.needRedraw) then if (self.needRedraw) then
local canvas = love.graphics.newCanvas(self.w, self.h) love.graphics.setCanvas( self.texture.shadows )
love.graphics.setCanvas( canvas ) love.graphics.clear()
for i,v in ipairs(self.shadowSources) do for i,v in ipairs(self.shadowSources) do
v.actor:drawShadow(v.x, v.y) v.actor:drawShadow(v.x, v.y)
end end
love.graphics.setCanvas( ) love.graphics.setCanvas( )
local imagedata = canvas:newImageData()
self.texture.shadows = love.graphics.newImage( imagedata )
imagedata:release()
canvas:release()
self.needRedraw = false self.needRedraw = false
end end
end end

View file

@ -431,13 +431,10 @@ function BaseWorld:draw(dt)
self:drawActors() self:drawActors()
else else
for i=1, camNumber do for i=1, camNumber do
self.cameras:setScissor(i)
self:drawMap(i)
self.cameras:attachView(i) self.cameras:attachView(i)
self:drawMap(i)
self:drawActors(i) self:drawActors(i)
self.cameras:detachView(i) self.cameras:detachView(i)
self.cameras:drawHUD(i)
self.cameras:resetScissor( )
end end
end end
end end
@ -451,21 +448,12 @@ function BaseWorld:drawActors(id)
end end
function BaseWorld:drawMap(id) function BaseWorld:drawMap(id)
local tx, ty, scale = 0, 0, 1
if id ~= nil then
-- Du à la manière dont fonctionne STI, on est obligé de récupérer les info
-- de position de camera pour afficher la carte par rapport à ces infos
tx, ty = self.cameras:getInternalCamCoordinate(id)
scale = self.cameras:getViewScale(id) or 2
local vx, vy = self.cameras:getOnScreenViewRelativePosition(id)
tx = math.floor(tx - math.abs(vx))
ty = math.floor(ty - math.abs(vy))
local w, h = core.screen:getDimensions()
end
if self.haveMap then if self.haveMap then
self.map:draw(-tx, -ty, scale, scale) for _, layer in ipairs(self.map.layers) do
if layer.visible and layer.opacity > 0 then
self.map:drawLayer(layer)
end
end
end end
end end

View file

@ -1,5 +1,4 @@
-- camera.lua :: a basic camera adapted to the asset/world system. -- camera.lua :: a basic camera adapted to the asset/world system.
-- Use hump.camera as the view backend.
--[[ --[[
Copyright © 2019 Kazhnuz Copyright © 2019 Kazhnuz
@ -22,10 +21,12 @@
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
]] ]]
local cwd = (...):gsub('%.camera$', '') .. "." local cwd = (...):gsub('%.init$', '') .. "."
local CameraSystem = Object:extend() local CameraSystem = Object:extend()
local View = require(cwd .. "libs.hump.camera") --local View = require(cwd .. "libs.hump.camera")
local camutils = require(cwd .. "utils")
local SPLITSCREEN_ISVERTICAL = false local SPLITSCREEN_ISVERTICAL = false
local SCREEN_LIMIT = 4 local SCREEN_LIMIT = 4
@ -57,43 +58,7 @@ function CameraSystem:initViews()
self.views.basewidth, self.views.baseheight = core.screen:getDimensions() self.views.basewidth, self.views.baseheight = core.screen:getDimensions()
self.views.width, self.views.height = self:getViewsDimensions() self.views.width, self.views.height = self:getViewsDimensions()
self.views.posList = {} self.views.posList = camutils.getViewsPositions(self.views.basewidth, self.views.baseheight, self.verticalSplit)
self.views.posList.dual = {}
self.views.posList.multi = {}
if (self.verticalSplit) then
self.views.posList.dual[1] = {}
self.views.posList.dual[1].x = 0
self.views.posList.dual[1].y = (self.views.baseheight/4)
self.views.posList.dual[2] = {}
self.views.posList.dual[2].x = 0
self.views.posList.dual[2].y = -(self.views.baseheight/4)
else
self.views.posList.dual[1] = {}
self.views.posList.dual[1].x = -(self.views.basewidth/4)
self.views.posList.dual[1].y = 0
self.views.posList.dual[2] = {}
self.views.posList.dual[2].x = (self.views.basewidth/4)
self.views.posList.dual[2].y = 0
end
self.views.posList.multi[1] = {}
self.views.posList.multi[1].x = -(self.views.basewidth /4)
self.views.posList.multi[1].y = -(self.views.baseheight/4)
self.views.posList.multi[2] = {}
self.views.posList.multi[2].x = (self.views.basewidth /4)
self.views.posList.multi[2].y = -(self.views.baseheight/4)
self.views.posList.multi[3] = {}
self.views.posList.multi[3].x = -(self.views.basewidth /4)
self.views.posList.multi[3].y = (self.views.baseheight/4)
self.views.posList.multi[4] = {}
self.views.posList.multi[4].x = (self.views.basewidth /4)
self.views.posList.multi[4].y = (self.views.baseheight/4)
end end
-- INFO FUNCTIONS -- INFO FUNCTIONS
@ -107,32 +72,22 @@ function CameraSystem:haveView()
return (self:getViewNumber() == 0) return (self:getViewNumber() == 0)
end end
function CameraSystem:getViewsDimensions(viewNumber) function CameraSystem:getViewsDimensions()
local basewidth, baseheight = self.views.basewidth, self.views.baseheight local basewidth, baseheight = self.views.basewidth, self.views.baseheight
local viewnumber = viewNumber or self:getViewNumber() local viewnumber = self:getViewNumber()
if (viewnumber <= 1) then return camutils.getViewsDimensions(viewnumber, basewidth, baseheight, self.verticalSplit)
return basewidth, baseheight
elseif (viewnumber == 2) then
if (self.verticalSplit) then
return (basewidth), (baseheight/2)
else
return (basewidth/2), (baseheight)
end
else
return (basewidth/2), (baseheight/2)
end
end end
function CameraSystem:recalculateViewsPositions() function CameraSystem:recalculateViewsPositions()
if #self.views.list == 1 then if #self.views.list == 1 then
self.views.list[1].pos.onScreen.x = 0 self.views.list[1].onScreen.x = 0
self.views.list[1].pos.onScreen.y = 0 self.views.list[1].onScreen.y = 0
else else
for i,v in ipairs(self.views.list) do for i,v in ipairs(self.views.list) do
local x, y = self:getViewPositions(i) local x, y = self:getViewPositions(i)
self.views.list[i].pos.onScreen.x = x self.views.list[i].onScreen.x = x
self.views.list[i].pos.onScreen.y = y self.views.list[i].onScreen.y = y
end end
end end
end end
@ -169,92 +124,93 @@ function CameraSystem:addView(x, y, target)
local view = {} local view = {}
view.pos = {} view.pos = {}
view.pos.x = x or 0 view.x = x or 0
view.pos.y = y or 0 view.y = y or 0
view.pos.onScreen = {} view.scale = 1
view.onScreen = {}
view.cam = View(view.pos.x, view.pos.y, 1, 0, true)
-- TODO: add a target system in order to make a camera able -- TODO: add a target system in order to make a camera able
-- to target a specific object -- to target a specific object
view.target = target view.target = target
table.insert(self.views.list, view) table.insert(self.views.list, view)
self.views.width, self.views.height = self:getViewsDimensions() self.views.width, self.views.height = self:getViewsDimensions()
self:regenerateCanvases()
self:recalculateViewsPositions() self:recalculateViewsPositions()
end end
end end
function CameraSystem:regenerateCanvases()
for i, view in ipairs(self.views.list) do
view.canvas = love.graphics.newCanvas(self.views.width, self.views.height)
end
end
function CameraSystem:getView(id) function CameraSystem:getView(id)
return self.views.list[id] return self.views.list[id]
end end
function CameraSystem:getViewCam(id)
local view = self:getView(id)
return view.cam
end
function CameraSystem:setScissor(id)
local viewx, viewy, vieww, viewh = self:getOnScreenViewCoordinate(id)
if (self:getViewNumber() > 2) or (self:getViewNumber() == 2 and SPLITSCREEN_ISVERTICAL) then
-- FIXME: it's an ugly workaround that need to be fixed. For an unkown
-- reason, the scissoring is wrong in and only in this function and only
-- when we have vertical split. Thus, we need to substract viewy to viewy
viewy = viewh - viewy
end
if (self.mode == "split") then
love.graphics.setScissor(viewx, viewy, vieww, viewh)
end
end
function CameraSystem:resetScissor( )
if (self.mode == "split") then
love.graphics.setScissor( )
end
end
function CameraSystem:attachView(id) function CameraSystem:attachView(id)
if (id ~= nil) then if (id ~= nil) then
local cam = self:getViewCam(id) local view = self:getView(id)
cam:attach() self.current_canvas = love.graphics.getCanvas()
love.graphics.setCanvas(view.canvas)
love.graphics.clear()
if id ~= nil then
-- Du à la manière dont fonctionne STI, on est obligé de récupérer les info
-- de position de camera pour afficher la carte par rapport à ces infos
tx, ty = self:getViewCoordinate(id)
scale = self:getViewScale(id) or 1
tx = math.floor(tx) * -1
ty = math.floor(ty) * -1
local w, h = core.screen:getDimensions()
end
love.graphics.push()
love.graphics.origin()
love.graphics.translate(math.floor(tx), math.floor(ty))
end end
end end
function CameraSystem:detachView(id) function CameraSystem:detachView(id)
if (id ~= nil) then if (id ~= nil) then
local cam = self:getViewCam(id) local view = self:getView(id)
love.graphics.pop()
cam:detach() love.graphics.setCanvas(self.current_canvas)
local tx, ty = self:getOnScreenViewCoordinate(id)
local scale = core.screen:getScale() * view.scale
love.graphics.push()
love.graphics.origin()
love.graphics.translate(math.floor(tx), math.floor(ty))
love.graphics.scale(scale, scale)
love.graphics.draw(view.canvas)
local unscale = 1 / view.scale
love.graphics.scale(unscale, unscale)
self:drawHUD(id)
love.graphics.pop()
end end
end end
function CameraSystem:getViewCoordinate(id) function CameraSystem:getViewCoordinate(id)
local view = self:getView(id) local view = self:getView(id)
local cam = self:getViewCam(id)
local viewx, viewy, vieww, viewh local viewx, viewy, vieww, viewh
viewx = view.pos.x - ((self.views.width/2) / cam.scale)
viewy = view.pos.y - ((self.views.height/2) / cam.scale)
vieww = self.views.width / cam.scale vieww = self.views.width / view.scale
viewh = self.views.height / cam.scale viewh = self.views.height / view.scale
return viewx, viewy, vieww, viewh
end
function CameraSystem:getInternalCamCoordinate(id) viewx = view.x - (vieww / 2)
local view = self:getView(id) viewy = view.y - (viewh / 2)
local cam = self:getViewCam(id)
local viewx, viewy, vieww, viewh
viewx = cam.x - ((self.views.width/2) / cam.scale)
viewy = cam.y - ((self.views.height/2) / cam.scale)
vieww, viewh = core.screen:getDimensions()
vieww = vieww / cam.scale
viewh = viewh / cam.scale
return viewx, viewy, vieww, viewh return viewx, viewy, vieww, viewh
end end
@ -263,11 +219,13 @@ function CameraSystem:getOnScreenViewCoordinate(id)
local viewx, viewy, vieww, viewh local viewx, viewy, vieww, viewh
local basex, basey = (self.views.basewidth / 2), (self.views.baseheight / 2) local basex, basey = (self.views.basewidth / 2), (self.views.baseheight / 2)
viewx = (basex) + view.pos.onScreen.x - (self.views.width / 2) viewx = (basex) + view.onScreen.x - (self.views.width / 2)
viewy = (basey) + view.pos.onScreen.y - (self.views.height / 2) viewy = (basey) + view.onScreen.y - (self.views.height / 2)
vieww = self.views.width viewx, viewy = core.screen:getScreenCoordinate(viewx, viewy)
viewh = self.views.height
vieww = self.views.width * core.screen:getScale()
viewh = self.views.height * core.screen:getScale()
return viewx, viewy, vieww, viewh return viewx, viewy, vieww, viewh
end end
@ -276,10 +234,10 @@ function CameraSystem:getOnScreenViewRelativePosition(id)
local viewx, viewy local viewx, viewy
local basex, basey = (self.views.basewidth / 2), (self.views.baseheight / 2) local basex, basey = (self.views.basewidth / 2), (self.views.baseheight / 2)
viewx = view.pos.onScreen.x viewx = view.onScreen.x
viewy = view.pos.onScreen.y viewy = view.onScreen.y
return viewx, viewy return core.screen:getScreenCoordinate(viewx, viewy)
end end
function CameraSystem:getOnScreenViewCenter(id) function CameraSystem:getOnScreenViewCenter(id)
@ -287,32 +245,40 @@ function CameraSystem:getOnScreenViewCenter(id)
local viewx, viewy local viewx, viewy
local basex, basey = (self.views.basewidth / 2), (self.views.baseheight / 2) local basex, basey = (self.views.basewidth / 2), (self.views.baseheight / 2)
viewx = (basex) + view.pos.onScreen.x viewx = (basex) + view.onScreen.x
viewy = (basey) + view.pos.onScreen.y viewy = (basey) + view.onScreen.y
return viewx, viewy return core.screen:getScreenCoordinate(viewx, viewy)
end
function CameraSystem:setViewScale(id, scale)
local view = self:getView(id)
view.scale = scale
local _, _, w, h = self:getViewCoordinate(id)
view.canvas = love.graphics.newCanvas(math.ceil(w), math.ceil(h))
end end
function CameraSystem:getViewScale(id) function CameraSystem:getViewScale(id)
local cam = self:getViewCam(id) local view = self:getView(id)
return cam.scale return view.scale
end end
function CameraSystem:limitView(id) function CameraSystem:limitView(id)
local viewx, viewy, vieww, viewh = self:getViewCoordinate(id) local viewx, viewy, vieww, viewh = self:getViewCoordinate(id)
local worldw, worldh = self.world:getDimensions() local worldw, worldh = self.world:getDimensions()
local posx = self.views.list[id].pos.x local posx = self.views.list[id].x
local posy = self.views.list[id].pos.y local posy = self.views.list[id].y
local minx = self.views.width / 2 local minx = self.views.width / 2
local miny = self.views.height / 2 local miny = self.views.height / 2
local maxx = worldw - minx local maxx = worldw - minx
local maxy = worldh - miny local maxy = worldh - miny
self.views.list[id].pos.x = utils.math.between(posx, minx, maxx) self.views.list[id].x = utils.math.between(posx, minx, maxx)
self.views.list[id].pos.y = utils.math.between(posy, miny, maxy) self.views.list[id].y = utils.math.between(posy, miny, maxy)
self:computeCamPosition(id)
end end
-- UPDATE and MOVE functions -- UPDATE and MOVE functions
@ -329,24 +295,12 @@ function CameraSystem:update(dt)
end end
function CameraSystem:moveView(id, x, y) function CameraSystem:moveView(id, x, y)
self.views.list[id].pos.x = x self.views.list[id].x = x
self.views.list[id].pos.y = y self.views.list[id].y = y
self:computeCamPosition(id)
self:limitView(id) self:limitView(id)
end end
function CameraSystem:computeCamPosition(id)
local decalx = self.views.list[id].pos.onScreen.x
local decaly = self.views.list[id].pos.onScreen.y
local realx = self.views.list[id].pos.x
local realy = self.views.list[id].pos.y
self.views.list[id].cam.x = realx - decalx
self.views.list[id].cam.y = realy - decaly
end
function CameraSystem:followActor(id) function CameraSystem:followActor(id)
local view = self:getView(id) local view = self:getView(id)
@ -397,7 +351,7 @@ function CameraSystem:followAllActors(id)
local scalex = (maxX-minX)/ww local scalex = (maxX-minX)/ww
local scaley = (maxY-minY)/hh local scaley = (maxY-minY)/hh
scale = math.max(scale, scalex, scaley) scale = math.max(scale, scalex, scaley)
view.cam.scale = 1/scale self:setViewScale(id, 1/scale)
self.world:resizeMap(ww * 3, hh * 3) self.world:resizeMap(ww * 3, hh * 3)
end end
@ -409,28 +363,11 @@ end
-- DRAW FUNCTIONS -- DRAW FUNCTIONS
-- Basic callback to draw stuff -- Basic callback to draw stuff
function CameraSystem:drawDebugViewBox(id)
local viewx, viewy, vieww, viewh = self:getOnScreenViewCoordinate(id)
utils.graphics.box(viewx, viewy, vieww, viewh)
local xx, yy = self:getOnScreenViewCenter(id)
love.graphics.line(xx-3, yy, xx+3, yy)
love.graphics.line(xx, yy-3, xx, yy+3)
local xx, yy = self:getInternalCamCoordinate(id)
local string = id .. " x:" .. xx .. " y:" .. yy
love.graphics.print(string, viewx + 4, viewy + 4)
end
function CameraSystem:drawHUD(id) function CameraSystem:drawHUD(id)
local viewx, viewy, vieww, viewh = self:getOnScreenViewCoordinate(id)
local view = self:getView(id) local view = self:getView(id)
local string2 = id .. " (" .. viewx .. ":" .. (viewh-viewy) .. ") " local viewx, viewy, vieww, viewh = self:getOnScreenViewCoordinate(id)
love.graphics.translate(viewx, viewy)
view.target:drawHUD(id, vieww, viewh) view.target:drawHUD(id, vieww, viewh)
love.graphics.translate(-viewx, -(viewy))
end end
return CameraSystem return CameraSystem

View file

@ -0,0 +1,83 @@
-- camutils.lua :: some camera utilities
--[[
Copyright © 2019 Kazhnuz
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
]]
local camutils = {}
function camutils.getViewsPositions(basewidth, baseheight, verticalSplit)
local posList = {}
posList.dual = {}
posList.multi = {}
if (verticalSplit) then
posList.dual[1] = {}
posList.dual[1].x = 0
posList.dual[1].y = (baseheight/4)
posList.dual[2] = {}
posList.dual[2].x = 0
posList.dual[2].y = -(baseheight/4)
else
posList.dual[1] = {}
posList.dual[1].x = -(basewidth/4)
posList.dual[1].y = 0
posList.dual[2] = {}
posList.dual[2].x = (basewidth/4)
posList.dual[2].y = 0
end
posList.multi[1] = {}
posList.multi[1].x = -(basewidth /4)
posList.multi[1].y = -(baseheight/4)
posList.multi[2] = {}
posList.multi[2].x = (basewidth /4)
posList.multi[2].y = -(baseheight/4)
posList.multi[3] = {}
posList.multi[3].x = -(basewidth /4)
posList.multi[3].y = (baseheight/4)
posList.multi[4] = {}
posList.multi[4].x = (basewidth /4)
posList.multi[4].y = (baseheight/4)
return posList
end
function camutils.getViewsDimensions(viewnumber, basewidth, baseheight, verticalSplit)
if (viewnumber <= 1) then
return basewidth, baseheight
elseif (viewnumber == 2) then
if (verticalSplit) then
return (basewidth), (baseheight/2)
else
return (basewidth/2), (baseheight)
end
else
return (basewidth/2), (baseheight/2)
end
end
return camutils

View file

@ -1,212 +0,0 @@
--[[
Copyright (c) 2010-2015 Matthias Richter
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
Except as contained in this notice, the name(s) of the above copyright holders
shall not be used in advertising or otherwise to promote the sale, use or
other dealings in this Software without prior written authorization.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
]]--
local _PATH = (...):match('^(.*[%./])[^%.%/]+$') or ''
local cos, sin = math.cos, math.sin
local camera = {}
camera.__index = camera
-- Movement interpolators (for camera locking/windowing)
camera.smooth = {}
function camera.smooth.none()
return function(dx,dy) return dx,dy end
end
function camera.smooth.linear(speed)
assert(type(speed) == "number", "Invalid parameter: speed = "..tostring(speed))
return function(dx,dy, s)
-- normalize direction
local d = math.sqrt(dx*dx+dy*dy)
local dts = math.min((s or speed) * love.timer.getDelta(), d) -- prevent overshooting the goal
if d > 0 then
dx,dy = dx/d, dy/d
end
return dx*dts, dy*dts
end
end
function camera.smooth.damped(stiffness)
assert(type(stiffness) == "number", "Invalid parameter: stiffness = "..tostring(stiffness))
return function(dx,dy, s)
local dts = love.timer.getDelta() * (s or stiffness)
return dx*dts, dy*dts
end
end
local function new(x,y, zoom, rot, smoother)
x,y = x or love.graphics.getWidth()/2, y or love.graphics.getHeight()/2
zoom = zoom or 1
rot = rot or 0
smoother = smoother or camera.smooth.none() -- for locking, see below
return setmetatable({x = x, y = y, scale = zoom, rot = rot, smoother = smoother}, camera)
end
function camera:lookAt(x,y)
self.x, self.y = x, y
return self
end
function camera:move(dx,dy)
self.x, self.y = self.x + dx, self.y + dy
return self
end
function camera:position()
return self.x, self.y
end
function camera:rotate(phi)
self.rot = self.rot + phi
return self
end
function camera:rotateTo(phi)
self.rot = phi
return self
end
function camera:zoom(mul)
self.scale = self.scale * mul
return self
end
function camera:zoomTo(zoom)
self.scale = zoom
return self
end
function camera:attach(x,y,w,h, noclip)
x,y = x or 0, y or 0
w,h = w or love.graphics.getWidth(), h or love.graphics.getHeight()
self._sx,self._sy,self._sw,self._sh = love.graphics.getScissor()
local cx,cy = x+w/2, y+h/2
love.graphics.push()
love.graphics.translate(cx, cy)
love.graphics.scale(self.scale)
love.graphics.rotate(self.rot)
love.graphics.translate(-self.x, -self.y)
end
function camera:detach()
love.graphics.pop()
end
function camera:draw(...)
local x,y,w,h,noclip,func
local nargs = select("#", ...)
if nargs == 1 then
func = ...
elseif nargs == 5 then
x,y,w,h,func = ...
elseif nargs == 6 then
x,y,w,h,noclip,func = ...
else
error("Invalid arguments to camera:draw()")
end
self:attach(x,y,w,h,noclip)
func()
self:detach()
end
-- world coordinates to camera coordinates
function camera:cameraCoords(x,y, ox,oy,w,h)
ox, oy = ox or 0, oy or 0
w,h = w or love.graphics.getWidth(), h or love.graphics.getHeight()
-- x,y = ((x,y) - (self.x, self.y)):rotated(self.rot) * self.scale + center
local c,s = cos(self.rot), sin(self.rot)
x,y = x - self.x, y - self.y
x,y = c*x - s*y, s*x + c*y
return x*self.scale + w/2 + ox, y*self.scale + h/2 + oy
end
-- camera coordinates to world coordinates
function camera:worldCoords(x,y, ox,oy,w,h)
ox, oy = ox or 0, oy or 0
w,h = w or love.graphics.getWidth(), h or love.graphics.getHeight()
-- x,y = (((x,y) - center) / self.scale):rotated(-self.rot) + (self.x,self.y)
local c,s = cos(-self.rot), sin(-self.rot)
x,y = (x - w/2 - ox) / self.scale, (y - h/2 - oy) / self.scale
x,y = c*x - s*y, s*x + c*y
return x+self.x, y+self.y
end
function camera:mousePosition(ox,oy,w,h)
local mx,my = love.mouse.getPosition()
return self:worldCoords(mx,my, ox,oy,w,h)
end
-- camera scrolling utilities
function camera:lockX(x, smoother, ...)
local dx, dy = (smoother or self.smoother)(x - self.x, self.y, ...)
self.x = self.x + dx
return self
end
function camera:lockY(y, smoother, ...)
local dx, dy = (smoother or self.smoother)(self.x, y - self.y, ...)
self.y = self.y + dy
return self
end
function camera:lockPosition(x,y, smoother, ...)
return self:move((smoother or self.smoother)(x - self.x, y - self.y, ...))
end
function camera:lockWindow(x, y, x_min, x_max, y_min, y_max, smoother, ...)
-- figure out displacement in camera coordinates
x,y = self:cameraCoords(x,y)
local dx, dy = 0,0
if x < x_min then
dx = x - x_min
elseif x > x_max then
dx = x - x_max
end
if y < y_min then
dy = y - y_min
elseif y > y_max then
dy = y - y_max
end
-- transform displacement to movement in world coordinates
local c,s = cos(-self.rot), sin(-self.rot)
dx,dy = (c*dx - s*dy) / self.scale, (s*dx + c*dy) / self.scale
-- move
self:move((smoother or self.smoother)(dx,dy,...))
end
-- the module
return setmetatable({new = new, smooth = camera.smooth},
{__call = function(_, ...) return new(...) end})

View file

@ -66,6 +66,14 @@ function ScreenManager:getMousePosition()
return CScreen.project(love.mouse.getX(), love.mouse.getY()) return CScreen.project(love.mouse.getX(), love.mouse.getY())
end end
function ScreenManager:getScale()
return CScreen.getScale()
end
function ScreenManager:getScreenCoordinate(x, y)
return CScreen.getScreenCoordinate(x, y)
end
-- INFO FUNCTIONS -- INFO FUNCTIONS
-- Get screen informations -- Get screen informations