public void Update(GameTime gameTime)
        {
            var velocity = GetDesiredVelocityFromInput();

            this.X += velocity.X * (float)gameTime.ElapsedGameTime.TotalSeconds;
            this.Y += velocity.Y * (float)gameTime.ElapsedGameTime.TotalSeconds;
            // la variabile velocità determina se il carattere si muove o si sta fermando
            bool isMoving = velocity != Vector2.Zero;

            if (isMoving)
            {
                /* se il valore assoluto del componente X è maggiore del valore assoluto
                 * della componente Y, allora questo significa che il carattere si sta
                 * muovendo orizzontalmente */
                bool isMovingHorizontally = Math.Abs(velocity.X) > Math.Abs(velocity.Y);
                if (isMovingHorizontally)
                {
                    /* non  sappiamo se il carattere si muove orizzontalmente
                     * possiamo verificare se la velocità è positiva (muovendosi a destra)
                     * o negativa (spostamento a sinistra) */
                    if (velocity.X > 0)
                    {
                        currentAnimation = walkRight;
                    }
                    else
                    {
                        currentAnimation = walkLeft;
                    }
                }
                else
                {
                    /* se il carattere non si muove orizzontalmente allora deve muoversi
                     * verticalmente, la classe SpriteBatch tratta positiva Y come giù, quindi
                     * questo definisce il sistema di coordinate per il gioco, pertanto, se Y
                     * è positivo, quindi il carattere si muove verso il basso, in caso
                     * contrario, il carattere si sta spostando verso l'alto  */
                    if (velocity.Y > 0)
                    {
                        currentAnimation = walkDown;
                    }
                    else
                    {
                        currentAnimation = walkUp;
                    }
                }
            }
            else
            {
                /* questa istruzione else contiene la logica per il carattere, in primo luogo
                 * ci accingiamo a controllare se il carattere sta attualmente
                 * riproducendo tutte le animazioni a piedi, se è così, allora vogliamo
                 * passare ad un'animazione permanente, vogliamo preservare la
                 * direzione in cui il carattere è rivolto verso di noi in modo da
                 * impostare la posizione corrispondente l'animazione in base
                 * all'animazione a piedi riprodotta */
                if (currentAnimation == walkRight)
                {
                    currentAnimation = standRight;
                }
                else if (currentAnimation == walkLeft)
                {
                    currentAnimation = standLeft;
                }
                else if (currentAnimation == walkUp)
                {
                    currentAnimation = standUp;
                }
                else if (currentAnimation == walkDown)
                {
                    currentAnimation = standDown;
                }
                // se il carattere è fermo ma non mostra alcuna animazione, allora avremo di default la fronte verso il basso
                else if (currentAnimation == null)
                {
                    currentAnimation = standDown;
                }
            }

            currentAnimation.Update(gameTime);
        }
Beispiel #2
0
        public void Update(GameTime gameTime)
        {
            //this.GetInput();
            Vector2 velocity = Vector2.Zero;

            if (this.currentAnimation != null)
            {
                velocity = GetDesiredVelocityFromInput();

                this.X += velocity.X * (float)gameTime.ElapsedGameTime.TotalSeconds;
                this.Y += velocity.Y * (float)gameTime.ElapsedGameTime.TotalSeconds;
            }

            if (velocity != Vector2.Zero)
            {
                bool movingHorizontally = Math.Abs(velocity.X) > Math.Abs(velocity.Y);
                if (movingHorizontally)
                {
                    if (velocity.X > 0)
                    {
                        currentAnimation = walkRight;
                    }
                    else
                    {
                        currentAnimation = walkLeft;
                    }
                }
                else
                {
                    if (velocity.Y > 0)
                    {
                        currentAnimation = walkDown;
                    }
                    else
                    {
                        currentAnimation = walkUp;
                    }
                }
            }
            else
            {
                // If the character was walking, we can set the standing animation
                // according to the walking animation that is playing:
                if (currentAnimation == walkRight)
                {
                    currentAnimation = standRight;
                }
                else if (currentAnimation == walkLeft)
                {
                    currentAnimation = standLeft;
                }
                else if (currentAnimation == walkUp)
                {
                    currentAnimation = standUp;
                }
                else if (currentAnimation == walkDown)
                {
                    currentAnimation = standDown;
                }
                else if (currentAnimation == null)
                {
                    currentAnimation = standDown;
                }

                // if none of the above code hit then the character
                // is already standing, so no need to change the animation.
            }


            currentAnimation.Update(gameTime);
        }
Beispiel #3
0
        public void Update(GameTime gameTime)
        {
            var velocity = GetDesiredVelocityFromInput();

            X += velocity.X * (float)gameTime.ElapsedGameTime.TotalSeconds;
            Y += velocity.Y * (float)gameTime.ElapsedGameTime.TotalSeconds;

            // We can use the velocity variable to determine if the
            // character is moving or standing still
            bool isMoving = velocity != Vector2.Zero;

            if (isMoving)
            {
                // If the absolute value of the X component
                // is larger than the absolute value of the Y
                // component, then that means the character is
                // moving horizontally:
                bool isMovingHorizontally = Math.Abs(velocity.X) > Math.Abs(velocity.Y);
                if (isMovingHorizontally)
                {
                    // No that we know the character is moving horizontally
                    // we can check if the velocity is positive (moving right)
                    // or negative (moving left)
                    currentAnimation = (velocity.X > 0) ? walkRight : walkLeft;
                }
                else
                {
                    // If the character is not moving horizontally
                    // then it must be moving vertically. The SpriteBatch
                    // class treats positive Y as down, so this defines the
                    // coordinate system for our game. Therefore if
                    // Y is positive then the character is moving down.
                    // Otherwise, the character is moving up.
                    currentAnimation = (velocity.Y > 0) ? walkDown : walkUp;
                }
            }
            else
            {
                // This else statement contains logic for if the
                // character is standing still.
                // First we are going to check if the character
                // is currently playing any walking animations.
                // If so, then we want to switch to a standing animation.
                // We want to preserve the direction that the character
                // is facing so we'll set the corresponding standing
                // animation according to the walking animation being played.
                if (currentAnimation == walkRight)
                {
                    currentAnimation = standRight;
                }
                else if (currentAnimation == walkLeft)
                {
                    currentAnimation = standLeft;
                }
                else if (currentAnimation == walkUp)
                {
                    currentAnimation = standUp;
                }
                else if (currentAnimation == walkDown)
                {
                    currentAnimation = standDown;
                }
                else if (currentAnimation == null)
                {
                    currentAnimation = standDown;
                }
            }

            currentAnimation.Update(gameTime);
        }
        public void Update(GameTime gameTime)
        {
            var velocity = GetDesiredVelocityFromInput();


            // next step
            var nextX = velocity.X;
            var nextY = velocity.Y;

            angle += (float)(gameTime.ElapsedGameTime.TotalSeconds * 4.00);

            this.X = (float)(nextX + Math.Cos(angle) * 45);
            this.Y = (float)(nextY + Math.Sin(angle) * 45);

            this.X += (float)(nextX * gameTime.ElapsedGameTime.TotalSeconds);
            this.Y += (float)(nextY * gameTime.ElapsedGameTime.TotalSeconds);

            // We can use the velocity variable to determine if the
            // character is moving or standing still
            bool isMoving = velocity != Vector2.Zero;

            if (isMoving)
            {
                // If the absolute value of the X component
                // is larger than the absolute value of the Y
                // component, then that means the character is
                // moving horizontally:
                bool isMovingHorizontally = Math.Abs(velocity.X) > Math.Abs(velocity.Y);
                if (isMovingHorizontally)
                {
                    // No that we know the character is moving horizontally
                    // we can check if the velocity is positive (moving right)
                    // or negative (moving left)
                    if (velocity.X > 0)
                    {
                        currentAnimation = walkRight;
                    }
                    else
                    {
                        currentAnimation = walkLeft;
                    }
                }
                else
                {
                    // If the character is not moving horizontally
                    // then it must be moving vertically. The SpriteBatch
                    // class treats positive Y as down, so this defines the
                    // coordinate system for our game. Therefore if
                    // Y is positive then the character is moving down.
                    // Otherwise, the character is moving up.
                    if (velocity.Y > 0)
                    {
                        currentAnimation = walkDown;
                    }
                    else
                    {
                        currentAnimation = walkUp;
                    }
                }
            }
            else
            {
                // This else statement contains logic for if the
                // character is standing still.
                // First we are going to check if the character
                // is currently playing any walking animations.
                // If so, then we want to switch to a standing animation.
                // We want to preserve the direction that the character
                // is facing so we'll set the corresponding standing
                // animation according to the walking animation being played.
                if (currentAnimation == walkRight)
                {
                    currentAnimation = standRight;
                }
                else if (currentAnimation == walkLeft)
                {
                    currentAnimation = standLeft;
                }
                else if (currentAnimation == walkUp)
                {
                    currentAnimation = standUp;
                }
                else if (currentAnimation == walkDown)
                {
                    currentAnimation = standDown;
                }
                // If the character is standing still but is not showing
                // any animation at all then we'll default to facing down.
                else if (currentAnimation == null)
                {
                    currentAnimation = standDown;
                }
            }

            // fade
            mFadeDelay -= gameTime.ElapsedGameTime.TotalSeconds;

            if (mFadeDelay <= 0)
            {
                //Reset the Fade delay
                mFadeDelay = .035;

                //Increment/Decrement the fade value for the image
                mAlphaValue += mFadeIncrement;

                //If the AlphaValue is equal or above the max Alpha value or
                //has dropped below or equal to the min Alpha value, then
                //reverse the fade
                if (mAlphaValue >= 255 || mAlphaValue <= 0)
                {
                    mFadeIncrement *= -1;
                }
            }

            currentAnimation.Update(gameTime);
        }