From 4e366051c4757be577c0c2aacaa4dcc9a3c0c2ee Mon Sep 17 00:00:00 2001 From: Kazhnuz Date: Fri, 19 Jul 2019 18:48:15 +0200 Subject: [PATCH] improvement(boxes): only use one texture for boxes instead of two --- .../world/actors/utils/boxes/parent.lua | 47 ++++++++++--------- 1 file changed, 24 insertions(+), 23 deletions(-) diff --git a/gamecore/modules/world/actors/utils/boxes/parent.lua b/gamecore/modules/world/actors/utils/boxes/parent.lua index 9ae975a..9b5e753 100644 --- a/gamecore/modules/world/actors/utils/boxes/parent.lua +++ b/gamecore/modules/world/actors/utils/boxes/parent.lua @@ -37,10 +37,8 @@ function Box3D:new(owner, w, h, d) self.shadowSources = {} self.needRedraw = false - self.texture = {} - self:setTopTexture() - self:setBottomTexture() - self.texture.shadows = love.graphics.newCanvas(self.w, self.h) + self:setTexture() + self.shadows = love.graphics.newCanvas(self.w, self.h) self:register() end @@ -62,29 +60,33 @@ function Box3D:setSize() self:regenerate() end -function Box3D:setTopTexture() - local canvas = love.graphics.newCanvas(self.w, self.h) +function Box3D:setTexture() + local canvas = love.graphics.newCanvas(self.w, self.h + self.d) love.graphics.setCanvas( canvas ) utils.graphics.resetColor() - love.graphics.rectangle("fill", 0, 0, self.w, self.h) + + self:drawTextureContent() + love.graphics.setCanvas( ) local imagedata = canvas:newImageData() - self.texture.top = love.graphics.newImage( imagedata ) + self.texture = love.graphics.newImage( imagedata ) imagedata:release() canvas:release() end -function Box3D:setBottomTexture() - local canvas = love.graphics.newCanvas(self.w, self.d) - love.graphics.setCanvas( canvas ) - love.graphics.setColor(0.9, 0.9, 0.9, 1) - love.graphics.rectangle("fill", 0, 0, self.w, self.d) +function Box3D:drawTextureContent() + self:drawTopTexture() + self:drawBottomTexture() +end + +function Box3D:drawTopTexture() utils.graphics.resetColor() - love.graphics.setCanvas( ) - local imagedata = canvas:newImageData() - self.texture.bottom = love.graphics.newImage( imagedata ) - imagedata:release() - canvas:release() + love.graphics.rectangle("fill", 0, 0, self.w, self.h) +end + +function Box3D:drawBottomTexture() + love.graphics.setColor(0.9, 0.9, 0.9, 1) + love.graphics.rectangle("fill", 0, self.h, self.w, self.d) end function Box3D:setShadowSource(actor, x, y) @@ -123,7 +125,7 @@ end function Box3D:redrawShadowCanvas() if (self.needRedraw) then - love.graphics.setCanvas( self.texture.shadows ) + love.graphics.setCanvas( self.shadows ) love.graphics.clear() for i,v in ipairs(self.shadowSources) do v.actor:drawShadow(v.x, v.y) @@ -141,10 +143,9 @@ function Box3D:draw(x, y, z) love.graphics.rectangle("line", x, (y-z) - (self.d), self.w, self.d + self.h) end utils.graphics.resetColor() - love.graphics.draw(self.texture.top, x, (y-z) - (self.d)) - love.graphics.draw(self.texture.bottom, x, (y-z) - (self.d) + (self.h)) - if (self.texture.shadows ~= nil) and (#self.shadowSources > 0) then - love.graphics.draw(self.texture.shadows, x, (y-z) - (self.d)) + love.graphics.draw(self.texture, x, (y-z) - (self.d)) + if (self.shadows ~= nil) and (#self.shadowSources > 0) then + love.graphics.draw(self.shadows, x, (y-z) - (self.d)) end end