Esempio n. 1
0
    protected override void Start()
    {
        base.Start();
        RecalculateHiddenValues();

        // States
        var falling = new FallingState("Falling", velocity, gravity, () => speed * input.Horizontal);
        var running = new RunningState("Running", velocity, () => speed * input.Horizontal);
        var jumping = new FallingState("Jumping", velocity, gravity, () => speed * input.Horizontal);

        // OnExit/Enter
        falling.OnExit += () => SetVelocityY();

        running.OnExit += () => SetVelocityY();
        running.OnExit += () => coyoteTimer = coyoteTime;

        jumping.OnEnter += () => SetVelocityY(jumpVelocity);

        // States
        falling.AddTransition(() => SurfaceClose(downHit), running);
        falling.AddTransition(() => input.Jump && !jumpLastPressed && coyoteTimer > 0, jumping,
                              () => { Debug.Log("COYOTE TIME!!!"); jumpLastPressed = true; });

        running.AddTransition(() => !SurfaceClose(downHit), falling);
        running.AddTransition(() => input.Jump && !jumpLastPressed, jumping, () => jumpLastPressed = true);

        jumping.AddTransition(() => velocity.Value.y <= 0, falling);
        jumping.AddTransition(() => SurfaceClose(upHit), falling, () => SetVelocityY(gravity * 0.06f));

        // Initialization
        _controllerBrain = new StateMachine(falling, DebugBlock);
    }
Esempio n. 2
0
    private void ConstructFSM()
    {
        /*
         * //Get the list of points
         * pointList = GameObject.FindGameObjectsWithTag("WandarPoint");
         *
         * Transform[] waypoints = new Transform[pointList.Length];
         * int i = 0;
         * foreach(GameObject obj in pointList)
         * {
         * waypoints[i] = obj.transform;
         * i++;
         * }
         *
         * PatrolState patrol = new PatrolState(waypoints);
         *
         * patrol.AddTransition(Transition.SawPlayer, FSMStateID.Chasing);
         * patrol.AddTransition(Transition.NoHealth, FSMStateID.Dead);
         */
/*        ChaseState chase = new ChaseState(waypoints);
 *      chase.AddTransition(Transition.LostPlayer, FSMStateID.Patrolling);
 *      chase.AddTransition(Transition.ReachPlayer, FSMStateID.Attacking);
 *      chase.AddTransition(Transition.NoHealth, FSMStateID.Dead);
 *
 *      AttackState attack = new AttackState(waypoints);
 *      attack.AddTransition(Transition.LostPlayer, FSMStateID.Patrolling);
 *      attack.AddTransition(Transition.SawPlayer, FSMStateID.Chasing);
 *      attack.AddTransition(Transition.NoHealth, FSMStateID.Dead);
 *
 */

        // AddFSMState(patrol);

/*        AddFSMState(chase);
 *      AddFSMState(attack);
 *      AddFSMState(dead);*/

        SwimState              swim              = new SwimState();
        SawObstacleState       sawObstacle       = new SawObstacleState();
        TurningAwayState       turningAway       = new TurningAwayState();
        JumpState              jump              = new JumpState();
        JumpingUnderWaterState jumpingUnderWater = new JumpingUnderWaterState();
        JumpingAboveWaterState jumpingAboveWater = new JumpingAboveWaterState();
        BumpState              bump              = new BumpState();
        FallingState           falling           = new FallingState();
        HitWaterState          hitWater          = new HitWaterState();
        DeadState              dead              = new DeadState();

        AddFSMState(swim);
        AddFSMState(sawObstacle);
        AddFSMState(turningAway);
        AddFSMState(jump);
        AddFSMState(jumpingUnderWater);
        AddFSMState(jumpingAboveWater);
        AddFSMState(falling);
        AddFSMState(hitWater);
        AddFSMState(bump);
        AddFSMState(dead);

        swim.AddTransition(Transition.Tapped, FSMStateID.Jump);
        swim.AddTransition(Transition.BumpedIntoSomething, FSMStateID.Bump);
        swim.AddTransition(Transition.SawObstacle, FSMStateID.SawObstacle);
        swim.AddTransition(Transition.SwimmingToTurning, FSMStateID.TurningAway);
        swim.AddTransition(Transition.GoneBelow, FSMStateID.Dead);

        sawObstacle.AddTransition(Transition.FoundClearDirection, FSMStateID.TurningAway);
        turningAway.AddTransition(Transition.TurnIsComplete, FSMStateID.Swimming);

        jump.AddTransition(Transition.HasJumped, FSMStateID.JumpingUnderWater);

        jumpingUnderWater.AddTransition(Transition.AboveWater, FSMStateID.JumpingAboveWater);
        jumpingUnderWater.AddTransition(Transition.ReturnToSwimming, FSMStateID.Swimming);

        jumpingAboveWater.AddTransition(Transition.ReachedApex, FSMStateID.Falling);

        falling.AddTransition(Transition.AtSurface, FSMStateID.HitWater);

        hitWater.AddTransition(Transition.UnderWater, FSMStateID.JumpingUnderWater);

        bump.AddTransition(Transition.HasBumped, FSMStateID.JumpingUnderWater);

        dead.AddTransition(Transition.NoHealth, FSMStateID.Dead);
        dead.AddTransition(Transition.Z, FSMStateID.Swimming);

        //falling.AddTransition(Transition.GoneBelow, FSMStateID.Dead);
    }