示例#1
0
 private void Awake()
 {
     stateMachine = new FiniteStateMachine();
     stateMachine.Add(new FiniteStateMachineNode(Move), (int)PlayerInputStates.MOVE);
     stateMachine.Add(new FiniteStateMachineNode(Turn, TurnEnter, TurnExit), (int)PlayerInputStates.TURN);
     stateMachine.Start((int)PlayerInputStates.MOVE);
 }
示例#2
0
    void Awake()
    {
        if (instance == null)
        {
            instance = this;
            //dungeonTurn = CharacterFaction.PLAYER;
            Players             = new List <DungeonCharacter> ();
            Friendlies          = new List <DungeonCharacter> ();
            Enemies             = new List <DungeonCharacter> ();
            dungeonStateMachine = new FiniteStateMachine();

            dungeonStateMachine.Add(new FiniteStateMachineNode(PlayerTurn, PlayerTurnEnter, PlayerTurnExit), (int)CharacterFaction.PLAYER);
            dungeonStateMachine.Add(new FiniteStateMachineNode(FriendlyTurn, FriendlyTurnEnter, FriendlyTurnExit), (int)CharacterFaction.FRIENDLY);
            dungeonStateMachine.Add(new FiniteStateMachineNode(EnemyTurn, EnemyTurnEnter, EnemyTurnExit), (int)CharacterFaction.ENEMY);
            dungeonStateMachine.Start((int)CharacterFaction.PLAYER);
        }
    }
示例#3
0
        private void Start()
        {
            behaviorStateMachine = new FiniteStateMachine();
            behaviorStateMachine.Add((int)BehaviorState.Idle, new NPCStateNode(behaviorStateMachine, BehaviorState.Idle, this));
            behaviorStateMachine.Add((int)BehaviorState.Move, new NPCStateNode(behaviorStateMachine, BehaviorState.Move, this));
            behaviorStateMachine.Add((int)BehaviorState.Attack, new NPCStateNode(behaviorStateMachine, BehaviorState.Attack, this));
            behaviorStateMachine.Add((int)BehaviorState.Damaged, new NPCStateNode(behaviorStateMachine, BehaviorState.Damaged, this));
            behaviorStateMachine.Add((int)BehaviorState.Die, new NPCStateNode(behaviorStateMachine, BehaviorState.Die, this));

            Init_IdleState();
            Init_AttackState();
            Init_DieState();
            Init_DamageState();
            Init_MoveState(myMove);

            behaviorStateMachine.SetCurrentState(behaviorStateMachine.GetState((int)BehaviorState.Move));
            InvokeRepeating("TriggerAttack", walkTime, walkTime);
        }
示例#4
0
    // Start is called before the first frame update
    void Start()
    {
        mImageFilename = GameApp.Instance.JigsawImageFilename;

        mSplitImage.mImageFilename = mImageFilename;
        mSplitImage.mSpriteRenderer = mSpriteRenderer;
        mSplitImage.TilesParent = TilesParent;
        mSplitImage.mShadowMaterial = mShadowMaterial;
        //mSplitImage.mSortingLayer = mSortingLayer;

        mFsm.Add(new State((int)GameStates.LOADING, OnEnterLoading, null, null, null));
        mFsm.Add(new State((int)GameStates.SHUFFLING, OnEnterShuffling, null, null, null));
        mFsm.Add(new State((int)GameStates.PLAYING, OnEnterPlaying, null, OnUpdatePlaying, null));
        mFsm.Add(new State((int)GameStates.WIN, OnEnterWin, null, null, null));
        mFsm.Add(new State((int)GameStates.SHOW_SOLUTION, OnEnterShowSolution, OnExitShowSolution, OnUpdateShowSolution, null));

        mFsm.SetCurrentState((int)GameStates.LOADING);
    }
示例#5
0
        void Start()
        {
            mGrid           = new Transform[width, height];
            mLevelText.text = mLevel.ToString();

            CreateBoardSquares();

            mFsm.Add(
                new GameState(
                    GameState.StateID.NEW_LEVEL,
                    OnEnterNewLevel)
                );
            mFsm.Add(
                new GameState(
                    GameState.StateID.CLEAR_LINES,
                    OnEnterClearLines)
                );
            mFsm.Add(
                new GameState(
                    GameState.StateID.PLAYING,
                    OnEnterPlaying,
                    null,
                    OnUpdatePlaying)
                );
            mFsm.Add(
                new GameState(
                    GameState.StateID.LOST,
                    OnEnterLost)
                );

            mCanvasConfirm.onClickNo  = OnCancelExitGame;
            mCanvasConfirm.onClickYes = LoadMainMenu;
            mFsm.SetCurrentState((int)GameState.StateID.PLAYING);

            mMaxScoreText.text = mMaxScore.ToString();
            mLinesText.text    = GetLinesToClearLevel(mLevel).ToString();
        }
示例#6
0
        void Start()
        {
            mPlayerMovement = GetComponent <PlayerMovement>();
            mPlayerMovement.mOnReachDestination += OnReachDestination;

            mFsm.Add(
                new GameState(
                    GameState.StateID.NEW_GAME,
                    OnEnterNewGame)
                );
            mFsm.Add(
                new GameState(
                    GameState.StateID.GENERATING_MAZE,
                    OnEnterGeneratingMaze,
                    null,
                    OnUpdateGeneratingMaze)
                );
            mFsm.Add(
                new GameState(
                    GameState.StateID.PLAYING,
                    OnEnterPlaying,
                    null,
                    OnUpdatePlaying)
                );
            mFsm.Add(
                new GameState(
                    GameState.StateID.WIN,
                    OnEnterWin)
                );
            mFsm.Add(
                new GameState(
                    GameState.StateID.LOSE,
                    OnEnterLose)
                );
            mMenuHandler.onClickNextGame += NextGame;
            mFsm.SetCurrentState((int)GameState.StateID.NEW_GAME);
        }
示例#7
0
        // Start is called before the first frame update
        void Start()
        {
            mConfirmExit.onClickYes = LoadMenu;
            mConfirmExit.onClickNo  = DisableExitMenu;
            mFsm.Add(
                new GameState(
                    GameState.StateID.NEW_GAME,
                    OnEnterNewGame)
                );
            mFsm.Add(
                new GameState(
                    GameState.StateID.WAITNG_FOR_FIRST_CARD,
                    OnEnterWaitingForFirstCard,
                    null,
                    OnUpdateWaitingForFirstCard)
                );
            mFsm.Add(
                new GameState(
                    GameState.StateID.FIRST_CARD,
                    OnEnterFirstCard,
                    OnExitFirstCard,
                    OnUpdateFirstCard)
                );
            mFsm.Add(
                new GameState(
                    GameState.StateID.SECOND_CARD,
                    OnEnterSecondCard,
                    OnExitSecondCard,
                    OnUpdateSecondCard)
                );
            mFsm.Add(
                new GameState(
                    GameState.StateID.WIN,
                    OnEnterWin,
                    OnExitWin,
                    OnUpdateWin)
                );
            mFsm.Add(
                new GameState(
                    GameState.StateID.NO_INPUT_MODE,
                    null)
                );
            mFsm.Add(
                new GameState(
                    GameState.StateID.NEXT_GAME,
                    OnEnterNextGame)
                );

            mFsm.SetCurrentState((int)GameState.StateID.NEW_GAME);
        }
示例#8
0
    void Start()
    {
        mFSM.Add(new StateAIWin(this));
        mFSM.Add(new StateDraw(this));
        mFSM.Add(new StateNewGame(this));
        mFSM.Add(new StatePlayAI(this));
        mFSM.Add(new StatePlayerWin(this));
        mFSM.Add(new StatePlayPlayer(this));
        mFSM.Add(new StateReset(this));

        mFSM.SetCurrentState((int)StateTypes.NEW_GAME);

        //StartCoroutine(FadeOutText(1f, WinText));
        StartCoroutine(WaitAndFadeOut(4.0f, 1.0f, WinText));
    }
示例#9
0
        // Start is called before the first frame update
        void Start()
        {
            mFsm.Add(
                new GameState(
                    GameState.StateID.NEW_QUESTION,
                    OnEnterNewQuestion)
                );
            mFsm.Add(
                new GameState(
                    GameState.StateID.NO_RESPONSE,
                    OnEnterNoResponse)
                );
            mFsm.Add(
                new GameState(
                    GameState.StateID.RESPONSE,
                    OnEnterResponse)
                );
            mFsm.Add(
                new GameState(
                    GameState.StateID.TIMER,
                    OnEnterTimer,
                    null,
                    OnUpdateTimer)
                );
            mFsm.Add(
                new GameState(
                    GameState.StateID.SHOW_RESULTS,
                    OnEnterShowResults)
                );
            mFsm.Add(
                new GameState(
                    GameState.StateID.SHOW_FEEDBACK,
                    OnEnterShowfeedback)
                );
            mFsm.Add(
                new GameState(
                    GameState.StateID.EXIT,
                    OnEnterExit)
                );
            mFsm.SetCurrentState((int)GameState.StateID.NEW_QUESTION);

            mGameMenuHandler.onClickNextGame += OnNextQuestion;
        }
示例#10
0
        void Start()
        {
            mBallResetPos = mBall.transform.position;
            mBatResetPos  = mBat.transform.position;

            mFsm.Add(
                new GameState(
                    GameState.StateID.NEW_GAME,
                    OnEnterNewGame)
                );
            mFsm.Add(
                new GameState(
                    GameState.StateID.GENERATING,
                    OnEnterGenerating)
                );
            mFsm.Add(
                new GameState(
                    GameState.StateID.PUSHBALL,
                    OnEnterPushball)
                );
            mFsm.Add(
                new GameState(
                    GameState.StateID.PLAYING,
                    OnEnterPlaying)
                );
            mFsm.Add(
                new GameState(
                    GameState.StateID.WIN,
                    OnEnterWin)
                );
            mFsm.Add(
                new GameState(
                    GameState.StateID.LOSE,
                    OnEnterLose)
                );

            Load();

            mMenuHandler.onClickNextGame += NextGame;
            mFsm.SetCurrentState((int)GameState.StateID.NEW_GAME);
        }
示例#11
0
 public override void _Ready()
 {
     _input = new InputHandler <InputActions, string>(
         map: new Dictionary <InputActions, string>
     {
         { InputActions.MoveLeft, "ui_left" },
         { InputActions.MoveRight, "ui_right" },
         { InputActions.Jump, "ui_up" },
         { InputActions.Attack, "ui_accept" }
     },
         actionJustPressed: Input.IsActionJustPressed,
         actionPressed: Input.IsActionPressed,
         actionReleased: Input.IsActionJustReleased
         );
     _fsm = new FiniteStateMachine <PlayerState, Player>(equalizer: (current, captured) => current == captured)
     {
         InitialState = PlayerState.OnAir
     };
     _fsm.Add(PlayerState.OnAir, (current, player) =>
     {
         if (!IsOnFloor() && !IsOnWall())
         {
             GlobalVelocity += GravityVector;
             return(PlayerState.OnAir);
         }
         return(IsOnFloor() && !IsOnWall() ? PlayerState.OnGround : current);
     });
     _fsm.Add(PlayerState.OnGround, (current, player) =>
     {
         if (_input.IsActionPressed(InputActions.Jump))
         {
             var jump       = new Vector2(GlobalVelocity.x, -JumpForce);
             GlobalVelocity = jump;
             return(PlayerState.OnAir);
         }
         return(current);
     });
     _fsm.Add(PlayerState.OnGround, (current, player) =>
     {
         var horizontal = Vector2.Zero;
         if (_input.IsActionPressed(InputActions.MoveLeft))
         {
             horizontal = new Vector2((-1 * HorizontalSpeed), GlobalVelocity.y);
         }
         if (_input.IsActionPressed(InputActions.MoveRight))
         {
             horizontal = new Vector2((HorizontalSpeed), GlobalVelocity.y);
         }
         GlobalVelocity = horizontal;
         return(current);
     });
     _fsm.Add(PlayerState.OnAir, (current, player) =>
     {
         if (_wallTime > 0)
         {
             return(current);
         }
         var horizontal = Vector2.Zero;
         if (_input.IsActionPressed(InputActions.MoveLeft))
         {
             horizontal     = new Vector2((-1 * HorizontalSpeed), GlobalVelocity.y);
             GlobalVelocity = horizontal;
         }
         if (_input.IsActionPressed(InputActions.MoveRight))
         {
             horizontal     = new Vector2((HorizontalSpeed), GlobalVelocity.y);
             GlobalVelocity = horizontal;
         }
         return(current);
     });
     _fsm.Add(PlayerState.OnAir, (current, player) =>
     {
         if (IsOnFloor())
         {
             GlobalVelocity.x = 0;
             return(PlayerState.OnGround);
         }
         return(current);
     });
     _fsm.Add(PlayerState.OnAir, (current, player) =>
     {
         if (IsOnWall())
         {
             GlobalVelocity = GravityVector;
             var isLef      = _getCollisionNormal() == Vector2.Left;
             return(isLef ? PlayerState.OnRightWall : PlayerState.OnLeftWall);
         }
         return(current);
     });
     _fsm.Add(PlayerState.OnLeftWall, (current, player) => IsOnFloor() ? PlayerState.OnGround : current);
     _fsm.Add(PlayerState.OnRightWall, (current, player) => IsOnFloor() ? PlayerState.OnGround : current);
     _fsm.Add(PlayerState.OnGround, (current, player) => !IsOnFloor() ? PlayerState.OnAir : current);
     _fsm.Add(PlayerState.OnLeftWall, (current, player) =>
     {
         if (_input.IsActionPressed(InputActions.MoveRight))
         {
             var jump       = new Vector2(HorizontalSpeed, -JumpForce);
             GlobalVelocity = jump;
             return(PlayerState.OnAir);
         }
         return(current);
     });
     _fsm.Add(PlayerState.OnRightWall, (current, player) =>
     {
         if (!_input.IsActionPressed(InputActions.MoveRight))
         {
             return(PlayerState.OnAir);
         }
         if (_input.IsActionPressed(InputActions.MoveLeft))
         {
             var jump       = new Vector2(HorizontalSpeed * -1, -JumpForce);
             GlobalVelocity = jump;
             return(PlayerState.OnAir);
         }
         return(current);
     });
     _fsm.Add(PlayerState.OnLeftWall, (current, player) =>
     {
         if (!_input.IsActionPressed(InputActions.MoveLeft))
         {
             return(PlayerState.OnAir);
         }
         if (_input.IsActionJustPressed(InputActions.Jump))
         {
             var jump       = new Vector2(WallJumpHorizontalForce, -JumpForce);
             GlobalVelocity = jump;
             _wallTime      = WallTime;
             return(PlayerState.OnAir);
         }
         return(current);
     });
     _fsm.Add(PlayerState.OnRightWall, (current, player) =>
     {
         if (_input.IsActionJustPressed(InputActions.Jump))
         {
             var jump       = new Vector2(WallJumpHorizontalForce * -1, -JumpForce);
             GlobalVelocity = jump;
             _wallTime      = WallTime;
             return(PlayerState.OnAir);
         }
         return(current);
     });
 }
    public override void _Ready()
    {
        _xInicial  = Position.x;
        _xOriginal = _xInicial;
        _textLabel = GetNode(TextPath) as RichTextLabel;
        _input     = new AIInputHandler <InputActions>();
        _fsm       = new FiniteStateMachine <PlayerState, Malo>(equalizer: (current, captured) => current == captured)
        {
            InitialState = PlayerState.OnAir
        };
        _enemyState = new FiniteStateMachine <EnemyState, Malo>(equalizer: (current, captured) => current == captured)
        {
            InitialState = EnemyState.MoviendoDerecha
        };
        _fsm.Add(PlayerState.OnAir, (current, player) =>
        {
            if (!IsOnFloor() && !IsOnWall())
            {
                GlobalVelocity += GravityVector;
                return(PlayerState.OnAir);
            }
            return(IsOnFloor() && !IsOnWall() ? PlayerState.OnGround : current);
        });
        _fsm.Add(PlayerState.OnGround, (current, player) =>
        {
            if (_input.IsActionPressed(InputActions.Jump))
            {
                var jump       = new Vector2(GlobalVelocity.x, -JumpForce);
                GlobalVelocity = jump;
                return(PlayerState.OnAir);
            }
            return(current);
        });
        _fsm.Add(PlayerState.OnGround, (current, player) =>
        {
            var horizontal = Vector2.Zero;
            if (_input.IsActionPressed(InputActions.MoveLeft))
            {
                horizontal = new Vector2((-1 * HorizontalSpeed), GlobalVelocity.y);
            }
            if (_input.IsActionPressed(InputActions.MoveRight))
            {
                horizontal = new Vector2((HorizontalSpeed), GlobalVelocity.y);
            }
            GlobalVelocity = horizontal;
            return(current);
        });
        _fsm.Add(PlayerState.OnAir, (current, player) =>
        {
            if (_wallTime > 0)
            {
                return(current);
            }
            var horizontal = Vector2.Zero;
            if (_input.IsActionPressed(InputActions.MoveLeft))
            {
                horizontal     = new Vector2((-1 * HorizontalSpeed), GlobalVelocity.y);
                GlobalVelocity = horizontal;
            }
            if (_input.IsActionPressed(InputActions.MoveRight))
            {
                horizontal     = new Vector2((HorizontalSpeed), GlobalVelocity.y);
                GlobalVelocity = horizontal;
            }
            return(current);
        });
        _fsm.Add(PlayerState.OnAir, (current, player) =>
        {
            if (IsOnFloor())
            {
                GlobalVelocity.x = 0;
                return(PlayerState.OnGround);
            }
            return(current);
        });
        _fsm.Add(PlayerState.OnAir, (current, player) =>
        {
            if (IsOnWall())
            {
                GlobalVelocity = GravityVector;
                var isLef      = _getCollisionNormal() == Vector2.Left;
                return(isLef ? PlayerState.OnRightWall : PlayerState.OnLeftWall);
            }
            return(current);
        });
        _fsm.Add(PlayerState.OnLeftWall, (current, delta) => IsOnFloor() ? PlayerState.OnGround : current);
        _fsm.Add(PlayerState.OnRightWall, (current, delta) => IsOnFloor() ? PlayerState.OnGround : current);
        _fsm.Add(PlayerState.OnLeftWall, (current, delta) =>
        {
            if (_input.IsActionPressed(InputActions.MoveRight))
            {
                var jump       = new Vector2(HorizontalSpeed, -JumpForce);
                GlobalVelocity = jump;
                return(PlayerState.OnAir);
            }
            return(current);
        });
        _fsm.Add(PlayerState.OnRightWall, (current, player) =>
        {
            if (_input.IsActionPressed(InputActions.MoveLeft))
            {
                var jump       = new Vector2(HorizontalSpeed * -1, -JumpForce);
                GlobalVelocity = jump;
                return(PlayerState.OnAir);
            }
            return(current);
        });
        _fsm.Add(PlayerState.OnLeftWall, (current, player) =>
        {
            if (_input.IsActionJustPressed(InputActions.Jump))
            {
                var jump       = new Vector2(WallJumpHorizontalForce, -JumpForce);
                GlobalVelocity = jump;
                _wallTime      = WallTime;
                return(PlayerState.OnAir);
            }
            return(current);
        });
        _fsm.Add(PlayerState.OnRightWall, (current, player) =>
        {
            if (_input.IsActionJustPressed(InputActions.Jump))
            {
                var jump       = new Vector2(WallJumpHorizontalForce * -1, -JumpForce);
                GlobalVelocity = jump;
                _wallTime      = WallTime;
                return(PlayerState.OnAir);
            }
            return(current);
        });

        _enemyState.Add(EnemyState.MoviendoDerecha, (current, enemy) =>
        {
            _estadoAnterior = current;
            Player jugador  = DetectarYPerseguir();

            if (_caminarHasta != 0)
            {
                HorizontalSpeed = _velocidadNormal * 4;
            }
            if (Mathf.Abs(Position.x - _caminarHasta) <= 10)
            {
                HorizontalSpeed = _velocidadNormal;

                if (_caminarHasta != 0)
                {
                    _caminarHasta = 0;
                    _xInicial     = Position.x;
                    _tiempoAlerta = 30;
                    return(EnemyState.AlertaDerecha);
                }
            }

            if (jugador != null && Mathf.Abs(Position.x - jugador.Position.x) <= 50)
            {
                _input.SetActionReleased(InputActions.MoveRight);
                return(EnemyState.Atacando);
            }

            if (IsOnWall())
            {
                _input.SetActionPressed(InputActions.MoveLeft);
                _input.SetActionReleased(InputActions.MoveRight);
                return(EnemyState.MoviendoIzquierda);
            }
            _input.SetActionPressed(InputActions.MoveRight);
            _input.SetActionReleased(InputActions.MoveLeft);

            var bodies = GetNode <Area2D>("Area2D2")?.GetOverlappingBodies() ?? new Godot.Collections.Array();

            foreach (var body in bodies)
            {
                jugador = body as Player;
                if (jugador != null && !jugador.Cubierto)
                {
                    GD.Print($"{DateTime.Now.ToString("G")}");
                    //_input.SetActionPressed(InputActions.MoveRight);
                    //_input.SetActionReleased(InputActions.MoveLeft);
                    return(EnemyState.MoviendoIzquierda);
                }
            }
            return(current);
        });

        _enemyState.Add(EnemyState.MoviendoIzquierda, (current, enemy) =>
        {
            _estadoAnterior = current;
            Player jugador  = DetectarYPerseguir();

            if (_caminarHasta != 0)
            {
                HorizontalSpeed = _velocidadNormal * 4;
            }
            if (Mathf.Abs(Position.x - _caminarHasta) <= 10)
            {
                HorizontalSpeed = _velocidadNormal;

                if (_caminarHasta != 0)
                {
                    _caminarHasta = 0;
                    _xInicial     = Position.x;
                    _tiempoAlerta = 30;
                    return(EnemyState.AlertaIzquierda);
                }
            }

            if (jugador != null && Mathf.Abs(Position.x - jugador.Position.x) <= 50)
            {
                _input.SetActionReleased(InputActions.MoveLeft);
                return(EnemyState.Atacando);
            }

            if (IsOnWall())
            {
                _input.SetActionPressed(InputActions.MoveRight);
                _input.SetActionReleased(InputActions.MoveLeft);
                return(EnemyState.MoviendoDerecha);
            }
            _input.SetActionPressed(InputActions.MoveLeft);
            _input.SetActionReleased(InputActions.MoveRight);

            var bodies = GetNode <Area2D>("Area2D2")?.GetOverlappingBodies() ?? new Godot.Collections.Array();

            foreach (var body in bodies)
            {
                jugador = body as Player;
                if (jugador != null && !jugador.Cubierto)
                {
                    GD.Print($"{DateTime.Now.ToString("G")}");
                    //_input.SetActionPressed(InputActions.MoveLeft);
                    //_input.SetActionReleased(InputActions.MoveRight);
                    return(EnemyState.MoviendoDerecha);
                }
            }
            return(current);
        });

        _enemyState.Add(EnemyState.AlertaIzquierda, (current, enemy) =>
        {
            _ = DetectarYPerseguir();

            var bodies = GetNode <Area2D>("Area2D2")?.GetOverlappingBodies() ?? new Godot.Collections.Array();

            foreach (var body in bodies)
            {
                if (body is Player jugador && !jugador.Cubierto)
                {
                    GD.Print($"{DateTime.Now:G}");
                    _input.SetActionPressed(InputActions.MoveRight);
                    _input.SetActionReleased(InputActions.MoveLeft);
                    return(EnemyState.AlertaDerecha);
                }
            }
示例#13
0
 public override void _Ready()
 {
     _textLabel = GetNode(TextPath) as RichTextLabel;
     _input     = new AIInputHandler <InputActions>();
     _fsm       = new FiniteStateMachine <PlayerState, Malo>(equalizer: (current, captured) => current == captured)
     {
         InitialState = PlayerState.OnAir
     };
     _fsm.Add(PlayerState.OnAir, (current, player) =>
     {
         if (!IsOnFloor() && !IsOnWall())
         {
             GlobalVelocity += GravityVector;
             return(PlayerState.OnAir);
         }
         return(IsOnFloor() && !IsOnWall() ? PlayerState.OnGround : current);
     });
     _fsm.Add(PlayerState.OnGround, (current, player) =>
     {
         if (_input.IsActionPressed(InputActions.Jump))
         {
             var jump       = new Vector2(GlobalVelocity.x, -JumpForce);
             GlobalVelocity = jump;
             return(PlayerState.OnAir);
         }
         return(current);
     });
     _fsm.Add(PlayerState.OnGround, (current, player) =>
     {
         var horizontal = Vector2.Zero;
         if (_input.IsActionPressed(InputActions.MoveLeft))
         {
             horizontal = new Vector2((-1 * HorizontalSpeed), GlobalVelocity.y);
         }
         if (_input.IsActionPressed(InputActions.MoveRight))
         {
             horizontal = new Vector2((HorizontalSpeed), GlobalVelocity.y);
         }
         GlobalVelocity = horizontal;
         return(current);
     });
     _fsm.Add(PlayerState.OnAir, (current, player) =>
     {
         if (_wallTime > 0)
         {
             return(current);
         }
         var horizontal = Vector2.Zero;
         if (_input.IsActionPressed(InputActions.MoveLeft))
         {
             horizontal     = new Vector2((-1 * HorizontalSpeed), GlobalVelocity.y);
             GlobalVelocity = horizontal;
         }
         if (_input.IsActionPressed(InputActions.MoveRight))
         {
             horizontal     = new Vector2((HorizontalSpeed), GlobalVelocity.y);
             GlobalVelocity = horizontal;
         }
         return(current);
     });
     _fsm.Add(PlayerState.OnAir, (current, player) =>
     {
         if (IsOnFloor())
         {
             GlobalVelocity.x = 0;
             return(PlayerState.OnGround);
         }
         return(current);
     });
     _fsm.Add(PlayerState.OnAir, (current, player) =>
     {
         if (IsOnWall())
         {
             GlobalVelocity = GravityVector;
             var isLef      = _getCollisionNormal() == Vector2.Left;
             return(isLef ? PlayerState.OnRightWall : PlayerState.OnLeftWall);
         }
         return(current);
     });
     _fsm.Add(PlayerState.OnLeftWall, (current, delta) => IsOnFloor() ? PlayerState.OnGround : current);
     _fsm.Add(PlayerState.OnRightWall, (current, delta) => IsOnFloor() ? PlayerState.OnGround : current);
     _fsm.Add(PlayerState.OnLeftWall, (current, delta) =>
     {
         if (_input.IsActionPressed(InputActions.MoveRight))
         {
             var jump       = new Vector2(HorizontalSpeed, -JumpForce);
             GlobalVelocity = jump;
             return(PlayerState.OnAir);
         }
         return(current);
     });
     _fsm.Add(PlayerState.OnRightWall, (current, player) =>
     {
         if (_input.IsActionPressed(InputActions.MoveLeft))
         {
             var jump       = new Vector2(HorizontalSpeed * -1, -JumpForce);
             GlobalVelocity = jump;
             return(PlayerState.OnAir);
         }
         return(current);
     });
     _fsm.Add(PlayerState.OnLeftWall, (current, player) =>
     {
         if (_input.IsActionJustPressed(InputActions.Jump))
         {
             var jump       = new Vector2(WallJumpHorizontalForce, -JumpForce);
             GlobalVelocity = jump;
             _wallTime      = WallTime;
             return(PlayerState.OnAir);
         }
         return(current);
     });
     _fsm.Add(PlayerState.OnRightWall, (current, player) =>
     {
         if (_input.IsActionJustPressed(InputActions.Jump))
         {
             var jump       = new Vector2(WallJumpHorizontalForce * -1, -JumpForce);
             GlobalVelocity = jump;
             _wallTime      = WallTime;
             return(PlayerState.OnAir);
         }
         return(current);
     });
     _bt = Bt.Root(
         _bt.Function(x => {
         if (!x.IsOnWall() || x.IsOnWall() && x._getCollisionNormal() == Vector2.Left)
         {
             x._input.SetActionPressed(InputActions.MoveLeft);
             return(Node <Malo> .Status.Prossess);
         }
         else
         {
             x._input.SetActionReleased(InputActions.MoveLeft);
             return(Node <Malo> .Status.Success);
         }
     }),
         _bt.Wait(5f),
         _bt.Function(x => {
         if (!x.IsOnWall() || x.IsOnWall() && x._getCollisionNormal() == Vector2.Right)
         {
             x._input.SetActionPressed(InputActions.MoveRight);
             return(Node <Malo> .Status.Prossess);
         }
         else
         {
             x._input.SetActionReleased(InputActions.MoveRight);
             return(Node <Malo> .Status.Success);
         }
     }),
         _bt.Wait(5f)
         );
 }
示例#14
0
文件: FSM.cs 项目: GF47/GRT
        public void TestFSM()
        {
            var fsm = new FiniteStateMachine();

            var a = new NormalState(AID);
            var b = new NormalState(BID);
            var c = new NormalState(CID);
            var d = new NormalState(DID);

            var abCondition  = new StringTriggerCondition(TRIGGER_AB);
            var abCondition2 = new DelegateCondition(GetCondition);
            var bcCondition  = new StringTriggerCondition(TRIGGER_BC);
            var bdCondition  = new TriggerCondition <string>(TRIGGER_BD);
            var caCondition  = new DelegateCondition(GetCondition);
            var daCondition  = new TriggerCondition <int>(DID);

            var a2b = new Transition(b.ID, new ICondition[] { abCondition, abCondition2 });
            var b2c = new Transition(c.ID, new ICondition[] { bcCondition });
            var b2d = new Transition(d.ID, new ICondition[] { bdCondition });
            var c2a = new Transition(a.ID, new ICondition[] { caCondition });
            var d2a = new Transition(a.ID, new ICondition[] { daCondition });

            a.AddNext(a2b);
            b.AddNext(b2c);
            b.AddNext(b2d);
            c.AddNext(c2a);
            d.AddNext(d2a);

            fsm.Add(a);
            fsm.Add(b);
            fsm.Add(c);
            fsm.Add(d);

            fsm.StartWith(a.ID);

            /**************************************************************/

            fsm.Update();
            Assert.AreEqual(AID, fsm.CurrentID);

            fsm.Trigger(TRIGGER_AB);
            fsm.Update();
            Assert.AreEqual(AID, fsm.CurrentID);

            _condition = true;
            fsm.Trigger(TRIGGER_AB);
            fsm.Update();
            Assert.AreEqual(BID, fsm.CurrentID);

            /*/// hard code switch A
             * fsm.Trigger(TRIGGER_BC);
             * fsm.Update();
             * Assert.AreEqual(CID, fsm.CurrentID);
             *
             * // _condition = false;
             * // fsm.Update();
             * // Assert.AreEqual(CID, fsm.CurrentID);
             *
             * // _condition = true;
             * fsm.Update();
             * Assert.AreEqual(AID, fsm.CurrentID);
             *
             * /*/// hard code switch B
            fsm.Trigger(TRIGGER_BD);
            fsm.Update();
            Assert.AreEqual(DID, fsm.CurrentID);

            fsm.Trigger(DID);
            fsm.Update();
            Assert.AreEqual(AID, fsm.CurrentID);

            //*/// hard code switch end
            fsm.Update();
            fsm.Update();
            fsm.Update();
            fsm.Update();
            fsm.Update();
            Assert.AreEqual(AID, fsm.CurrentID);
        }
    public override void _Ready()
    {
        _beforePosition = Position;
        GetNode <Kemono>("Slot/Kemono").Rotation = 0;
        _input = new InputHandler <InputActions, string>(
            map: new Dictionary <InputActions, string>
        {
            { InputActions.MoveLeft, "Izquierda" },
            { InputActions.MoveRight, "Derecha" },
            { InputActions.Jump, "Arriba" },
            { InputActions.Attack, "ui_accept" },
            { InputActions.Dash, "dash" }
        },
            actionJustPressed: Input.IsActionJustPressed,
            actionPressed: Input.IsActionPressed,
            actionReleased: Input.IsActionJustReleased
            );
        _fsm = new FiniteStateMachine <PlayerState, Player>(equalizer: (current, captured) => current == captured)
        {
            InitialState = PlayerState.OnAir
        };
        _fsm.Add(PlayerState.OnAir, (current, player) =>
        {
            if (!IsOnFloor() && !IsOnWall())
            {
                GlobalVelocity += GravityVector;
                return(PlayerState.OnAir);
            }
            return(IsOnFloor() && !IsOnWall() ? PlayerState.OnGround : current);
        });
        _fsm.Add(PlayerState.OnGround, (current, player) =>
        {
            if (_input.IsActionPressed(InputActions.Jump))
            {
                var jump       = new Vector2(GlobalVelocity.x, -JumpForce);
                GlobalVelocity = jump;
                GetNode <Kemono>("Slot/Kemono").Saltar();
                return(PlayerState.OnAir);
            }
            return(current);
        });
        _fsm.Add(PlayerState.OnGround, (current, player) =>
        {
            if (_input.IsActionPressed(InputActions.Dash))
            {
                return(current);
            }

            var horizontal = Vector2.Zero;
            var correr     = false;
            if (_input.IsActionPressed(InputActions.MoveLeft))
            {
                horizontal = new Vector2((-1 * HorizontalSpeed), GlobalVelocity.y);
                correr     = true;
            }
            if (_input.IsActionPressed(InputActions.MoveRight))
            {
                horizontal = new Vector2((HorizontalSpeed), GlobalVelocity.y);
                correr     = true;
            }
            if (correr)
            {
                GetNode <Kemono>("Slot/Kemono").Correr();
            }
            else
            {
                GetNode <Kemono>("Slot/Kemono").Quieto();
            }
            GlobalVelocity = horizontal;
            return(current);
        });
        _fsm.Add(PlayerState.OnAir, (current, player) =>
        {
            if (_wallTime > 0)
            {
                return(current);
            }
            var horizontal = Vector2.Zero;
            if (_input.IsActionPressed(InputActions.MoveLeft) && !_input.IsActionPressed(InputActions.MoveRight))
            {
                horizontal     = new Vector2((-1 * HorizontalSpeed), GlobalVelocity.y);
                GlobalVelocity = horizontal;
                return(current);
            }
            if (_input.IsActionPressed(InputActions.MoveRight) && !_input.IsActionPressed(InputActions.MoveLeft))
            {
                horizontal     = new Vector2((HorizontalSpeed), GlobalVelocity.y);
                GlobalVelocity = horizontal;
                return(current);
            }
            return(current);
        });
        _fsm.Add(PlayerState.OnAir, (current, player) =>
        {
            if (IsOnFloor())
            {
                GlobalVelocity.x = 0;
                return(PlayerState.OnGround);
            }
            return(current);
        });
        _fsm.Add(PlayerState.OnAir, (current, player) =>
        {
            if (IsOnWall())
            {
                GlobalVelocity = GravityVector;
                var isRight    = _getCollisionNormal() == Vector2.Left;
                return(isRight ? PlayerState.OnRightWall : PlayerState.OnLeftWall);
            }
            return(current);
        });
        _fsm.Add(PlayerState.OnLeftWall, (current, player) =>
        {
            if (_input.IsActionJustPressed(InputActions.Jump))
            {
                var jump       = new Vector2(WallJumpHorizontalForce, -JumpForce);
                GlobalVelocity = jump;
                _wallTime      = WallTime;
                return(PlayerState.OnAir);
            }
            if (!_input.IsActionPressed(InputActions.MoveLeft) && !_input.IsActionPressed(InputActions.Jump) || !GetNode <Left>("Left").LeftCast())
            {
                return(PlayerState.OnAir);
            }
            return(current);
        });
        _fsm.Add(PlayerState.OnLeftWall, (current, player) =>
        {
            if (_input.IsActionJustPressed(InputActions.MoveRight))
            {
                var jump       = new Vector2(JumpFromWallHorizontalForce, -JumpForce);
                GlobalVelocity = jump;
                _wallTime      = 0;
                return(PlayerState.OnAir);
            }
            if (!_input.IsActionPressed(InputActions.MoveLeft) && !_input.IsActionPressed(InputActions.Jump) || !GetNode <Left>("Left").LeftCast())
            {
                return(PlayerState.OnAir);
            }
            return(current);
        });
        _fsm.Add(PlayerState.OnRightWall, (current, player) =>
        {
            if (_input.IsActionJustPressed(InputActions.MoveLeft))
            {
                var jump       = new Vector2(JumpFromWallHorizontalForce * -1f, -JumpForce);
                GlobalVelocity = jump;
                _wallTime      = 0;
                return(PlayerState.OnAir);
            }
            if (!_input.IsActionPressed(InputActions.MoveRight) && !_input.IsActionPressed(InputActions.Jump) || !GetNode <Right>("Right").RightCast())
            {
                return(PlayerState.OnAir);
            }
            return(current);
        });
        _fsm.Add(PlayerState.OnRightWall, (current, player) =>
        {
            if (_input.IsActionJustPressed(InputActions.Jump))
            {
                var jump       = new Vector2(WallJumpHorizontalForce * -1, -JumpForce);
                GlobalVelocity = jump;
                _wallTime      = WallTime;
                return(PlayerState.OnAir);
            }
            if (!_input.IsActionPressed(InputActions.MoveRight) && !_input.IsActionPressed(InputActions.Jump) || !GetNode <Right>("Right").RightCast())
            {
                return(PlayerState.OnAir);
            }
            return(current);
        });
        _fsm.Add(PlayerState.OnGround, (current, player) =>
        {
            if (_input.IsActionPressed(InputActions.Dash) && !_dashed || _dashTime > 0)
            {
                _dashed  = true;
                var dash = new Vector2(GlobalVelocity.x * DashFactor, GlobalVelocity.y);
                if (_dashTime == 0)
                {
                    _dashTime = 0.25f;
                }
                GlobalVelocity = dash;
            }
            if (!_input.IsActionPressed(InputActions.Dash))
            {
                _dashed = false;
            }
            return(current);
        });
        _fsm.Add(PlayerState.OnGround, (current, player) => !IsOnFloor() && !IsOnWall() ? PlayerState.OnAir : current);
    }