From 78784c27b3e63405a11f400222bb888c57747810 Mon Sep 17 00:00:00 2001 From: Kazhnuz Date: Sun, 21 Mar 2021 16:19:28 +0100 Subject: [PATCH] fix(overworld): refactor the actor drawing system --- .../scenes/overworld/actors/gizmo.lua | 1 + .../scenes/overworld/actors/parent.lua | 39 +++++++++++++++++-- .../scenes/overworld/actors/player.lua | 30 ++++++-------- 3 files changed, 49 insertions(+), 21 deletions(-) diff --git a/sonic-radiance.love/scenes/overworld/actors/gizmo.lua b/sonic-radiance.love/scenes/overworld/actors/gizmo.lua index 2d13bda..920d028 100644 --- a/sonic-radiance.love/scenes/overworld/actors/gizmo.lua +++ b/sonic-radiance.love/scenes/overworld/actors/gizmo.lua @@ -11,6 +11,7 @@ function Gizmo:new(world, x, y, w, h, overrides) local h = h or 16; Gizmo.super.new(self, world, "gizmo", x, y, w, h, false) self.overrides = overrides + self.drawDebugBox = true end function Gizmo:setProperties(properties) diff --git a/sonic-radiance.love/scenes/overworld/actors/parent.lua b/sonic-radiance.love/scenes/overworld/actors/parent.lua index 5a237a7..156662f 100644 --- a/sonic-radiance.love/scenes/overworld/actors/parent.lua +++ b/sonic-radiance.love/scenes/overworld/actors/parent.lua @@ -4,8 +4,8 @@ local Parent = Base:extend() function Parent:new(world, type, x, y, w, h, isSolid) self.scene = world.scene Parent.super.new(self, world, type, x, y, w, h, isSolid) - self.charDir = "down" - self.charset = self.world.scene.charsetManager + self:initCharset() + self.drawDebugBox = false end function Parent:update(dt) @@ -14,8 +14,41 @@ function Parent:update(dt) self.depth = -self.y end +function Parent:isMoving() + return ((math.abs(self.ysp) > 0.01) or (math.abs(self.xsp) > 0.01)) +end + +-- Charset / Draw functions +-- Handle the charset of an object + +function Parent:initCharset() + self.charsetManager = self.world.scene.charsetManager + self.charset = nil + self.charDir = "down" + self.cantWalk = false +end + +function Parent:setCharset(charset, charId, cantWalk) + self.charset = charset + self.charId = charId + self.cantWalk = (cantWalk == true) +end + +function Parent:drawCharset(charset, charId) + if (self:isMoving() and (not self.cantWalk)) then + self.charsetManager:draw(self.charset, self.charId, self.charDir, self.x, self.y) + else + self.charsetManager:drawStanding(self.charset, self.charId, self.charDir, self.x, self.y) + end +end + function Parent:draw() - love.graphics.rectangle("fill", math.floor(self.x), math.floor(self.y), self.w, self.h) + if (self.charset ~= nil) then + self:drawCharset() + end + if (self.drawDebugBox) then + love.graphics.rectangle("line", math.floor(self.x), math.floor(self.y), self.w, self.h) + end end return Parent diff --git a/sonic-radiance.love/scenes/overworld/actors/player.lua b/sonic-radiance.love/scenes/overworld/actors/player.lua index 27a4e7d..18f6edc 100644 --- a/sonic-radiance.love/scenes/overworld/actors/player.lua +++ b/sonic-radiance.love/scenes/overworld/actors/player.lua @@ -22,10 +22,8 @@ function Player:new(world, x, y, id) self.tweens = TweenManager(self) self.lastCollision = -1 self.haveCollided = false -end -function Player:isMoving() - return ((math.abs(self.ysp) > 0.01) or (math.abs(self.xsp) > 0.01)) + self:updateCurrentCharset() end function Player:updateStart(dt) @@ -65,6 +63,10 @@ function Player:updateStart(dt) self.haveCollided = false end +function Player:updateCurrentCharset() + self:setCharset(self.active.data.charset, self.active.data.charId) +end + function Player:collisionResponse(col) if (not col.other.owner.isDestroyed) then if (col.other.type == "gizmo") then @@ -97,14 +99,7 @@ function Player:timerResponse(response) self.canChangeActive = true self.activeVisible = game.characters.active self.active = game.characters:getActiveCharacterData() - end -end - -function Player:draw() - if (self:isMoving()) then - self.charset:draw(self.active.data.charset, self.active.data.charId, self.charDir, self.x, self.y) - else - self.charset:drawStanding(self.active.data.charset, self.active.data.charId, self.charDir, self.x, self.y) + self:updateCurrentCharset() end end @@ -114,13 +109,12 @@ function Player:drawHUD(id) end function Player:drawEmblems(x, y) -for i,emblem in ipairs(self.emblems) do - local angle = ((i-self.activeVisible) * (360/#self.emblems)) - 90 - local rad = math.rad(angle) - local emblemX, emblemY = utils.math.lengthdir(18, rad) - emblem:draw(x + emblemX, y + emblemY) -end - + for i,emblem in ipairs(self.emblems) do + local angle = ((i-self.activeVisible) * (360/#self.emblems)) - 90 + local rad = math.rad(angle) + local emblemX, emblemY = utils.math.lengthdir(18, rad) + emblem:draw(x + emblemX, y + emblemY) + end end return Player