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

View file

@ -32,21 +32,112 @@ function Parent:register()
end end
function Parent:update(dt) function Parent:update(dt)
-- lol self:updateSprite(dt)
end end
-- SPRITE FUNCTIONS -- SPRITE FUNCTIONS
-- Handle the character sprite -- Handle the character sprite
function Parent:setSprite(name, ox, oy, active) function Parent:setSprite(spritename, ox, oy, active)
self.sprite = {} self.sprite = {}
self.sprite.name = spritename or nil
self.sprite.name = name
self.sprite.ox = ox or 0 self.sprite.ox = ox or 0
self.sprite.oy = oy 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 self.sprite.active = active or false
end 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) function Parent:drawSprite(tx, ty)
utils.graphics.resetColor() utils.graphics.resetColor()
@ -56,7 +147,12 @@ function Parent:drawSprite(tx, ty)
local ty = ty or 0 local ty = ty or 0
if (self.sprite.active) then 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
end end