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;
Gizmo.super.new(self, world, "gizmo", x, y, w, h, false)
self.overrides = overrides
self.drawDebugBox = true
end
function Gizmo:setProperties(properties)

View file

@ -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

View file

@ -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