private void Awake()
    {
        m_fallingState = transform.parent.parent.Find("States").gameObject.GetComponentInChildren(typeof(FallingState)) as FallingState;

        m_stateMachine        = transform.parent.parent.GetComponentInChildren(typeof(StateMachine)) as StateMachine;
        m_characterController = transform.parent.parent.GetComponent(typeof(CharacterController)) as CharacterController;
    }
示例#2
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);
    }
示例#3
0
        public void FallingStateTransition()
        {
            var state = new FallingState(peach);

            peach.ActionState = state;
            peach.Sprite      = PeachSpriteFactory.Instance.FactoryMethod(peach);
        }
 public override void Start()
 {
     fallState = GetComponent <FallingState>();
     base.Start();
     actions.Add(new FallAction(this));
     actions.Add(new ExpireAction(stateMachine, GetComponent <RopeTarget>().GetState, 0.7f));
 }
    private void Awake()
    {
        m_fallingState = transform.parent.parent.Find("States").GetComponentInChildren(typeof(FallingState)) as FallingState;

        m_stateMachine       = transform.parent.parent.GetComponentInChildren(typeof(StateMachine)) as StateMachine;
        m_playerGroundChecks = transform.parent.parent.GetComponent(typeof(GroundChecks)) as GroundChecks;
    }
示例#6
0
    public void Construct(MoveState ms, StayState ss, FallingState fs)
    {
        _stateMap.Add(SphereState.Stay, ss);
        _stateMap.Add(SphereState.Move, ms);
        _stateMap.Add(SphereState.Falling, fs);

        _currentState = ss;
    }
示例#7
0
 static public FallingState GetInstance()
 {
     if (instance == null)
     {
         instance = new FallingState();
     }
     return(instance);
 }
示例#8
0
    //RaycastHit2D[] info;
    public override void UpdateState(Character c, UserInput input, RaycastHit2D[] info)
    {
        this.info = info;
        //base.UpdateState(c, input);


        //Debug.Log("Running state updating " + c.currentLinearSpeed);

        if (c.currentLinearSpeed > 0)
        {
            c.playerSprite.transform.localScale = new Vector3(1, 1, 1);
        }
        if (c.currentLinearSpeed < 0)
        {
            c.playerSprite.transform.localScale = new Vector3(-1, 1, 1);
        }

        if (c.StateList.Count == index)
        {
            //animation
            AnimateState(c);
            //sound
            c.SetSoundEffect(c.playerAudio.walk, true, false, 0.15f);

            //Debug.Log("sdfssfsfsfsfsfsfsf");
            //Exit conditions
            //if (c.State.GetType() != typeof(FallingState))
            //{
            //Debug.Log("x input: " + input.xInput);
            if (input.xInput == 0)
            {
                //c.State = null;
                //c.State = new IdleState();
                c.StateList.RemoveAt(c.StateList.Count - 1);
                //Debug.Log("Removed");
            }
            if (input.jumpPressed)
            {
                //c.State = null;
                //c.State = new JumpingState();
                States nextState = new JumpingState();
                nextState.index = c.StateList.Count + 1;
                c.StateList.Add(nextState);
                return;
            }

            if (info.Length == 0 && c.StateList[c.StateList.Count - 1].GetType() != typeof(FallingState))
            {
                States nextState = new FallingState(1);
                nextState.index = c.StateList.Count + 1;
                c.StateList.Add(nextState);
                return;
            }
        }
    }
示例#9
0
    public StateMachine(Player player)
    {
        stationaryState  = new StationaryState(player);
        runningState     = new RunningState(player);
        jumpingState     = new JumpingState(player);
        fallingState     = new FallingState(player);
        wallSlidingState = new WallSlidingState(player);
        wallHuggingState = new WallHuggingState(player);

        _currentState = stationaryState;
    }
示例#10
0
 void StateExitConditions(Character c, UserInput input)
 {
     //If the special button pressed again while climbing, set the state to fall
     if (input.climbPressed)
     {
         VirtualJoystick.ResetArrows(c);
         Debug.Log("Falling from climbing");
         c.StateList.RemoveAt(c.StateList.Count - 1);
         //Add the falling state
         States nextState = new FallingState(1);
         //States nextState = new FallingState(1);
         nextState.index = c.StateList.Count + 1;
         c.StateList.Add(nextState);
     }
     else if (input.jumpPressed && input.jumpReleased)
     {
         VirtualJoystick.ResetArrows(c);
         Debug.Log("Transition to jump state");
         c.StateList.RemoveAt(c.StateList.Count - 1);
         //Add the falling state
         States nextState = new JumpingState();
         //States nextState = new FallingState(1);
         nextState.index = c.StateList.Count + 1;
         c.StateList.Add(nextState);
     }
     else if (input.xInput < 0 && (Mathf.Abs(bottomNode.position.y - c.transform.position.y) < minimumDifference ||
                                   c.transform.position.y < bottomNode.position.y))
     {
         Debug.Log("X input down");
         VirtualJoystick.ResetArrows(c);
         //If the character is closer to the bottom node and is trying to go down,
         //transition to idle state
         c.StateList.RemoveAt(c.StateList.Count - 1);
         c.BlockInputs();
         //Add the falling state
         //States nextState = new LandState();
         States nextState = new FallingState(1);
         nextState.index = c.StateList.Count + 1;
         c.StateList.Add(nextState);
     }
     //else if(Mathf.Abs(topNode.position.y - c.transform.position.y) < minimumDifference || c.transform.position.y > topNode.position.y)
     //{
     //    Debug.Log("reached top");
     //    VirtualJoystick.ResetArrows(c);
     //    //Player state is set to falling if he attempts to go beyond the ladder proximity
     //    c.StateList.RemoveAt(c.StateList.Count - 1);
     //    c.currentJumpSpeed = 0;
     //    //Add the falling state
     //    States nextState = new FallingState(1);
     //    //States nextState = new FallingState(1);
     //    nextState.index = c.StateList.Count + 1;
     //    c.StateList.Add(nextState);
     //}
 }
示例#11
0
 public void StateDependentUpdate(PlayerMovement pm)
 {
     jumpStrength = pm.jumpStrength;
     if (wantsToJump)
     {
         //rBody.AddRelativeForce(Vector2.up * jumpStrength, ForceMode2D.Force);
         pm.rBody.velocity = Vector2.up * jumpStrength;
         wantsToJump       = false;
         pm.anim.SetBool("isJumping", true);
         pm.SwapState(FallingState.GetInstance());
     }
     pm.CheckHorizontalMovement();
 }
示例#12
0
    void StateExitConditions(RaycastHit2D[] info)
    {
        if (timeElapsed > duration)
        {
            c.StateList.RemoveAt(c.StateList.Count - 1);
        }

        else if (info.Length == 0 && c.StateList[c.StateList.Count - 1].GetType() != typeof(FallingState))
        {
            Debug.Log("fallllllllllll" + c.StateList[c.StateList.Count - 1].GetType());
            States nextState = new FallingState(1);
            nextState.index = c.StateList.Count + 1;
            c.StateList.Add(nextState);
            return;
        }
    }
示例#13
0
    private void Start()
    {
        playtime = 0;
        pData    = GetComponent <PlayerData>();
        if (File.Exists(selectedProfilePath))
        {
            selectedProfile = File.ReadAllText(selectedProfilePath);
            saveDataPath   += selectedProfile + ".json";
            if (File.Exists(saveDataPath))
            {
                string json = File.ReadAllText(saveDataPath);
                var    dto  = JsonConvert.DeserializeObject <PlayerDTO>(json);
                pData.serializeData = dto.playerSerializeData;
                playtime            = dto.Playtime;
                if (pData.HasCheckPoint)
                {
                    transform.position = new Vector2(pData.PosX, pData.PosY);
                }
            }
        }

        pInput         = new PlayerInput(pData);
        spriteRenderer = GetComponent <SpriteRenderer>();
        audioSource    = GetComponent <AudioSource>();

        #region Initialize States

        IdleState                    = new IdleState(this, pInput, pData, "Idle_Animation");
        RunningState                 = new RunningState(this, pInput, pData, "Running_Animation");
        FallingState                 = new FallingState(this, pInput, pData, "Falling_Animation");
        JumpState                    = new JumpState(this, pInput, pData, "Jump_Animation");
        StartFallingState            = new StartFallingState(this, pInput, pData, "Start_Falling_Animation");
        WallSlideState               = new WallSlideState(this, pInput, pData, "Wall_slide");
        DashingState                 = new DashingState(this, pInput, pData, "Dash_Animation");
        PrimaryAttackState           = new PrimaryAttackState(this, pInput, pData, "Left_swing_attack");
        GroundedSecondaryAttackState = new GroundedSecondaryAttackState(this, pInput, pData, "Right_swing_attack");
        GroundedDownwardAttackState  = new GroundedDownwardAttackState(this, pInput, pData, "Downward");;
        DeathState                   = new DeathState(this, pInput, pData, "Death_Animation");
        CheckPointState              = new CheckPointState(this, pInput, pData, "CheckPoint_Animation");
        WallJumpState                = new WallJumpState(this, pInput, pData, "Jump_Animation");

        currentState = IdleState;

        #endregion Initialize States
    }
示例#14
0
    //RaycastHit2D[] info;
    public override void UpdateState(Character c, UserInput input, RaycastHit2D[] info)
    {
        this.info = info;
        //Debug.Log("Jumping state updating");

        //c.currentLinearSpeed = input.xInput * c.maxSpeed * GameManager.Instance.DeltaTime * GameManager.Instance.DeltaTime;


        if (input.jumpPressed && input.jumpReleased && !jumping)
        {
            //sound must only be played once
            c.SetSoundEffect(c.playerAudio.jump, false, false);

            jumping            = true;
            c.currentJumpSpeed = c.maxJumpSpeed * GameManager.Instance.stepSize * GameManager.Instance.stepSize;
            //This is set so that the jump in the next frame is not detected
            input.jumpReleased = false;
        }

        //setting the animation
        AnimateState(c);

        if (input.jumpPressed)
        {
            if (jumpDamper > 0.25f)
            {
                jumpDamper -= c.jumpMultiplier * GameManager.Instance.DeltaTime;
            }
            else
            {
                jumpDamper = 0.25f;
            }
        }
        //There has to be a default downwardforce acting all the time, which is gravity
        c.currentJumpSpeed += c.gravity * 4.5f * jumpDamper * GameManager.Instance.DeltaTime * GameManager.Instance.DeltaTime;
        //Debug.Log("Current jump speed: " + c.currentJumpSpeed);


        if (c.StateList.Count == index)
        {
            Collider2D upwardCollision = Physics2D.OverlapCircle(c.ceilingCheck.transform.position, c.groundCheckCircleRadius, LayerMask.GetMask("Platform"));
            //Debug.Log("upward collision: " + upwardCollision);

            if (upwardCollision != null && c.currentJumpSpeed > 0 && !upwardCollision.isTrigger)
            {
                Debug.Log("Upward collision is not null");
                c.currentJumpSpeed = 0;
                //c.State = null;
                //c.State = new FallingState(1);
                c.StateList.RemoveAt(c.StateList.Count - 1);
                States nextState = new FallingState(1);
                nextState.index = c.StateList.Count + 1;
                c.StateList.Add(nextState);
            }
            else if (c.currentJumpSpeed < 0)
            {
                //c.State = null;
                //c.State = new FallingState(1);
                c.StateList.RemoveAt(c.StateList.Count - 1);
                States nextState = new FallingState(1);
                nextState.index = c.StateList.Count + 1;
                c.StateList.Add(nextState);
            }
        }
    }
示例#15
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);
    }
示例#16
0
    protected override void Update()
    {
        if (currentState.AllowJumping() && Input.GetAxis("Jump") > 0.1)
        {
            float jumpForce = Mathf.Sqrt(2 * -gravity * jumpHeight);
            velocity.y = jumpForce;
            SetState(JumpingState.Instance());
        }

        if (!onGround && velocity.y < 0)
        {
            SetState(FallingState.Instance());
        }

        float horizontal          = Input.GetAxis("Horizontal");
        float vertical            = Input.GetAxis("Vertical");
        float strafe              = 0f;
        bool  applyGroundFriction = true;

        if (Math.Abs(horizontal) > AXIS_DEADZONE)
        {
            if (currentState.AllowRunning())
            {
                strafe = horizontal;
                SetState(RunningState.Instance());

                velocity.x          = horizontalSpeed * strafe;
                applyGroundFriction = false;
            }
            else if (currentState.AllowAirControl())
            {
                strafe      = horizontal;
                velocity.x += strafe * airControlHorizontalAcceleration * Time.deltaTime * 1.0f / 0.999f /*counter-drag*/;
            }
        }

        if (Math.Abs(velocity.x) > horizontalSpeed)
        {
            velocity.x = horizontalSpeed * Math.Sign(velocity.x);
        }

        if (currentState.AllowCrouching() && vertical < -AXIS_DEADZONE)
        {
            SetState(CrouchingState.Instance());
        }
        else if (onGround)
        {
            if (strafe == 0f)
            {
                SetState(IdleState.Instance());
            }
            else
            {
                SetState(RunningState.Instance());
            }
        }

        if (applyGroundFriction)
        {
            if (onGround)
            {
                velocity.x *= 0.85f;
            }
            else
            {
                velocity.x *= 0.999f;
            }
        }

        currentState.Update(gameObject, this);

        base.Update();
    }
示例#17
0
        public void Awake()
        {
            sprite       = GetComponent <SpriteRenderer>();
            actorPhysics = GetComponent <IActorPhysics>();
            if (!animator)
            {
                animator = GetComponent <Animator>();
                if (!animator)
                {
                    Debug.LogWarning("No animator found for " + gameObject.name);
                }
            }

            actionStateLookup   = new Dictionary <ActionStateType, IActionState>();
            movementStateLookup = new Dictionary <MovementStateType, IMovementState>();

            if (!standingState)
            {
                standingState = GetComponentInChildren <StandingState>();
            }
            if (standingState)
            {
                standingState.SetActor(this);
                movementStateLookup.Add(MovementStateType.STANDING, standingState);
            }

            if (!duckingState)
            {
                duckingState = GetComponentInChildren <KneelingState>();
            }
            if (duckingState)
            {
                duckingState.SetActor(this);
                movementStateLookup.Add(MovementStateType.KNEELING, duckingState);
            }

            if (!walkingState)
            {
                walkingState = GetComponentInChildren <WalkingState>();
            }
            if (walkingState)
            {
                walkingState.SetActor(this);
                movementStateLookup.Add(MovementStateType.WALKING, walkingState);
            }

            if (stairsState)
            {
                stairsState.SetActor(this);
                movementStateLookup.Add(MovementStateType.STAIRS, stairsState);
            }

            if (!airbornState)
            {
                airbornState = GetComponentInChildren <FallingState>();
            }
            if (airbornState)
            {
                airbornState.SetActor(this);
                movementStateLookup.Add(MovementStateType.FALLING, airbornState);
            }

            if (!jetpackState)
            {
                jetpackState = GetComponentInChildren <JetpackState>();
            }
            if (jetpackState)
            {
                jetpackState.SetActor(this);
                movementStateLookup.Add(MovementStateType.JETPACK, jetpackState);
            }

            elevatorState = GetComponentInChildren <ElevatorState>();
            if (elevatorState)
            {
                elevatorState.SetActor(this);
                movementStateLookup.Add(MovementStateType.ELEVATOR, elevatorState);
                actionStateLookup.Add(ActionStateType.ELEVATOR, elevatorState);
            }



            NoActionState awaitingState = new NoActionState();

            awaitingState.SetActor(this);
            actionStateLookup.Add(ActionStateType.AWAITING_ACTION, awaitingState);

            if (!shootingState)
            {
                shootingState = GetComponentInChildren <ShootingState>();
            }
            if (shootingState)
            {
                shootingState.SetActor(this);
                actionStateLookup.Add(ActionStateType.SHOOT, shootingState);
            }

            currentMovementState = MovementStateType.FALLING;
            movementState        = airbornState;
            currentActionState   = ActionStateType.AWAITING_ACTION;
            actionState          = actionStateLookup[ActionStateType.AWAITING_ACTION];
        }