2019-04-29 08:44:10 +02:00
|
|
|
-- camera.lua :: a basic camera adapted to the asset/world system.
|
|
|
|
-- Use hump.camera as the view backend.
|
|
|
|
|
|
|
|
--[[
|
|
|
|
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 cwd = (...):gsub('%.camera$', '') .. "."
|
|
|
|
|
|
|
|
local CameraSystem = Object:extend()
|
|
|
|
local View = require(cwd .. "libs.hump.camera")
|
|
|
|
|
2019-04-29 10:42:43 +02:00
|
|
|
local SPLITSCREEN_ISVERTICAL = false
|
|
|
|
local SCREEN_LIMIT = 4
|
2019-04-29 08:44:10 +02:00
|
|
|
|
|
|
|
-- INIT FUNCTIONS
|
|
|
|
-- Initialize the camera system
|
|
|
|
|
|
|
|
function CameraSystem:new(world)
|
|
|
|
self.scene = world.scene
|
|
|
|
self.world = world
|
|
|
|
|
2019-06-29 11:25:40 +02:00
|
|
|
self.mode = "split"
|
|
|
|
self.verticalSplit = SPLITSCREEN_ISVERTICAL
|
2019-04-29 08:44:10 +02:00
|
|
|
|
|
|
|
self:initViews()
|
|
|
|
end
|
|
|
|
|
2019-06-29 11:25:40 +02:00
|
|
|
function CameraSystem:setMode(mode)
|
|
|
|
self.mode = mode
|
|
|
|
print("Camera system mode set to " .. mode)
|
|
|
|
return mode
|
|
|
|
end
|
|
|
|
|
2019-04-29 08:44:10 +02:00
|
|
|
function CameraSystem:initViews()
|
|
|
|
self.views = {}
|
|
|
|
|
|
|
|
self.views.list = {}
|
2019-04-29 11:39:07 +02:00
|
|
|
self.views.basewidth, self.views.baseheight = core.screen:getDimensions()
|
|
|
|
self.views.width, self.views.height = self:getViewsDimensions()
|
2019-04-29 13:58:13 +02:00
|
|
|
|
|
|
|
self.views.posList = {}
|
|
|
|
self.views.posList.dual = {}
|
|
|
|
self.views.posList.multi = {}
|
|
|
|
|
|
|
|
if (self.verticalSplit) then
|
|
|
|
self.views.posList.dual[1] = {}
|
|
|
|
self.views.posList.dual[1].x = 0
|
2019-05-02 18:46:44 +02:00
|
|
|
self.views.posList.dual[1].y = (self.views.baseheight/4)
|
2019-04-29 13:58:13 +02:00
|
|
|
|
|
|
|
self.views.posList.dual[2] = {}
|
|
|
|
self.views.posList.dual[2].x = 0
|
2019-05-02 18:46:44 +02:00
|
|
|
self.views.posList.dual[2].y = -(self.views.baseheight/4)
|
2019-04-29 13:58:13 +02:00
|
|
|
else
|
|
|
|
self.views.posList.dual[1] = {}
|
2019-04-29 15:10:02 +02:00
|
|
|
self.views.posList.dual[1].x = -(self.views.basewidth/4)
|
2019-04-29 13:58:13 +02:00
|
|
|
self.views.posList.dual[1].y = 0
|
|
|
|
|
|
|
|
self.views.posList.dual[2] = {}
|
2019-04-29 15:10:02 +02:00
|
|
|
self.views.posList.dual[2].x = (self.views.basewidth/4)
|
2019-04-29 13:58:13 +02:00
|
|
|
self.views.posList.dual[2].y = 0
|
|
|
|
end
|
|
|
|
|
|
|
|
self.views.posList.multi[1] = {}
|
|
|
|
self.views.posList.multi[1].x = -(self.views.basewidth /4)
|
2019-06-21 18:50:45 +02:00
|
|
|
self.views.posList.multi[1].y = -(self.views.baseheight/4)
|
2019-04-29 13:58:13 +02:00
|
|
|
|
|
|
|
self.views.posList.multi[2] = {}
|
|
|
|
self.views.posList.multi[2].x = (self.views.basewidth /4)
|
2019-06-21 18:50:45 +02:00
|
|
|
self.views.posList.multi[2].y = -(self.views.baseheight/4)
|
2019-04-29 13:58:13 +02:00
|
|
|
|
|
|
|
self.views.posList.multi[3] = {}
|
|
|
|
self.views.posList.multi[3].x = -(self.views.basewidth /4)
|
2019-06-21 18:50:45 +02:00
|
|
|
self.views.posList.multi[3].y = (self.views.baseheight/4)
|
2019-04-29 13:58:13 +02:00
|
|
|
|
|
|
|
self.views.posList.multi[4] = {}
|
2019-06-21 18:50:45 +02:00
|
|
|
self.views.posList.multi[4].x = (self.views.basewidth /4)
|
|
|
|
self.views.posList.multi[4].y = (self.views.baseheight/4)
|
2019-04-29 08:44:10 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
-- INFO FUNCTIONS
|
|
|
|
-- Get informations from the camera system
|
|
|
|
|
|
|
|
function CameraSystem:getViewNumber()
|
|
|
|
return #self.views.list
|
|
|
|
end
|
|
|
|
|
|
|
|
function CameraSystem:haveView()
|
|
|
|
return (self:getViewNumber() == 0)
|
|
|
|
end
|
|
|
|
|
2019-04-29 13:58:13 +02:00
|
|
|
function CameraSystem:getViewsDimensions(viewNumber)
|
2019-04-29 11:39:07 +02:00
|
|
|
local basewidth, baseheight = self.views.basewidth, self.views.baseheight
|
2019-04-29 13:58:13 +02:00
|
|
|
local viewnumber = viewNumber or self:getViewNumber()
|
2019-04-29 11:39:07 +02:00
|
|
|
|
|
|
|
if (viewnumber <= 1) then
|
|
|
|
return basewidth, baseheight
|
|
|
|
elseif (viewnumber == 2) then
|
|
|
|
if (self.verticalSplit) then
|
2019-04-29 13:58:13 +02:00
|
|
|
return (basewidth), (baseheight/2)
|
2019-04-29 11:39:07 +02:00
|
|
|
else
|
2019-04-29 13:58:13 +02:00
|
|
|
return (basewidth/2), (baseheight)
|
2019-04-29 11:39:07 +02:00
|
|
|
end
|
|
|
|
else
|
|
|
|
return (basewidth/2), (baseheight/2)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-04-29 13:58:13 +02:00
|
|
|
function CameraSystem:recalculateViewsPositions()
|
|
|
|
if #self.views.list == 1 then
|
|
|
|
self.views.list[1].pos.onScreen.x = 0
|
|
|
|
self.views.list[1].pos.onScreen.y = 0
|
|
|
|
else
|
|
|
|
for i,v in ipairs(self.views.list) do
|
|
|
|
local x, y = self:getViewPositions(i)
|
|
|
|
self.views.list[i].pos.onScreen.x = x
|
|
|
|
self.views.list[i].pos.onScreen.y = y
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function CameraSystem:getViewPositions(id)
|
|
|
|
local viewNumber = #self.views.list
|
|
|
|
|
2019-05-02 18:47:36 +02:00
|
|
|
if (viewNumber == 2) and ((id == 1) or (id == 2)) then
|
2019-04-29 13:58:13 +02:00
|
|
|
return self.views.posList.dual[id].x, self.views.posList.dual[id].y
|
2019-05-02 18:46:44 +02:00
|
|
|
elseif (viewNumber > 2) then
|
2019-04-29 13:58:13 +02:00
|
|
|
return self.views.posList.multi[id].x, self.views.posList.multi[id].y
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2019-04-29 08:44:10 +02:00
|
|
|
-- WRAPPER and UTILS
|
|
|
|
-- Access data from the views
|
|
|
|
|
2019-06-29 11:25:40 +02:00
|
|
|
function CameraSystem:addTarget(target)
|
|
|
|
if (#self.views < SCREEN_LIMIT) then
|
|
|
|
if (#self.views.list == 0) or (self.mode == "split") then
|
|
|
|
local x, y = target:getViewCenter()
|
|
|
|
self:addView(x, y, target)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-04-29 15:03:28 +02:00
|
|
|
function CameraSystem:addView(x, y, target)
|
2019-04-29 10:47:01 +02:00
|
|
|
if (#self.views.list < SCREEN_LIMIT) then
|
2019-04-29 13:58:13 +02:00
|
|
|
local id = #self.views.list + 1
|
2019-04-29 10:42:43 +02:00
|
|
|
local view = {}
|
2019-04-29 08:44:10 +02:00
|
|
|
|
2019-04-29 13:58:13 +02:00
|
|
|
view.pos = {}
|
|
|
|
view.pos.x = x or 0
|
|
|
|
view.pos.y = y or 0
|
|
|
|
view.pos.onScreen = {}
|
|
|
|
|
|
|
|
view.cam = View(view.pos.x, view.pos.y, 1, 0, true)
|
2019-04-29 10:42:43 +02:00
|
|
|
-- TODO: add a target system in order to make a camera able
|
|
|
|
-- to target a specific object
|
2019-04-29 15:03:28 +02:00
|
|
|
view.target = target
|
2019-04-29 08:44:10 +02:00
|
|
|
|
2019-04-29 10:42:43 +02:00
|
|
|
table.insert(self.views.list, view)
|
2019-04-29 11:39:07 +02:00
|
|
|
self.views.width, self.views.height = self:getViewsDimensions()
|
2019-04-29 13:58:13 +02:00
|
|
|
self:recalculateViewsPositions()
|
2019-04-29 10:42:43 +02:00
|
|
|
end
|
2019-04-29 08:44:10 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
function CameraSystem:getView(id)
|
|
|
|
return self.views.list[id]
|
|
|
|
end
|
|
|
|
|
|
|
|
function CameraSystem:getViewCam(id)
|
|
|
|
local view = self:getView(id)
|
|
|
|
|
|
|
|
return view.cam
|
|
|
|
end
|
|
|
|
|
|
|
|
function CameraSystem:attachView(id)
|
2019-04-29 08:45:48 +02:00
|
|
|
if (id ~= nil) then
|
|
|
|
local cam = self:getViewCam(id)
|
2019-04-29 08:44:10 +02:00
|
|
|
|
2019-04-29 14:12:04 +02:00
|
|
|
local viewx, viewy, vieww, viewh = self:getOnScreenViewCoordinate(id)
|
2019-04-29 08:45:48 +02:00
|
|
|
cam:attach()
|
2019-06-21 18:50:45 +02:00
|
|
|
|
|
|
|
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
|
|
|
|
|
2019-04-29 14:12:04 +02:00
|
|
|
love.graphics.setScissor(viewx, viewy, vieww, viewh)
|
2019-04-29 08:45:48 +02:00
|
|
|
end
|
2019-04-29 08:44:10 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
function CameraSystem:detachView(id)
|
2019-04-29 08:45:48 +02:00
|
|
|
if (id ~= nil) then
|
|
|
|
local cam = self:getViewCam(id)
|
2019-04-29 08:44:10 +02:00
|
|
|
|
2019-04-29 14:12:04 +02:00
|
|
|
love.graphics.setScissor( )
|
2019-04-29 08:45:48 +02:00
|
|
|
cam:detach()
|
|
|
|
end
|
2019-04-29 08:44:10 +02:00
|
|
|
end
|
|
|
|
|
2019-04-29 11:20:59 +02:00
|
|
|
function CameraSystem:getViewCoordinate(id)
|
2019-06-21 18:27:02 +02:00
|
|
|
local view = self:getView(id)
|
2019-04-29 11:20:59 +02:00
|
|
|
|
2019-06-21 18:27:02 +02:00
|
|
|
local viewx, viewy, vieww, viewh
|
|
|
|
viewx = view.pos.x - (self.views.width/2)
|
|
|
|
viewy = view.pos.y - (self.views.height/2)
|
|
|
|
|
|
|
|
vieww = self.views.width
|
|
|
|
viewh = self.views.height
|
|
|
|
return viewx, viewy, vieww, viewh
|
|
|
|
end
|
2019-04-29 11:20:59 +02:00
|
|
|
|
2019-06-21 18:27:02 +02:00
|
|
|
function CameraSystem:getInternalCamCoordinate(id)
|
|
|
|
local view = self:getView(id)
|
|
|
|
local cam = self:getViewCam(id)
|
|
|
|
|
|
|
|
local viewx, viewy, vieww, viewh
|
|
|
|
viewx = cam.x - (self.views.width/2)
|
|
|
|
viewy = cam.y - (self.views.height/2)
|
|
|
|
|
|
|
|
vieww, viewh = core.screen:getDimensions()
|
|
|
|
return viewx, viewy, vieww, viewh
|
2019-04-29 11:20:59 +02:00
|
|
|
end
|
|
|
|
|
2019-04-29 13:58:13 +02:00
|
|
|
function CameraSystem:getOnScreenViewCoordinate(id)
|
|
|
|
local view = self:getView(id)
|
|
|
|
|
|
|
|
local viewx, viewy, vieww, viewh
|
|
|
|
local basex, basey = (self.views.basewidth / 2), (self.views.baseheight / 2)
|
|
|
|
viewx = (basex) + view.pos.onScreen.x - (self.views.width / 2)
|
|
|
|
viewy = (basey) + view.pos.onScreen.y - (self.views.height / 2)
|
|
|
|
|
|
|
|
vieww = self.views.width
|
|
|
|
viewh = self.views.height
|
|
|
|
return viewx, viewy, vieww, viewh
|
|
|
|
end
|
|
|
|
|
2019-05-02 16:19:59 +02:00
|
|
|
function CameraSystem:getOnScreenViewRelativePosition(id)
|
|
|
|
local view = self:getView(id)
|
|
|
|
|
|
|
|
local viewx, viewy
|
|
|
|
local basex, basey = (self.views.basewidth / 2), (self.views.baseheight / 2)
|
|
|
|
viewx = view.pos.onScreen.x
|
|
|
|
viewy = view.pos.onScreen.y
|
|
|
|
|
|
|
|
return viewx, viewy
|
|
|
|
end
|
|
|
|
|
2019-04-29 13:58:13 +02:00
|
|
|
function CameraSystem:getOnScreenViewCenter(id)
|
|
|
|
local view = self:getView(id)
|
|
|
|
|
|
|
|
local viewx, viewy
|
|
|
|
local basex, basey = (self.views.basewidth / 2), (self.views.baseheight / 2)
|
|
|
|
viewx = (basex) + view.pos.onScreen.x
|
|
|
|
viewy = (basey) + view.pos.onScreen.y
|
|
|
|
|
|
|
|
return viewx, viewy
|
|
|
|
end
|
|
|
|
|
2019-04-29 11:20:59 +02:00
|
|
|
function CameraSystem:getViewScale(id)
|
|
|
|
local cam = self:getViewCam(id)
|
|
|
|
|
|
|
|
return cam.scale
|
|
|
|
end
|
|
|
|
|
2019-05-01 10:27:07 +02:00
|
|
|
function CameraSystem:limitView(id)
|
2019-05-02 16:19:28 +02:00
|
|
|
local viewx, viewy, vieww, viewh = self:getViewCoordinate(id)
|
|
|
|
local worldw, worldh = self.world:getDimensions()
|
|
|
|
local posx = self.views.list[id].pos.x
|
|
|
|
local posy = self.views.list[id].pos.y
|
|
|
|
local minx = self.views.width / 2
|
|
|
|
local miny = self.views.height / 2
|
|
|
|
local maxx = worldw - minx
|
|
|
|
local maxy = worldh - miny
|
|
|
|
|
|
|
|
self.views.list[id].pos.x = utils.math.between(posx, minx, maxx)
|
|
|
|
self.views.list[id].pos.y = utils.math.between(posy, miny, maxy)
|
2019-05-01 10:27:07 +02:00
|
|
|
|
|
|
|
self:computeCamPosition(id)
|
|
|
|
end
|
|
|
|
|
2019-04-29 15:03:28 +02:00
|
|
|
-- UPDATE and MOVE functions
|
|
|
|
-- Move and update the camera system
|
|
|
|
|
|
|
|
function CameraSystem:update(dt)
|
|
|
|
for i,v in ipairs(self.views.list) do
|
|
|
|
self:followActor(i)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function CameraSystem:moveView(id, x, y)
|
|
|
|
self.views.list[id].pos.x = x
|
|
|
|
self.views.list[id].pos.y = y
|
|
|
|
|
|
|
|
self:computeCamPosition(id)
|
2019-05-01 10:27:07 +02:00
|
|
|
self:limitView(id)
|
2019-04-29 15:03:28 +02:00
|
|
|
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
|
2019-06-21 18:50:45 +02:00
|
|
|
self.views.list[id].cam.y = realy - decaly
|
2019-04-29 15:03:28 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
function CameraSystem:followActor(id)
|
|
|
|
local view = self:getView(id)
|
|
|
|
|
|
|
|
if view.target ~= nil then
|
|
|
|
local x, y = view.target:getViewCenter()
|
|
|
|
x = math.floor(x)
|
|
|
|
y = math.floor(y)
|
|
|
|
self:moveView(id, x, y)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-06-13 22:23:23 +02:00
|
|
|
-- DRAW FUNCTIONS
|
|
|
|
-- Basic callback to draw stuff
|
|
|
|
|
2019-05-02 18:27:58 +02:00
|
|
|
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)
|
2019-06-21 18:50:45 +02:00
|
|
|
|
|
|
|
local xx, yy = self:getInternalCamCoordinate(id)
|
|
|
|
local string = id .. " x:" .. xx .. " y:" .. yy
|
2019-05-02 18:27:58 +02:00
|
|
|
love.graphics.print(string, viewx + 4, viewy + 4)
|
2019-06-13 22:23:23 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
function CameraSystem:drawHUD(id)
|
|
|
|
local viewx, viewy, vieww, viewh = self:getOnScreenViewCoordinate(id)
|
|
|
|
local view = self:getView(id)
|
|
|
|
local string2 = id .. " (" .. viewx .. ":" .. (viewh-viewy) .. ") "
|
|
|
|
|
|
|
|
|
2019-06-21 18:50:45 +02:00
|
|
|
love.graphics.setScissor(viewx, viewy, vieww, viewh)
|
|
|
|
love.graphics.translate(viewx, viewy)
|
2019-06-13 22:23:23 +02:00
|
|
|
view.target:drawHUD(id, vieww, viewh)
|
|
|
|
|
2019-06-21 18:50:45 +02:00
|
|
|
love.graphics.translate(-viewx, -(viewy))
|
2019-06-13 22:23:23 +02:00
|
|
|
love.graphics.setScissor( )
|
2019-04-29 13:58:13 +02:00
|
|
|
end
|
|
|
|
|
2019-04-29 08:44:10 +02:00
|
|
|
return CameraSystem
|