From 084d31a13c8cffe3bda46ac5d49509005d468ece Mon Sep 17 00:00:00 2001 From: Kazhnuz Date: Fri, 20 May 2022 14:46:08 +0200 Subject: [PATCH] improvement: mise en commun de fonction de sprite --- .../world/actors/fighters/mixins/sprites.lua | 28 +++++++++++++++++++ .../subgames/world/actors/fighters/parent.lua | 9 ++++-- .../world/actors/fighters/player/init.lua | 7 ++--- .../world/actors/fighters/player/sprites.lua | 28 ++++++------------- 4 files changed, 44 insertions(+), 28 deletions(-) create mode 100644 sonic-radiance.love/game/modules/subgames/world/actors/fighters/mixins/sprites.lua diff --git a/sonic-radiance.love/game/modules/subgames/world/actors/fighters/mixins/sprites.lua b/sonic-radiance.love/game/modules/subgames/world/actors/fighters/mixins/sprites.lua new file mode 100644 index 0000000..4f8e90a --- /dev/null +++ b/sonic-radiance.love/game/modules/subgames/world/actors/fighters/mixins/sprites.lua @@ -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 \ No newline at end of file diff --git a/sonic-radiance.love/game/modules/subgames/world/actors/fighters/parent.lua b/sonic-radiance.love/game/modules/subgames/world/actors/fighters/parent.lua index 4c8facf..f01a787 100644 --- a/sonic-radiance.love/game/modules/subgames/world/actors/fighters/parent.lua +++ b/sonic-radiance.love/game/modules/subgames/world/actors/fighters/parent.lua @@ -5,16 +5,15 @@ local TweenManager = require "birb.classes.time" local Actions = require "game.modules.subgames.world.actors.fighters.mixins.actions" 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(Movements) -FighterParent:implement(Score) FighterParent:implement(Sprites) -FighterParent:implement(Team) -FighterParent:implement(Controls) function FighterParent:new(world, x, y, w, h, d, fighterType) FighterParent.super.new(self, world, fighterType, x, y, 0, w, h, d, true) + self.defaultDir = self.defaultDir or -1 self:initMovements() self:initActions() @@ -27,6 +26,10 @@ function FighterParent:update(dt) self.tweens:update(dt) end +function FighterParent:updateEnd(dt) + self:updateSprites() +end + function FighterParent:animationEnded(name) end diff --git a/sonic-radiance.love/game/modules/subgames/world/actors/fighters/player/init.lua b/sonic-radiance.love/game/modules/subgames/world/actors/fighters/player/init.lua index 2b805ab..9c10773 100644 --- a/sonic-radiance.love/game/modules/subgames/world/actors/fighters/player/init.lua +++ b/sonic-radiance.love/game/modules/subgames/world/actors/fighters/player/init.lua @@ -13,8 +13,9 @@ Player:implement(Team) Player:implement(Controls) function Player:new(world, x, y, z, id) + self.defaultDir = 1 Player.super.new(self, world, x, y, 16, 12, 24, "fighter") - self:initCharactersSprites() + self:updateCurrentCharset() self:initScore() self:initTeam() end @@ -39,10 +40,6 @@ function Player:animationEnded(name) end -function Player:updateEnd(dt) - self:setAnimation() -end - function Player:getViewCenter() local x, y = Player.super.getViewCenter(self) return x, y-16 diff --git a/sonic-radiance.love/game/modules/subgames/world/actors/fighters/player/sprites.lua b/sonic-radiance.love/game/modules/subgames/world/actors/fighters/player/sprites.lua index cd751d5..41724e7 100644 --- a/sonic-radiance.love/game/modules/subgames/world/actors/fighters/player/sprites.lua +++ b/sonic-radiance.love/game/modules/subgames/world/actors/fighters/player/sprites.lua @@ -1,17 +1,15 @@ local SpritedPlayer = Object:extend() -function SpritedPlayer:initCharactersSprites() - for id, name in ipairs(game.characters.team) do - self.assets:addSprite(name, "datas/gamedata/characters/" .. name .. "/sprites") - end - self.direction = 1 - self:updateCurrentCharset() +function SpritedPlayer:getSpritePath(name) + return "datas/gamedata/characters/" .. name .. "/sprites" +end + +function SpritedPlayer:getCustomSpeed() + local gsp = utils.math.pointDistance(0, 0, self.xsp, self.ysp) + return math.abs(gsp) / 12 end 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 --the animation system is already active else @@ -31,19 +29,9 @@ function SpritedPlayer:setAnimation() 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() self.charName = game.characters:getActiveCharacter() - self:setSprite(self.charName, true, 8, 10) - self.sprite:setScallingX(self.direction) + self:changeSprite(self.charName) end return SpritedPlayer