From 61b3c29f6bcd993c5a513344bc5cc47d1d3ba2ee Mon Sep 17 00:00:00 2001 From: Kazhnuz Date: Sun, 16 Jun 2019 10:00:05 +0200 Subject: [PATCH] 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 --- gamecore/modules/world/baseworld.lua | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/gamecore/modules/world/baseworld.lua b/gamecore/modules/world/baseworld.lua index c9fc6b1..a1f0ee1 100644 --- a/gamecore/modules/world/baseworld.lua +++ b/gamecore/modules/world/baseworld.lua @@ -29,6 +29,8 @@ local BaseWorld = Object:extend() local Sti = require(cwd .. "libs.sti") local CameraSystem = require(cwd .. "camera") +local PADDING_VALUE = 10/100 + -- INIT FUNCTIONS -- All functions to init the world and the map @@ -138,6 +140,17 @@ function BaseWorld:getActors() return self.actors 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 -- Give infos about the world @@ -360,7 +373,12 @@ function BaseWorld:draw(dt) end 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 v:draw() end