tiny-ecs-love-template/systems/camera-pan.lua

56 lines
1.6 KiB
Lua

Camera = {
pan = {
x = 0,
y = 0,
},
}
expireBelowScreenSystem = filteredSystem("expireBelowScreen", { position = T.XyPair, expireBelowScreenBy = T.number })
local focusPriority = {}
cameraPanSystem = filteredSystem("cameraPan", { focusPriority = T.number, position = T.XyPair }, function(e, _)
if e.focusPriority >= focusPriority.priority then
focusPriority.position = e.position
end
end)
function cameraPanSystem.preProcess()
focusPriority.priority = 0
focusPriority.position = { x = 0, y = 0 }
end
function cameraPanSystem:postProcess()
Camera.pan.x = math.max(0, focusPriority.position.x - 200)
Camera.pan.y = math.min(0, focusPriority.position.y - 120)
-- TODO: set draw offset
for _, entity in pairs(expireBelowScreenSystem.entities) do
if entity.position.y - (Camera.pan.y + 240) > entity.expireBelowScreenBy then
self.world:removeEntity(entity)
end
end
end
local cameraTopIsh, cameraBottomIsh
local enableNearCameraY = filteredSystem(
"enableNearCameraY",
{ enableNearCameraY = Arr(T.Entity) },
function(e, _, system)
if e.position.y > cameraTopIsh and e.position.y < cameraBottomIsh then
for _, enable in ipairs(e.enableNearCameraY) do
enable.velocity = e.velocity
system.world:addEntity(enable)
end
system.world:removeEntity(e)
end
end
)
local within = 1000
function enableNearCameraY:preProcess()
cameraTopIsh = Camera.pan.y - within
cameraBottomIsh = Camera.pan.y + 240 + within
end