feat(cbs): use the sprite code from baseactor

This commit is contained in:
Kazhnuz 2019-08-14 20:24:32 +02:00
parent 7d182bd3a1
commit 721b0ff04d
2 changed files with 125 additions and 25 deletions

View file

@ -16,14 +16,12 @@ function Hero:new(world, x, y, charid, charnumber)
if charid == nil then
core.debug:error("FATAL ERROR: charid not set")
end
self.charid = charid
self.actionPerTurn = game.characters.list[self.charid].turns
self.assets:addSprite(charid, "datas/gamedata/characters/" .. charid .. "/sprites")
self.assets.sprites[self.charid]:setCustomSpeed(16)
self:setAnimation("idle")
self:setSprite(charid, 32, 48, true)
self.charid = charid
self.charnumber = charnumber or 1
self.actionPerTurn = game.characters.list[self.charid].turns
self:initSprite()
end
-- INFO FUNCTIONS
@ -54,6 +52,8 @@ end
-- Update the hero
function Hero:update(dt)
self:updateSprite(dt)
self.keys = self.scene:getKeys(1)
if (self.currentAction == "moving") then
self:updateMoving(dt)
@ -79,7 +79,7 @@ function Hero:updateMoving(dt)
math.abs(self.y - self.yprevious)
local speed = math.sqrt(xspeed*xspeed + yspeed*yspeed) * 32
self.assets.sprites[self.charid]:setCustomSpeed(speed * 60)
self:setCustomSpeed(speed * 60)
-- Handle direction
local direction = utils.math.sign(self.x - self.xprevious)
@ -92,7 +92,7 @@ function Hero:updateMoving(dt)
if (math.abs(self.x - self.dx) < 0.01) and (math.abs(self.y - self.dy) < 0.01) then
self.x = self.dx
self.y = self.dy
self:setAnimation("idle")
self:changeAnimation("idle")
self.currentAction = "selectAttack"
self.scene.menu:set( self )
end
@ -118,7 +118,7 @@ function Hero:updateDirectionSelection(dt)
math.abs(self.y - self.yprevious)
local speed = math.sqrt(xspeed*xspeed + yspeed*yspeed) * 32
self.assets.sprites[self.charid]:setCustomSpeed(speed * 60)
self:setCustomSpeed(speed * 60)
local direction = utils.math.sign(self.x - self.xprevious)
if direction ~= 0 then
self.direction = direction
@ -127,7 +127,7 @@ function Hero:updateDirectionSelection(dt)
if (math.abs(self.x - self.startx) < 0.01) and (math.abs(self.y - self.starty) < 0.01) then
self.x = self.startx
self.y = self.starty
self:setAnimation("idle")
self:changeAnimation("idle")
self.direction = self.directionPrevious
end
end
@ -152,7 +152,7 @@ end
function Hero:receiveBackSignal()
self.currentAction = "selectDirection"
self.world.cursor:set(self.x, self.y)
self:setAnimation("walk")
self:changeAnimation("walk")
end
-- ACTION FUNCTIONS
@ -160,7 +160,7 @@ end
function Hero:validateAction()
if (self.currentAction == "selectDirection") then
self:setAnimation("walk")
self:changeAnimation("walk", true)
self.currentAction = "moving"
self.dx, self.dy = self.world.cursor.x, self.world.cursor.y
@ -168,16 +168,20 @@ function Hero:validateAction()
end
end
-- SPRITE FUNCTIONS
-- Handle the hero sprite
function Hero:initSprite()
self.assets:addSprite(self.charid, "datas/gamedata/characters/" .. self.charid .. "/sprites")
self.assets.sprites[self.charid]:setCustomSpeed(16)
self:setSprite(self.charid, 32, 48, true)
self:cloneSprite()
self:changeAnimation("idle")
end
-- DRAW FUNCTIONS
-- Draw everything related to the hero
function Hero:setAnimation(animation)
if (self.animation ~= animation) then
self.animation = animation
self.assets.sprites[self.charid]:changeAnimation(animation, true)
end
end
function Hero:draw()
x, y = self.maputils.gridToPixel(self.x, self.y, true)
--love.graphics.rectangle("fill", x - 8, y - 32, 16, 32)

View file

@ -32,21 +32,112 @@ function Parent:register()
end
function Parent:update(dt)
-- lol
self:updateSprite(dt)
end
-- SPRITE FUNCTIONS
-- Handle the character sprite
function Parent:setSprite(name, ox, oy, active)
function Parent:setSprite(spritename, ox, oy, active)
self.sprite = {}
self.sprite.name = name
self.sprite.name = spritename or nil
self.sprite.ox = ox or 0
self.sprite.oy = oy or 0
self.sprite.sx = 1
self.sprite.sy = 1
self.sprite.exist = (spritename ~= nil)
self.sprite.clone = nil
self.sprite.active = active or false
end
function Parent:cloneSprite()
if self.sprite.name ~= nil then
self.sprite.clone = self.assets.sprites[self.sprite.name]:clone()
self.sprite.clone:setCallback(self)
end
end
function Parent:changeAnimation(animation, restart)
if (self.sprite.clone == nil) then
self.assets.sprites[self.sprite.name]:changeAnimation(animation, restart)
else
self.sprite.clone:changeAnimation(animation, restart)
end
end
function Parent:animationEnded(animation)
-- Empty placeholder function
end
function Parent:setCustomSpeed(customSpeed)
if (self.sprite.clone == nil) then
self.assets.sprites[self.sprite.name]:setCustomSpeed(customSpeed)
else
self.sprite.clone:setCustomSpeed(customSpeed)
end
end
function Parent:updateSprite(dt)
if (self.sprite.clone ~= nil) then
self.sprite.clone:update(dt)
end
end
function Parent:setSpriteScallingX(sx)
local sx = sx or 1
self.sprite.sx = sx
end
function Parent:setSpriteScallingY(sy)
local sy = sy or 1
self.sprite.sy = sy
end
function Parent:getCurrentAnimation()
if (self.sprite.clone == nil) then
return self.assets.sprites[self.sprite.name]:getCurrentAnimation()
else
return self.sprite.clone:getCurrentAnimation()
end
end
function Parent:getSpriteScalling()
return self.sprite.sx, self.sprite.sy
end
function Parent:getFrame()
if (self.sprite.name ~= nil) then
if (self.sprite.clone ~= nil) then
return self.sprite.clone:getFrame()
else
return self.assets.sprites[self.sprite.name]:getFrame()
end
end
end
function Parent:getRelativeFrame()
if (self.sprite.name ~= nil) then
if (self.sprite.clone ~= nil) then
return self.sprite.clone:getRelativeFrame()
else
return self.assets.sprites[self.sprite.name]:getRelativeFrame()
end
end
end
function Parent:getAnimationDuration()
if (self.sprite.name ~= nil) then
if (self.sprite.clone ~= nil) then
return self.sprite.clone:getAnimationDuration()
else
return self.assets.sprites[self.sprite.name]:getAnimationDuration()
end
end
end
function Parent:drawSprite(tx, ty)
utils.graphics.resetColor()
@ -56,7 +147,12 @@ function Parent:drawSprite(tx, ty)
local ty = ty or 0
if (self.sprite.active) then
self.assets.sprites[self.sprite.name]:drawAnimation(x + tx, y + ty, 0, self.direction, 1, self.sprite.ox, self.sprite.oy)
if (self.sprite.clone ~= nil) then
self.sprite.clone:draw(x + tx, y + ty, 0, self.direction, 1, self.sprite.ox, self.sprite.oy)
else
self.assets.sprites[self.sprite.name]:drawAnimation(x + tx, y + ty, 0, self.direction, 1, self.sprite.ox, self.sprite.oy)
end
end
end