improvement(world): only draw visible actors when a camera is active

It'll make the game less laggy. For the moment, no need to only update 
visible actors.

Fixes #14
This commit is contained in:
Kazhnuz 2019-06-16 10:00:05 +02:00
parent 65b0c74a06
commit 61b3c29f6b

View file

@ -29,6 +29,8 @@ local BaseWorld = Object:extend()
local Sti = require(cwd .. "libs.sti") local Sti = require(cwd .. "libs.sti")
local CameraSystem = require(cwd .. "camera") local CameraSystem = require(cwd .. "camera")
local PADDING_VALUE = 10/100
-- INIT FUNCTIONS -- INIT FUNCTIONS
-- All functions to init the world and the map -- All functions to init the world and the map
@ -138,6 +140,17 @@ function BaseWorld:getActors()
return self.actors return self.actors
end end
function BaseWorld:getVisibleActors(id)
local camx, camy, camw, camh = self.cameras:getViewCoordinate(id)
local paddingw = camw * PADDING_VALUE
local paddingh = camh * PADDING_VALUE
local x = camx - paddingw
local y = camy - paddingh
local w = camw + paddingw * 2
local h = camh + paddingh * 2
return self:queryRect(x, y, w, h)
end
-- INFO FUNCTIONS -- INFO FUNCTIONS
-- Give infos about the world -- Give infos about the world
@ -360,7 +373,12 @@ function BaseWorld:draw(dt)
end end
function BaseWorld:drawActors(id) function BaseWorld:drawActors(id)
local actors = self:getActors() local actors
if (id == nil) then
actors = self:getActors()
else
actors = self:getVisibleActors(id)
end
for i,v in ipairs(actors) do for i,v in ipairs(actors) do
v:draw() v:draw()
end end