コード例 #1
0
 void Start()
 {
     rigidBody    = GetComponent <Rigidbody2D>();
     stateMachine = new StateMachine();
     standing     = new StandingState(this, stateMachine);
     jumping      = new JumpingState(this, stateMachine);
     crouching    = new CrouchingState(this, stateMachine);
     stateMachine.Initialize(standing);
 }
コード例 #2
0
 public void CrouchingStateTransition()
 {
     if (!(peach.PowerUpState is DeadState))
     {
         var state = new CrouchingState(peach);
         peach.ActionState = state;
         peach.Sprite      = PeachSpriteFactory.Instance.FactoryMethod(peach);
     }
 }
コード例 #3
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();
    }