improvement: replace actions by the state machine

This commit is contained in:
Kazhnuz 2022-05-27 09:29:39 +02:00
parent 75145f1485
commit 3545e6b898
7 changed files with 46 additions and 36 deletions

View file

@ -1,7 +0,0 @@
local PlayerActions = Object:extend()
function PlayerActions:initActions()
self.action = "idle"
end
return PlayerActions

View file

@ -23,6 +23,30 @@ function FighterSprite:updateSprites(dt)
self:setAnimation() self:setAnimation()
end end
function FighterSprite:setAnimation()
if (self:getStateVar("defaultAnim", false)) then
self:defaultAnim()
else
self:playStateFunc("changeAnimation")
end
end
function FighterSprite:defaultAnim()
if (self.onGround or self:isAerial()) then
if (math.abs(self.xsp) > 0) or (math.abs(self.ysp) > 0) then
self.sprite:changeAnimation("walk", false)
else
self.sprite:changeAnimation("idle", false)
end
else
if (self.zsp) > 0 then
self.sprite:changeAnimation("jump", false)
else
self.sprite:changeAnimation("fall", false)
end
end
end
function FighterSprite:setDirection(direction) function FighterSprite:setDirection(direction)
direction = direction or 0 direction = direction or 0
if (direction ~= 0) then if (direction ~= 0) then

View file

@ -3,16 +3,18 @@ local FighterParent = Parent:extend()
local TweenManager = require "birb.classes.time" 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 Movements = require "game.modules.subgames.world.actors.fighters.mixins.movements"
local Sprites = require "game.modules.subgames.world.actors.fighters.mixins.sprites" local Sprites = require "game.modules.subgames.world.actors.fighters.mixins.sprites"
local Abstract = require "game.modules.subgames.world.actors.fighters.mixins.abstract" local Abstract = require "game.modules.subgames.world.actors.fighters.mixins.abstract"
FighterParent:implement(Actions)
FighterParent:implement(Movements) FighterParent:implement(Movements)
FighterParent:implement(Sprites) FighterParent:implement(Sprites)
FighterParent:implement(Abstract) FighterParent:implement(Abstract)
local states = {"idle"}
FighterParent:addStates("game.modules.subgames.world.actors.fighters.states", "idle")
function FighterParent:new(world, x, y, fighterType) function FighterParent:new(world, x, y, fighterType)
self.abstract = self:getAbstract() self.abstract = self:getAbstract()
@ -22,7 +24,6 @@ function FighterParent:new(world, x, y, fighterType)
FighterParent.super.new(self, world, fighterType, x, y, z, w, h, d, false) FighterParent.super.new(self, world, fighterType, x, y, z, w, h, d, false)
self.defaultDir = self.defaultDir or -1 self.defaultDir = self.defaultDir or -1
self:initMovements() self:initMovements()
self:initActions()
self.tweens = TweenManager(self) self.tweens = TweenManager(self)
end end

View file

@ -55,7 +55,7 @@ function PlayerControls:dpadToAngle()
end end
function PlayerControls:applyJumpInput() function PlayerControls:applyJumpInput()
if self.keys["A"].isPressed and (self:isNotJumping()) then if self.keys["A"].isPressed and (self:isNotJumping() and self:getStateVar("canJump", false)) then
self:jump() self:jump()
end end
end end
@ -68,12 +68,14 @@ function PlayerControls:applyActionsInputs()
end end
function PlayerControls:applySwitchInputs() function PlayerControls:applySwitchInputs()
if self.keys["L1"].isPressed and (self.action == "normal") and (self.onGround) then if (self.onGround and self:getStateVar("canSwitch", false)) then
if self.keys["L1"].isPressed then
self:switchActiveCharacter() self:switchActiveCharacter()
end end
if self.keys["R1"].isPressed and (self.action == "normal") and (self.onGround) then if self.keys["R1"].isPressed then
self:switchActiveCharacter(-1) self:switchActiveCharacter(-1)
end end
end end
end
return PlayerControls return PlayerControls

View file

@ -9,26 +9,6 @@ function SpritedPlayer:getCustomSpeed()
return math.abs(gsp) / 12 return math.abs(gsp) / 12
end end
function SpritedPlayer:setAnimation()
if (self.action == "punching") then
--the animation system is already active
else
if (self.onGround) then
if (math.abs(self.xsp) > 0) or (math.abs(self.ysp) > 0) then
self.sprite:changeAnimation("walk", false)
else
self.sprite:changeAnimation("idle", false)
end
else
if (self.zsp) > 0 then
self.sprite:changeAnimation("jump", false)
else
self.sprite:changeAnimation("fall", false)
end
end
end
end
function SpritedPlayer:updateCurrentCharset() function SpritedPlayer:updateCurrentCharset()
self.charName = game.characters:getActiveCharacter() self.charName = game.characters:getActiveCharacter()
self:initAbstract() self:initAbstract()

View file

@ -0,0 +1,7 @@
local idleState = {}
idleState.canSwitch = true
idleState.canJump = true
idleState.defaultAnim = true
return idleState

View file

@ -0,0 +1,3 @@
return {
"idle",
}