예제 #1
0
        protected override void Update(float dt)
        {
            // Check input buttons
            _moveLeft  = Input.GetButton("a") == ButtonState.Down;
            _moveRight = Input.GetButton("d") == ButtonState.Down;
            _moveUp    = Input.GetButton("w") == ButtonState.Down;
            _moveDown  = Input.GetButton("s") == ButtonState.Down;

            // Detect climb
            var map = Scene.GetEntity <Map>();

            if (!map.IsNearClimable(Transform.Position))
            {
                if (_isClimb)
                {
                    Physics.Velocity = Vector.Zero;
                    ChangeAnimation("idle");
                }

                _isClimb = false;
            }
            else if (_moveUp || _moveDown)
            {
                _isClimb = true;
            }

            // Did we press the jump button?
            if (Input.GetButton("space") == ButtonState.Pressed)
            {
                if (_isClimb)
                {
                    ChangeAnimation("jump");
                    _isClimb = false;
                }

                _doJump = true;
            }

            // Movement
            if (_isClimb)
            {
                Physics.GravityMultiplier = 0;

                //
                ChangeAnimation("climb", false);
                SpriteRenderer.GotoFrame((int)Transform.Position.Y / 32 % 2);
            }
            else
            {
                Physics.GravityMultiplier = 1;

                if (_moveLeft || _moveRight)
                {
                    // Flip sprite for movement direction
                    if (_moveLeft)
                    {
                        Transform.Scale = (-1, 1);
                    }
                    else
                    {
                        Transform.Scale = (1, 1);
                    }

                    // If on the ground, set walking animation
                    if (Physics.HasGroundCollision)
                    {
                        ChangeAnimation("walk");
                    }
                }
                else
                {
                    // If on the ground, set idle animation
                    if (Physics.HasGroundCollision)
                    {
                        ChangeAnimation("idle");
                    }
                }
            }

            // Are we on the ground?
            if (Physics.HasGroundCollision)
            {
                // Reset jumping state
                _allowJump = true;
                _isClimb   = false;

                // If we intend to do a jump
                if (_doJump)
                {
                    // Set jump animation
                    ChangeAnimation("jump");
                }
            }
        }