// Use this for initialization
    void Start()
    {
        spriteRenderer = gameObject.GetComponentInChildren <SpriteRenderer> ();
        shadow         = gameObject.GetComponentInChildren <MeshRenderer>();
        gc             = GameController.Instance;
        rb             = GetComponent <Rigidbody> ();
        maxHealth      = health;
        Reset();
        Hide();

        //STATE MACHINE INITIALISATION
        fsm        = new FSM();
        idle       = new Idle();
        move       = new Move();
        attack     = new Attack();
        moveAttack = new MoveAttack();

        //idle:			moveVec.sqrMagnitude == 0 && shootVec.sqrMagnitude == 0
        //move:			moveVec.sqrMagnitude != 0 && shootVec.sqrMagnitude == 0
        //attack:		moveVec.sqrMagnitude == 0 && shootVec.sqrMagnitude != 0
        //moveattack:	moveVec.sqrMagnitude != 0 && shootVec.sqrMagnitude != 0

        idle.AddTransition(new Transition(move, () => moveVec.sqrMagnitude != 0 && shootVec.sqrMagnitude == 0));
        idle.AddTransition(new Transition(attack, () => moveVec.sqrMagnitude == 0 && shootVec.sqrMagnitude != 0));
        idle.AddTransition(new Transition(moveAttack, () => moveVec.sqrMagnitude != 0 && shootVec.sqrMagnitude != 0));

        move.AddTransition(new Transition(idle, () => moveVec.sqrMagnitude == 0 && shootVec.sqrMagnitude == 0));
        move.AddTransition(new Transition(attack, () => moveVec.sqrMagnitude == 0 && shootVec.sqrMagnitude != 0));
        move.AddTransition(new Transition(moveAttack, () => moveVec.sqrMagnitude != 0 && shootVec.sqrMagnitude != 0));

        //attack not moving
        attack.AddTransition(new Transition(move, () => moveVec.sqrMagnitude != 0 && shootVec.sqrMagnitude == 0 && !CurrentWeaponScript.shootDelaying));
        attack.AddTransition(new Transition(idle, () => moveVec.sqrMagnitude == 0 && shootVec.sqrMagnitude == 0 && !CurrentWeaponScript.shootDelaying));
        attack.AddTransition(new Transition(moveAttack, () => moveVec.sqrMagnitude != 0 && shootVec.sqrMagnitude != 0));

        //attack while moving
        moveAttack.AddTransition(new Transition(move, () => moveVec.sqrMagnitude != 0 && shootVec.sqrMagnitude == 0 && !CurrentWeaponScript.shootDelaying));
        moveAttack.AddTransition(new Transition(idle, () => moveVec.sqrMagnitude == 0 && shootVec.sqrMagnitude == 0 && !CurrentWeaponScript.shootDelaying));
        moveAttack.AddTransition(new Transition(attack, () => moveVec.sqrMagnitude == 0 && shootVec.sqrMagnitude != 0));


        fsm.AddState(idle);
        fsm.AddState(move);
        fsm.AddState(attack);

        fsm.Init(idle, Directions.Unspecified, Directions.Unspecified);
    }
Esempio n. 2
0
        protected override GUIFSM <ListBox <T> > CreateFSM()
        {
            var idle = new Idle();

            idle.AddTransition(GUIEventID.FocusGained, 1);
            var focused = new FocusedState();

            focused.AddTransition(GUIEventID.FocusLost, 0);

            return(new GUIFSM <ListBox <T> >(this, new GUIState <ListBox <T> >[] { idle, focused }));
        }
    private void MakeFSM()
    {
        fsm = new SimpleAIFSMSystem();

        Idle idle =  new Idle(fsm, gameObject, pc);
        idle.AddTransition(SimpleAITransition.FoundPlayer, SimpleAIStateID.ChasePlayer);

        ChasePlayer chasePlayer = new ChasePlayer(fsm, gameObject, pc);
        chasePlayer.AddTransition(SimpleAITransition.LostPlayer, SimpleAIStateID.Idle);

        fsm.AddState(idle);
        fsm.AddState(chasePlayer);
    }
Esempio n. 4
0
        protected override GUIFSM <AnimationBox> CreateFSM()
        {
            var idle = new Idle();

            idle.AddTransition(GUIEventID.MouseEnter, 1);
            idle.AddTransition(GUIEventID.FocusGained, 2);

            var hover = new AnimUpdate();

            hover.AddTransition(GUIEventID.MouseExit, 0);
            hover.AddTransition(GUIEventID.FocusGained, 3);

            var focus = new AnimUpdate();

            focus.AddTransition(GUIEventID.FocusLost, 0);
            focus.AddTransition(GUIEventID.MouseEnter, 3);

            var focusH = new AnimUpdate();

            focusH.AddTransition(GUIEventID.FocusLost, 1);
            focusH.AddTransition(GUIEventID.MouseExit, 2);

            return(new GUIFSM <AnimationBox>(this, new GUIState <AnimationBox>[] { idle, hover, focus, focusH }));
        }
        protected override void InitialiseBrain()
        {
            brain = new StateMachine.FSM(this);
            var start = new Start();
            var idle1 = new Idle();
            var idle2 = new Idle();
            var up    = new Up(this);
            var down  = new Down(this);

            //Transitions out of start state
            start.AddTransition(down, () => { return(start.down); });
            start.AddTransition(up, () => { return(start.up); });

            //Behaviour loop: down -> idle -> up -> idle -> down
            idle1.AddTransition(down, () => { return(idle1.DurationOver); });
            down.AddTransition(idle2, () => { return(down.Done); });
            idle2.AddTransition(up, () => { return(idle2.DurationOver); });
            up.AddTransition(idle1, () => { return(up.Done); });

            brain.AddStates(start, idle1, idle2, up, down);
            brain.Initialise("Start");
        }
        public StateController <CharacterState, CharacterContext> BuildCharacterControllerImpl(StateControllerBuilder <CharacterState, CharacterContext> builder)
        {
            Builder = builder;
            InjectState(this);

            // Declare Smash Attacks
            SmashUp.Charge.Data.SmashAttack   = SmashAttack.Charge;
            SmashSide.Charge.Data.SmashAttack = SmashAttack.Charge;
            SmashDown.Charge.Data.SmashAttack = SmashAttack.Charge;

            SmashUp.Attack.Data.SmashAttack   = SmashAttack.Attack;
            SmashSide.Attack.Data.SmashAttack = SmashAttack.Attack;
            SmashDown.Attack.Data.SmashAttack = SmashAttack.Attack;

            // Ground Attacks
            new [] { Idle, Walk, CrouchStart, Crouch, CrouchEnd }
            // Smash Attacks
            .AddTransitions <CharacterState, CharacterContext>(context => {
                var input = context.Input;
                if (!input.Attack.WasPressed)
                {
                    return(null);
                }
                switch (input.Smash.Direction)
                {
                case Direction.Right:
                case Direction.Left:
                    return(SmashSide.Charge);

                case Direction.Up:
                    return(SmashUp.Charge);

                case Direction.Down:
                    return(SmashDown.Charge);
                }
                return(null);
            })
            // Tilt Attacks
            .AddTransitions <CharacterState, CharacterContext>(context => {
                var input = context.Input;
                if (!input.Attack.WasPressed)
                {
                    return(null);
                }
                switch (input.Movement.Direction)
                {
                case Direction.Right:
                case Direction.Left:
                    return(TiltSide);

                case Direction.Up:
                    return(TiltUp);

                case Direction.Down:
                    return(TiltDown);
                }
                return(Neutral);
            });
            SmashUp.Charge.AddTransitionTo(SmashUp.Attack);
            SmashDown.Charge.AddTransitionTo(SmashDown.Attack);
            SmashSide.Charge.AddTransitionTo(SmashSide.Attack);
            TiltDown.AddTransitionTo(Crouch, Input(i => i.Movement.Direction == Direction.Down));
            new[] { Neutral, TiltUp, TiltDown, TiltSide, SmashUp.Attack, SmashDown.Attack, SmashSide.Attack }
            .AddTransitionTo(Idle);

            new [] { Fall, Jump, JumpAerial }
            .AddTransitions(Land, ctx => ctx.IsGrounded)
            // Aerial Attacks
            .AddTransitions <CharacterState, CharacterContext>(context => {
                var input = context.Input;
                if (!input.Attack.WasPressed)
                {
                    return(null);
                }
                switch (input.Movement.Direction)
                {
                case Direction.Right:
                    return(context.Direction >= 0f ? AerialForward : AerialBackward);

                case Direction.Left:
                    return(context.Direction >= 0f ? AerialBackward : AerialForward);

                case Direction.Up:
                    return(AerialUp);

                case Direction.Down:
                    return(AerialDown);
                }
                return(AerialNeutral);
            });
            new[] { AerialForward, AerialBackward, AerialDown, AerialUp, AerialNeutral }
            .AddTransitions(AerialAttackLand, ctx => ctx.IsGrounded)
            .AddTransitionTo(Fall);
            AerialAttackLand.AddTransitionTo(Idle);

            // Aerial Movement
            new [] { Idle, Walk, Dash, Run, RunTurn, RunBrake, CrouchStart, Crouch, CrouchEnd, Shield.Main }
            .AddTransitions(JumpStart, ctx => ctx.Input.Jump.WasPressed && ctx.CanJump);
            new[] { JumpStart, JumpAerial }.AddTransitionTo(Jump);
            new[] { Jump, Fall }.AddTransitions(JumpAerial, ctx => ctx.Input.Jump.WasPressed && ctx.CanJump)
            .AddTransitions(EscapeAir, Input(i => i.Shield.WasPressed));
            Jump.AddTransition(Idle, ctx => ctx.NormalizedStateTime >= 1.0f && ctx.IsGrounded)
            .AddTransition(Fall, ctx => ctx.NormalizedStateTime >= 1.0f && !ctx.IsGrounded);
            EscapeAir.AddTransitionTo(FallHelpless);
            new[] { Fall, FallHelpless, EscapeAir }.AddTransitions(Land, ctx => ctx.IsGrounded);
            Land.AddTransitionTo(Idle);

            Func <Func <PlayerInputContext, DirectionalInput>, Func <CharacterContext, bool> >
            movementContext = func => {
                return(ctx => !DirectionInput(Direction.Down)(ctx) &&
                       Input(i => Mathf.Abs(func(i).Value.x) > DirectionalInput.DeadZone)(ctx));
            };

            // Running States
            Idle.AddTransition(Dash, movementContext(i => i.Smash));
            Dash.AddTransitionTo(Idle, DirectionInput(Direction.Neutral));
            new[] { Dash, RunTurn }.AddTransitionTo(Run);
            Run.AddTransition(RunBrake, DirectionInput(Direction.Neutral));
            Run.AddTransition(RunTurn,
                              ctx => !Mathf.Approximately(Mathf.Sign(ctx.Input.Movement.Value.x), Mathf.Sign(ctx.Direction)));
            RunBrake.AddTransitionTo(Idle);

            // Ground Movement
            new[] { Idle, Walk, Run }
            .AddTransitions(CrouchStart, DirectionInput(Direction.Down))
            .AddTransitions(Fall, ctx => !ctx.IsGrounded);

            Idle.AddTransition(Walk, movementContext(i => i.Movement));
            Walk.AddTransition(Idle, DirectionInput(Direction.Neutral));

            // Crouching States
            CrouchStart.AddTransitionTo(Crouch);
            CrouchEnd.AddTransitionTo(Idle);
            new[] { CrouchStart, Crouch, CrouchEnd }.AddTransitions(Fall, ctx => !ctx.IsGrounded);
            Crouch.AddTransition(CrouchEnd, Input(i => i.Movement.Direction != Direction.Down));

            // Ledge States
            new[] { Idle, Fall, FallHelpless }.AddTransitions(LedgeGrab, ctx => ctx.State.IsGrabbingLedge);
            LedgeGrab.AddTransitionTo(LedgeIdle);
            LedgeIdle.AddTransition(LedgeRelease, ctx => !ctx.State.IsGrabbingLedge)
            .AddTransition(LedgeClimb, DirectionInput(Direction.Up))
            .AddTransition(LedgeJump, ctx => ctx.Input.Jump.WasPressed && ctx.CanJump)
            .AddTransition(LedgeAttack, Attack());
            LedgeJump.AddTransitionTo(Jump);
            new[] { LedgeRelease, LedgeClimb, LedgeEscape, LedgeAttack }
            .AddTransitions(Idle, ctx => ctx.NormalizedStateTime >= 1.0f && ctx.IsGrounded)
            .AddTransitions(Fall, ctx => ctx.NormalizedStateTime >= 1.0f && !ctx.IsGrounded);

            // Shielding
            Idle.AddTransition(Shield.On, Input(i => i.Shield.Current));
            Shield.On.AddTransition(Shield.Perfect, ctx => ctx.State.IsHit)
            .AddTransitionTo(Shield.Main);
            Shield.Main.AddTransition(Shield.Broken, ctx => ctx.State.ShieldDamage <= 0)
            .AddTransition(Shield.Off, Input(i => !i.Shield.Current));
            Shield.Off.AddTransitionTo(Idle);
            new[] { Shield.Broken, Shield.Stunned, Idle }.Chain();

            // Rolls/Sidesteps
            Shield.Main
            .AddTransition(EscapeForward, ctx => {
                if (ctx.Direction > 0f)
                {
                    return(DirectionalSmash(Direction.Right)(ctx));
                }
                else
                {
                    return(DirectionalSmash(Direction.Left)(ctx));
                }
            })
            .AddTransition(EscapeBackward, ctx => {
                if (ctx.Direction > 0f)
                {
                    return(DirectionalSmash(Direction.Left)(ctx));
                }
                else
                {
                    return(DirectionalSmash(Direction.Right)(ctx));
                }
            })
            .AddTransition(Escape, DirectionInput(Direction.Down));
            new[] { Escape, EscapeForward, EscapeBackward }.AddTransitionTo(Shield.Main);

            Builder.WithDefaultState(Idle);
            BuildCharacterController();
            return(Builder.Build());
        }
Esempio n. 7
0
    protected virtual void InitializeStateMachine()
    {
        Idle idleState = new Idle(this);
        idleState.AddTransition(Transition.E_FAILGESTURE, StateID.E_ATTACK);
        idleState.AddTransition(Transition.E_LOSTHP, StateID.E_DAMAGED);
        idleState.AddTransition(Transition.E_NOHP, StateID.E_DEATH);

        Attack atkState = new Attack(this);
        atkState.AddTransition(Transition.E_FINISHATTACK, StateID.E_IDLE);
        atkState.AddTransition(Transition.E_LOSTHP, StateID.E_DAMAGED);

        Damaged dmgState = new Damaged(this);
        dmgState.AddTransition(Transition.E_NOHP, StateID.E_DEATH);
        dmgState.AddTransition(Transition.E_FINISHATTACK, StateID.E_IDLE);
        dmgState.AddTransition(Transition.E_LOSTHP, StateID.E_DAMAGED);

        Death deathState = new Death(this);
        enemyState = new FiniteStateMachine();
        enemyState.AddState(idleState);

        enemyState.AddState(atkState);
        enemyState.AddState(dmgState);
        enemyState.AddState(deathState);
    }