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()
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)
direction = direction or 0
if (direction ~= 0) then

View file

@ -3,16 +3,18 @@ local FighterParent = Parent:extend()
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"
local Abstract = require "game.modules.subgames.world.actors.fighters.mixins.abstract"
FighterParent:implement(Actions)
FighterParent:implement(Movements)
FighterParent:implement(Sprites)
FighterParent:implement(Abstract)
local states = {"idle"}
FighterParent:addStates("game.modules.subgames.world.actors.fighters.states", "idle")
function FighterParent:new(world, x, y, fighterType)
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)
self.defaultDir = self.defaultDir or -1
self:initMovements()
self:initActions()
self.tweens = TweenManager(self)
end

View file

@ -55,7 +55,7 @@ function PlayerControls:dpadToAngle()
end
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()
end
end
@ -68,12 +68,14 @@ function PlayerControls:applyActionsInputs()
end
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()
end
if self.keys["R1"].isPressed and (self.action == "normal") and (self.onGround) then
if self.keys["R1"].isPressed then
self:switchActiveCharacter(-1)
end
end
end
return PlayerControls

View file

@ -9,26 +9,6 @@ function SpritedPlayer:getCustomSpeed()
return math.abs(gsp) / 12
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()
self.charName = game.characters:getActiveCharacter()
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",
}