improvement: mise en commun de fonction de sprite

This commit is contained in:
Kazhnuz 2022-05-20 14:46:08 +02:00
parent 4d22e35288
commit 084d31a13c
4 changed files with 44 additions and 28 deletions

View file

@ -0,0 +1,28 @@
local FighterSprite = Object:extend()
function FighterSprite:changeSprite(name)
if (self.assets.sprites[name] == nil) then
self.assets:addSprite(name, self:getSpritePath(name))
end
self.assets.sprites[name]:setCustomSpeed(16)
self:setSprite(name, true, 8, 10)
self:changeAnimation("idle")
self:setDirection(self.direction or self.defaultDir)
end
function FighterSprite:updateSprites(dt)
self.sprite:setCustomSpeed(self:getCustomSpeed())
self:setDirection(self.xsp)
self:setAnimation()
end
function FighterSprite:setDirection(direction)
direction = direction or 0
if (direction ~= 0) then
direction = utils.math.sign(direction)
self.direction = direction
self.sprite:setScallingX(direction)
end
end
return FighterSprite

View file

@ -5,16 +5,15 @@ local TweenManager = require "birb.classes.time"
local Actions = require "game.modules.subgames.world.actors.fighters.mixins.actions" local Actions = require "game.modules.subgames.world.actors.fighters.mixins.actions"
local Movements = require "game.modules.subgames.world.actors.fighters.mixins.movements" local Movements = require "game.modules.subgames.world.actors.fighters.mixins.movements"
local Sprites = require "game.modules.subgames.world.actors.fighters.mixins.sprites"
FighterParent:implement(Actions) FighterParent:implement(Actions)
FighterParent:implement(Movements) FighterParent:implement(Movements)
FighterParent:implement(Score)
FighterParent:implement(Sprites) FighterParent:implement(Sprites)
FighterParent:implement(Team)
FighterParent:implement(Controls)
function FighterParent:new(world, x, y, w, h, d, fighterType) function FighterParent:new(world, x, y, w, h, d, fighterType)
FighterParent.super.new(self, world, fighterType, x, y, 0, w, h, d, true) FighterParent.super.new(self, world, fighterType, x, y, 0, w, h, d, true)
self.defaultDir = self.defaultDir or -1
self:initMovements() self:initMovements()
self:initActions() self:initActions()
@ -27,6 +26,10 @@ function FighterParent:update(dt)
self.tweens:update(dt) self.tweens:update(dt)
end end
function FighterParent:updateEnd(dt)
self:updateSprites()
end
function FighterParent:animationEnded(name) function FighterParent:animationEnded(name)
end end

View file

@ -13,8 +13,9 @@ Player:implement(Team)
Player:implement(Controls) Player:implement(Controls)
function Player:new(world, x, y, z, id) function Player:new(world, x, y, z, id)
self.defaultDir = 1
Player.super.new(self, world, x, y, 16, 12, 24, "fighter") Player.super.new(self, world, x, y, 16, 12, 24, "fighter")
self:initCharactersSprites() self:updateCurrentCharset()
self:initScore() self:initScore()
self:initTeam() self:initTeam()
end end
@ -39,10 +40,6 @@ function Player:animationEnded(name)
end end
function Player:updateEnd(dt)
self:setAnimation()
end
function Player:getViewCenter() function Player:getViewCenter()
local x, y = Player.super.getViewCenter(self) local x, y = Player.super.getViewCenter(self)
return x, y-16 return x, y-16

View file

@ -1,17 +1,15 @@
local SpritedPlayer = Object:extend() local SpritedPlayer = Object:extend()
function SpritedPlayer:initCharactersSprites() function SpritedPlayer:getSpritePath(name)
for id, name in ipairs(game.characters.team) do return "datas/gamedata/characters/" .. name .. "/sprites"
self.assets:addSprite(name, "datas/gamedata/characters/" .. name .. "/sprites")
end end
self.direction = 1
self:updateCurrentCharset() function SpritedPlayer:getCustomSpeed()
local gsp = utils.math.pointDistance(0, 0, self.xsp, self.ysp)
return math.abs(gsp) / 12
end end
function SpritedPlayer:setAnimation() function SpritedPlayer:setAnimation()
local gsp = utils.math.pointDistance(0, 0, self.xsp, self.ysp)
self.sprite:setCustomSpeed(math.abs(gsp) / 12)
self:setDirection(self.xsp)
if (self.action == "punching") then if (self.action == "punching") then
--the animation system is already active --the animation system is already active
else else
@ -31,19 +29,9 @@ function SpritedPlayer:setAnimation()
end end
end end
function SpritedPlayer:setDirection(direction)
direction = direction or 0
if direction ~= 0 then
direction = utils.math.sign(direction)
self.direction = direction
self.sprite:setScallingX(direction)
end
end
function SpritedPlayer:updateCurrentCharset() function SpritedPlayer:updateCurrentCharset()
self.charName = game.characters:getActiveCharacter() self.charName = game.characters:getActiveCharacter()
self:setSprite(self.charName, true, 8, 10) self:changeSprite(self.charName)
self.sprite:setScallingX(self.direction)
end end
return SpritedPlayer return SpritedPlayer