From 5ef20afaf6a9aca1999faf8904594d94fcfaaa66 Mon Sep 17 00:00:00 2001 From: Aki Date: Fri, 8 Sep 2017 15:59:39 +0200 Subject: Created Layer class --- not/Layer.lua | 27 +++++++++++++++++++++++++++ not/World.lua | 32 +++++++++++++++----------------- 2 files changed, 42 insertions(+), 17 deletions(-) create mode 100644 not/Layer.lua diff --git a/not/Layer.lua b/not/Layer.lua new file mode 100644 index 0000000..db16175 --- /dev/null +++ b/not/Layer.lua @@ -0,0 +1,27 @@ +--- A little bit more than just a Canvas. +Layer = require "not.Object":extends() + +function Layer:new (width, height) + self.canvas = love.graphics.newCanvas(width, height) +end + +--- Sets this layer as current canvas for drawing with love.graphics functions. +-- @return old canvas used by love +function Layer:setAsCanvas () + local c = love.graphics.getCanvas() + love.graphics.setCanvas(self.canvas) + return c +end + +function Layer:clear () + local c = self:setAsCanvas() + love.graphics.clear() + love.graphics.setCanvas(c) +end + +function Layer:draw () + love.graphics.setColor(255, 255, 255, 255) + love.graphics.draw(self.canvas) +end + +return Layer diff --git a/not/World.lua b/not/World.lua index e1f2d59..e8b847a 100644 --- a/not/World.lua +++ b/not/World.lua @@ -10,6 +10,7 @@ require "not.Decoration" require "not.Ray" require "not.Cloud" require "not.CloudGenerator" +require "not.Layer" --- ZA WARUDO! -- TODO: Missing documentation on most of World's methods. @@ -24,12 +25,12 @@ function World:new (map, nauts) self.entities = {} local width, height = love.graphics.getDimensions() self.layers = { - love.graphics.newCanvas(width, height), -- back - love.graphics.newCanvas(width, height), -- cloud - love.graphics.newCanvas(width, height), -- deco - love.graphics.newCanvas(width, height), -- nauts - love.graphics.newCanvas(width, height), -- plats - love.graphics.newCanvas(width, height), -- front + Layer(width, height), -- back + Layer(width, height), -- cloud + Layer(width, height), -- deco + Layer(width, height), -- nauts + Layer(width, height), -- plats + Layer(width, height), -- front } self.map = map @@ -225,33 +226,30 @@ function World:draw () for _,entity in pairs(self.entities) do if entity:is(Decoration) then if entity.layer == 1 then - love.graphics.setCanvas(self.layers[1]) + self.layers[1]:setAsCanvas() else - love.graphics.setCanvas(self.layers[3]) + self.layers[3]:setAsCanvas() end end if entity:is(Cloud) then - love.graphics.setCanvas(self.layers[2]) + self.layers[2]:setAsCanvas() end if entity:is(Player) then - love.graphics.setCanvas(self.layers[4]) + self.layers[4]:setAsCanvas() end if entity:is(Platform) or entity:is(Effect) then - love.graphics.setCanvas(self.layers[5]) + self.layers[5]:setAsCanvas() end if entity:is(Ray) then - love.graphics.setCanvas(self.layers[6]) + self.layers[6]:setAsCanvas() end entity:draw(offset_x, offset_y, scale, debug) end love.graphics.setCanvas() for _,layer in ipairs(self.layers) do - love.graphics.setColor(255, 255, 255, 255) - love.graphics.draw(layer) - love.graphics.setCanvas(layer) - love.graphics.clear() - love.graphics.setCanvas() + layer:draw() + layer:clear() end if debug then -- cgit v1.1