From b92f441d4022f54150c86b952f84ec802db3674b Mon Sep 17 00:00:00 2001 From: Kazhnuz Date: Sat, 27 Jul 2019 13:52:41 +0200 Subject: [PATCH] feat: add parallax backgrounds --- .../game/modules/world/init.lua | 26 ++++++++++++++++++- .../game/modules/world/maps/test.lua | 15 +++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/sonic-radiance.love/game/modules/world/init.lua b/sonic-radiance.love/game/modules/world/init.lua index c07057f..07622ae 100644 --- a/sonic-radiance.love/game/modules/world/init.lua +++ b/sonic-radiance.love/game/modules/world/init.lua @@ -16,7 +16,6 @@ function RadianceWorld:createMapController() end end - function RadianceWorld:loadMapObjects() RadianceWorld.super.loadMapObjects(self) self:addInvisibleWalls() @@ -31,4 +30,29 @@ function RadianceWorld:addInvisibleWalls() self.obj.collisions["invisible"](self, -16, 0, 0, 16, h, 1000) end +function RadianceWorld:draw(dt) + self:drawBackgroundColor() + local camNumber = self.cameras:getViewNumber() + + if (camNumber == 0) then + self:drawMap() + self:drawActors() + else + for i=1, camNumber do + self:drawParallax(i) + self.cameras:attachView(i) + self:drawMap(i) + self:drawActors(i) + self.cameras:detachView(i) + end + end +end + +function RadianceWorld:drawParallax(i) + local x, y, w, h = self.cameras:getViewCoordinate(i) + if (self.map ~= nil) and (self.maptype ~= "sti") then + self.map:drawParallax(x, y, w, h) + end +end + return RadianceWorld diff --git a/sonic-radiance.love/game/modules/world/maps/test.lua b/sonic-radiance.love/game/modules/world/maps/test.lua index 930418f..d497e45 100644 --- a/sonic-radiance.love/game/modules/world/maps/test.lua +++ b/sonic-radiance.love/game/modules/world/maps/test.lua @@ -4,6 +4,8 @@ local TestMap = BaseMap:extend() function TestMap:new(world) TestMap.super.new(self, world) self:setPadding(0, 96, 0, 0) + + self.background = love.graphics.newImage("assets/backgrounds/parallax/test-back.png") end function TestMap:loadCollisions() @@ -30,4 +32,17 @@ function TestMap:draw() -- Empty Placeholder function end +function TestMap:drawParallax(x, y, w, h) + local imax, jmax = (w/32)+1, (h/32)+1 + local x, y = x or 0, y or 0 + local x = math.floor(x/4) % 32 + local y = math.floor((y+96)/6) % 32 + + for i=0, math.ceil(imax) do + for j=0, math.ceil(jmax) do + love.graphics.draw(self.background, (i-1)*32-x, (j-1)*32-y) + end + end +end + return TestMap