141 lines
3.6 KiB
Lua
141 lines
3.6 KiB
Lua
-- 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")
|
|
|
|
local SPLITSCREEN_ISVERTICAL = false
|
|
local SCREEN_LIMIT = 4
|
|
|
|
-- INIT FUNCTIONS
|
|
-- Initialize the camera system
|
|
|
|
function CameraSystem:new(world)
|
|
self.scene = world.scene
|
|
self.world = world
|
|
|
|
self.verticalSplit = SPLITSCREEN_ISVERTICAL
|
|
|
|
self:initViews()
|
|
end
|
|
|
|
function CameraSystem:initViews()
|
|
self.views = {}
|
|
|
|
self.views.list = {}
|
|
self.views.basewidth, self.views.baseheight = core.screen:getDimensions()
|
|
self.views.width, self.views.height = self:getViewsDimensions()
|
|
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
|
|
|
|
function CameraSystem:getViewsDimensions()
|
|
local basewidth, baseheight = self.views.basewidth, self.views.baseheight
|
|
local viewnumber = self:getViewNumber()
|
|
|
|
if (viewnumber <= 1) then
|
|
return basewidth, baseheight
|
|
elseif (viewnumber == 2) then
|
|
if (self.verticalSplit) then
|
|
return (basewidth/2), (baseheight)
|
|
else
|
|
return (basewidth), (baseheight/2)
|
|
end
|
|
else
|
|
return (basewidth/2), (baseheight/2)
|
|
end
|
|
end
|
|
|
|
-- WRAPPER and UTILS
|
|
-- Access data from the views
|
|
|
|
function CameraSystem:addView(x, y)
|
|
if (#self.views.list < SCREEN_LIMIT) then
|
|
local view = {}
|
|
|
|
view.cam = View(x, y, 1, 0, true)
|
|
-- TODO: add a target system in order to make a camera able
|
|
-- to target a specific object
|
|
|
|
table.insert(self.views.list, view)
|
|
self.views.width, self.views.height = self:getViewsDimensions()
|
|
end
|
|
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)
|
|
if (id ~= nil) then
|
|
local cam = self:getViewCam(id)
|
|
|
|
cam:attach()
|
|
end
|
|
end
|
|
|
|
function CameraSystem:detachView(id)
|
|
if (id ~= nil) then
|
|
local cam = self:getViewCam(id)
|
|
|
|
cam:detach()
|
|
end
|
|
end
|
|
|
|
function CameraSystem:getViewCoordinate(id)
|
|
local cam = self:getViewCam(id)
|
|
|
|
local camx, camy, camh, camw
|
|
camx = cam.x - (self.views.width/2)
|
|
camy = cam.y - (self.views.height/2)
|
|
|
|
camw = self.views.width
|
|
camh = self.views.height
|
|
return camx, camy, camh, camw
|
|
end
|
|
|
|
function CameraSystem:getViewScale(id)
|
|
local cam = self:getViewCam(id)
|
|
|
|
return cam.scale
|
|
end
|
|
|
|
return CameraSystem
|