2019-04-29 08:44:10 +02:00
|
|
|
-- camera.lua :: a basic camera adapted to the asset/world system.
|
|
|
|
|
|
|
|
--[[
|
|
|
|
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.
|
|
|
|
]]
|
|
|
|
|
2019-07-14 18:28:29 +02:00
|
|
|
local cwd = (...):gsub('%.init$', '') .. "."
|
2019-04-29 08:44:10 +02:00
|
|
|
|
|
|
|
local CameraSystem = Object:extend()
|
2019-07-14 18:57:50 +02:00
|
|
|
--local View = require(cwd .. "libs.hump.camera")
|
2019-04-29 08:44:10 +02:00
|
|
|
|
2019-07-14 18:35:33 +02:00
|
|
|
local camutils = require(cwd .. "utils")
|
|
|
|
|
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:32:42 +02:00
|
|
|
self.mode = "split"
|
|
|
|
self.verticalSplit = SPLITSCREEN_ISVERTICAL
|
2019-06-29 18:42:38 +02:00
|
|
|
self.targets = {}
|
2019-04-29 08:44:10 +02:00
|
|
|
|
|
|
|
self:initViews()
|
|
|
|
end
|
|
|
|
|
2019-06-29 11:32:42 +02:00
|
|
|
function CameraSystem:setMode(mode)
|
|
|
|
self.mode = mode
|
2019-07-24 11:21:32 +02:00
|
|
|
core.debug:print("camera", "mode is now set to " .. mode)
|
2019-06-29 11:32:42 +02:00
|
|
|
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
|
|
|
|
2019-07-14 18:35:33 +02:00
|
|
|
self.views.posList = camutils.getViewsPositions(self.views.basewidth, self.views.baseheight, self.verticalSplit)
|
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-07-14 23:12:22 +02:00
|
|
|
function CameraSystem:getViewsDimensions()
|
2019-04-29 11:39:07 +02:00
|
|
|
local basewidth, baseheight = self.views.basewidth, self.views.baseheight
|
2019-07-14 23:12:22 +02:00
|
|
|
local viewnumber = self:getViewNumber()
|
|
|
|
|
|
|
|
return camutils.getViewsDimensions(viewnumber, basewidth, baseheight, self.verticalSplit)
|
2019-04-29 11:39:07 +02:00
|
|
|
end
|
|
|
|
|
2019-04-29 13:58:13 +02:00
|
|
|
function CameraSystem:recalculateViewsPositions()
|
|
|
|
if #self.views.list == 1 then
|
2019-07-14 22:59:58 +02:00
|
|
|
self.views.list[1].onScreen.x = 0
|
|
|
|
self.views.list[1].onScreen.y = 0
|
2019-04-29 13:58:13 +02:00
|
|
|
else
|
|
|
|
for i,v in ipairs(self.views.list) do
|
|
|
|
local x, y = self:getViewPositions(i)
|
2019-07-14 22:59:58 +02:00
|
|
|
self.views.list[i].onScreen.x = x
|
|
|
|
self.views.list[i].onScreen.y = y
|
2019-04-29 13:58:13 +02:00
|
|
|
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:32:42 +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)
|
2019-06-29 18:42:38 +02:00
|
|
|
table.insert(self.targets, target)
|
|
|
|
elseif (self.mode == "middle") or (self.mode == "zoom") then
|
|
|
|
table.insert(self.targets, target)
|
2019-06-29 11:32:42 +02:00
|
|
|
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 = {}
|
2019-07-14 22:59:58 +02:00
|
|
|
view.x = x or 0
|
|
|
|
view.y = y or 0
|
|
|
|
view.scale = 1
|
|
|
|
|
|
|
|
view.onScreen = {}
|
|
|
|
|
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-07-14 22:49:27 +02:00
|
|
|
self:regenerateCanvases()
|
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
|
|
|
|
|
2019-07-14 22:49:27 +02:00
|
|
|
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
|
|
|
|
|
2019-04-29 08:44:10 +02:00
|
|
|
function CameraSystem:getView(id)
|
|
|
|
return self.views.list[id]
|
|
|
|
end
|
|
|
|
|
|
|
|
function CameraSystem:attachView(id)
|
2019-04-29 08:45:48 +02:00
|
|
|
if (id ~= nil) then
|
2019-07-14 22:49:27 +02:00
|
|
|
local view = self:getView(id)
|
|
|
|
|
|
|
|
self.current_canvas = love.graphics.getCanvas()
|
|
|
|
love.graphics.setCanvas(view.canvas)
|
|
|
|
love.graphics.clear()
|
2019-04-29 08:44:10 +02:00
|
|
|
|
2019-07-14 18:48:25 +02:00
|
|
|
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
|
2019-07-14 22:49:27 +02:00
|
|
|
tx, ty = self:getViewCoordinate(id)
|
|
|
|
scale = self:getViewScale(id) or 1
|
2019-07-15 10:04:06 +02:00
|
|
|
tx = math.floor(tx) * -1
|
|
|
|
ty = math.floor(ty) * -1
|
2019-07-14 18:48:25 +02:00
|
|
|
|
|
|
|
local w, h = core.screen:getDimensions()
|
|
|
|
end
|
|
|
|
|
|
|
|
love.graphics.push()
|
|
|
|
love.graphics.origin()
|
|
|
|
love.graphics.translate(math.floor(tx), math.floor(ty))
|
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
|
2019-07-14 22:49:27 +02:00
|
|
|
local view = self:getView(id)
|
|
|
|
love.graphics.pop()
|
|
|
|
|
|
|
|
love.graphics.setCanvas(self.current_canvas)
|
|
|
|
local tx, ty = self:getOnScreenViewCoordinate(id)
|
2019-07-15 10:04:06 +02:00
|
|
|
local scale = core.screen:getScale() * view.scale
|
|
|
|
|
2019-07-14 22:49:27 +02:00
|
|
|
love.graphics.push()
|
|
|
|
love.graphics.origin()
|
|
|
|
love.graphics.translate(math.floor(tx), math.floor(ty))
|
|
|
|
love.graphics.scale(scale, scale)
|
2019-04-29 08:44:10 +02:00
|
|
|
|
2019-07-14 22:49:27 +02:00
|
|
|
love.graphics.draw(view.canvas)
|
2019-07-15 10:04:06 +02:00
|
|
|
|
|
|
|
local unscale = 1 / view.scale
|
|
|
|
love.graphics.scale(unscale, unscale)
|
2019-07-14 22:49:27 +02:00
|
|
|
self:drawHUD(id)
|
2019-07-15 10:04:06 +02:00
|
|
|
|
2019-07-14 18:48:25 +02:00
|
|
|
love.graphics.pop()
|
2019-04-29 08:45:48 +02:00
|
|
|
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
|
|
|
|
|
2019-07-14 22:59:58 +02:00
|
|
|
vieww = self.views.width / view.scale
|
|
|
|
viewh = self.views.height / view.scale
|
2019-07-14 22:49:27 +02:00
|
|
|
|
2019-07-14 22:59:58 +02:00
|
|
|
viewx = view.x - (vieww / 2)
|
|
|
|
viewy = view.y - (viewh / 2)
|
2019-07-14 22:49:27 +02:00
|
|
|
|
2019-06-21 18:27:02 +02:00
|
|
|
return viewx, viewy, vieww, viewh
|
|
|
|
end
|
2019-04-29 11:20:59 +02:00
|
|
|
|
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)
|
2019-07-14 22:59:58 +02:00
|
|
|
viewx = (basex) + view.onScreen.x - (self.views.width / 2)
|
|
|
|
viewy = (basey) + view.onScreen.y - (self.views.height / 2)
|
2019-04-29 13:58:13 +02:00
|
|
|
|
2019-07-14 18:41:13 +02:00
|
|
|
viewx, viewy = core.screen:getScreenCoordinate(viewx, viewy)
|
|
|
|
|
|
|
|
vieww = self.views.width * core.screen:getScale()
|
|
|
|
viewh = self.views.height * core.screen:getScale()
|
2019-04-29 13:58:13 +02:00
|
|
|
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)
|
2019-07-14 22:59:58 +02:00
|
|
|
viewx = view.onScreen.x
|
|
|
|
viewy = view.onScreen.y
|
2019-05-02 16:19:59 +02:00
|
|
|
|
2019-07-14 18:41:13 +02:00
|
|
|
return core.screen:getScreenCoordinate(viewx, viewy)
|
2019-05-02 16:19:59 +02:00
|
|
|
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)
|
2019-07-14 22:59:58 +02:00
|
|
|
viewx = (basex) + view.onScreen.x
|
|
|
|
viewy = (basey) + view.onScreen.y
|
2019-04-29 13:58:13 +02:00
|
|
|
|
2019-07-14 18:41:13 +02:00
|
|
|
return core.screen:getScreenCoordinate(viewx, viewy)
|
2019-04-29 13:58:13 +02:00
|
|
|
end
|
|
|
|
|
2019-07-15 10:04:06 +02:00
|
|
|
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
|
|
|
|
|
2019-04-29 11:20:59 +02:00
|
|
|
function CameraSystem:getViewScale(id)
|
2019-07-14 22:59:58 +02:00
|
|
|
local view = self:getView(id)
|
2019-04-29 11:20:59 +02:00
|
|
|
|
2019-07-14 22:59:58 +02:00
|
|
|
return view.scale
|
2019-04-29 11:20:59 +02:00
|
|
|
end
|
|
|
|
|
2019-05-01 10:27:07 +02:00
|
|
|
function CameraSystem:limitView(id)
|
2019-07-26 11:43:54 +02:00
|
|
|
local viewx, viewy, vieww, viewh = self:getViewCoordinate(id)
|
|
|
|
local worldx, worldy, worldw, worldh = self.world:getBox()
|
2019-07-14 22:59:58 +02:00
|
|
|
local posx = self.views.list[id].x
|
|
|
|
local posy = self.views.list[id].y
|
2019-07-26 11:43:54 +02:00
|
|
|
local minx = worldx + self.views.width / 2
|
|
|
|
local miny = worldy + self.views.height / 2
|
2019-07-26 11:53:10 +02:00
|
|
|
local maxx = worldw - self.views.width / 2
|
|
|
|
local maxy = worldh - self.views.height / 2
|
2019-05-02 16:19:28 +02:00
|
|
|
|
2019-07-14 22:59:58 +02:00
|
|
|
self.views.list[id].x = utils.math.between(posx, minx, maxx)
|
|
|
|
self.views.list[id].y = utils.math.between(posy, miny, maxy)
|
2019-05-01 10:27:07 +02:00
|
|
|
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
|
2019-06-29 18:42:38 +02:00
|
|
|
if (self.mode == "middle") or (self.mode == "zoom") then
|
|
|
|
self:followAllActors(i)
|
|
|
|
else
|
|
|
|
self:followActor(i)
|
|
|
|
end
|
2019-04-29 15:03:28 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function CameraSystem:moveView(id, x, y)
|
2019-07-14 22:59:58 +02:00
|
|
|
self.views.list[id].x = x
|
|
|
|
self.views.list[id].y = y
|
2019-04-29 15:03:28 +02:00
|
|
|
|
2019-05-01 10:27:07 +02:00
|
|
|
self:limitView(id)
|
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-29 18:42:38 +02:00
|
|
|
function CameraSystem:followAllActors(id)
|
|
|
|
local view = self:getView(id)
|
|
|
|
local PADDING = 16
|
|
|
|
-- TODO: make all the padding and stuff part of object definition instead ?
|
|
|
|
-- It would especially allow better work in future fake3D worlds
|
|
|
|
|
|
|
|
if (#self.targets > 0) then
|
|
|
|
local minX, minY, maxX, maxY
|
|
|
|
for i, target in ipairs(self.targets) do
|
2019-07-01 14:15:10 +02:00
|
|
|
local x, y, w, h = target:getShape()
|
|
|
|
local x2, y2 = x + w, y + h
|
2019-06-29 18:42:38 +02:00
|
|
|
-- If start by initializing the value at the first found value
|
2019-07-01 14:15:10 +02:00
|
|
|
if (minX == nil) then minX = x end
|
|
|
|
if (maxX == nil) then maxX = x2 end
|
|
|
|
if (minY == nil) then minY = y end
|
|
|
|
if (maxY == nil) then maxY = y2 end
|
2019-06-29 18:42:38 +02:00
|
|
|
|
|
|
|
|
2019-07-01 14:15:10 +02:00
|
|
|
minX = math.min(minX, x)
|
|
|
|
maxX = math.max(maxX, x2)
|
|
|
|
minY = math.min(minY, y)
|
|
|
|
maxY = math.max(maxY, y2)
|
2019-06-29 18:42:38 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
-- Add padding
|
|
|
|
minX = minX - PADDING
|
|
|
|
minY = minY - PADDING
|
|
|
|
maxX = maxX + PADDING
|
|
|
|
maxY = maxY + PADDING
|
|
|
|
local x, y
|
|
|
|
x = (minX + maxX) / 2
|
|
|
|
y = (minY + maxY) / 2
|
|
|
|
|
|
|
|
local scale = 1
|
|
|
|
if (self.mode == "zoom") then
|
|
|
|
local ww, hh = core.screen:getDimensions()
|
|
|
|
local scalex = (maxX-minX)/ww
|
|
|
|
local scaley = (maxY-minY)/hh
|
|
|
|
scale = math.max(scale, scalex, scaley)
|
2019-07-15 10:04:06 +02:00
|
|
|
self:setViewScale(id, 1/scale)
|
2019-06-29 18:42:38 +02:00
|
|
|
self.world:resizeMap(ww * 3, hh * 3)
|
|
|
|
end
|
|
|
|
|
|
|
|
self:moveView(id, x, y)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2019-06-13 22:23:23 +02:00
|
|
|
-- DRAW FUNCTIONS
|
|
|
|
-- Basic callback to draw stuff
|
|
|
|
|
|
|
|
function CameraSystem:drawHUD(id)
|
|
|
|
local view = self:getView(id)
|
2019-07-14 22:49:27 +02:00
|
|
|
local viewx, viewy, vieww, viewh = self:getOnScreenViewCoordinate(id)
|
2019-06-13 22:23:23 +02:00
|
|
|
|
|
|
|
view.target:drawHUD(id, vieww, viewh)
|
2019-04-29 13:58:13 +02:00
|
|
|
end
|
|
|
|
|
2019-04-29 08:44:10 +02:00
|
|
|
return CameraSystem
|