fix(overworld): refactor the actor drawing system

This commit is contained in:
Kazhnuz 2021-03-21 16:19:28 +01:00
parent 0f08296cf3
commit 78784c27b3
3 changed files with 49 additions and 21 deletions

View file

@ -11,6 +11,7 @@ function Gizmo:new(world, x, y, w, h, overrides)
local h = h or 16; local h = h or 16;
Gizmo.super.new(self, world, "gizmo", x, y, w, h, false) Gizmo.super.new(self, world, "gizmo", x, y, w, h, false)
self.overrides = overrides self.overrides = overrides
self.drawDebugBox = true
end end
function Gizmo:setProperties(properties) function Gizmo:setProperties(properties)

View file

@ -4,8 +4,8 @@ local Parent = Base:extend()
function Parent:new(world, type, x, y, w, h, isSolid) function Parent:new(world, type, x, y, w, h, isSolid)
self.scene = world.scene self.scene = world.scene
Parent.super.new(self, world, type, x, y, w, h, isSolid) Parent.super.new(self, world, type, x, y, w, h, isSolid)
self.charDir = "down" self:initCharset()
self.charset = self.world.scene.charsetManager self.drawDebugBox = false
end end
function Parent:update(dt) function Parent:update(dt)
@ -14,8 +14,41 @@ function Parent:update(dt)
self.depth = -self.y self.depth = -self.y
end 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() 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 end
return Parent return Parent

View file

@ -22,10 +22,8 @@ function Player:new(world, x, y, id)
self.tweens = TweenManager(self) self.tweens = TweenManager(self)
self.lastCollision = -1 self.lastCollision = -1
self.haveCollided = false self.haveCollided = false
end
function Player:isMoving() self:updateCurrentCharset()
return ((math.abs(self.ysp) > 0.01) or (math.abs(self.xsp) > 0.01))
end end
function Player:updateStart(dt) function Player:updateStart(dt)
@ -65,6 +63,10 @@ function Player:updateStart(dt)
self.haveCollided = false self.haveCollided = false
end end
function Player:updateCurrentCharset()
self:setCharset(self.active.data.charset, self.active.data.charId)
end
function Player:collisionResponse(col) function Player:collisionResponse(col)
if (not col.other.owner.isDestroyed) then if (not col.other.owner.isDestroyed) then
if (col.other.type == "gizmo") then if (col.other.type == "gizmo") then
@ -97,14 +99,7 @@ function Player:timerResponse(response)
self.canChangeActive = true self.canChangeActive = true
self.activeVisible = game.characters.active self.activeVisible = game.characters.active
self.active = game.characters:getActiveCharacterData() self.active = game.characters:getActiveCharacterData()
end self:updateCurrentCharset()
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)
end end
end end
@ -114,13 +109,12 @@ function Player:drawHUD(id)
end end
function Player:drawEmblems(x, y) function Player:drawEmblems(x, y)
for i,emblem in ipairs(self.emblems) do for i,emblem in ipairs(self.emblems) do
local angle = ((i-self.activeVisible) * (360/#self.emblems)) - 90 local angle = ((i-self.activeVisible) * (360/#self.emblems)) - 90
local rad = math.rad(angle) local rad = math.rad(angle)
local emblemX, emblemY = utils.math.lengthdir(18, rad) local emblemX, emblemY = utils.math.lengthdir(18, rad)
emblem:draw(x + emblemX, y + emblemY) emblem:draw(x + emblemX, y + emblemY)
end end
end end
return Player return Player