Esempio n. 1
0
        public override void Update(GameTime gameTime, Rectangle clientBounds)
        {
            // First, move the sprite along its direction vector
            position += speed;

            // Use the player position to move the sprite closer in
            // the X and/or Y directions
            Vector2 player = spriteManager.GetPlayerPosition();

            if (evade)
            {
                // Move away from the player horizontally
                if (player.X < position.X)
                {
                    position.X += Math.Abs(speed.Y);
                }
                else if (player.X > position.X)
                {
                    position.X -= Math.Abs(speed.Y);
                }

                // Move away from the player vertically
                if (player.Y < position.Y)
                {
                    position.Y += Math.Abs(speed.X);
                }
                else if (player.Y > position.Y)
                {
                    position.Y -= Math.Abs(speed.X);
                }
            }
            else
            {
                if (Vector2.Distance(position, player) < evasionRange)
                {
                    // Player is within evasion range,
                    // reverse direction and modify speed
                    speed *= -evasionSpeedModifier;
                    evade  = true;
                }
            }

            base.Update(gameTime, clientBounds);
        }
        public override void Update(GameTime gameTime, Rectangle clientBounds)
        {
            // First, move the sprite along its direction vector
            position += speed * 1.2f;

            // Use the player position to move the sprite closer in
            // the X and/or Y directions
            Vector2 player = spriteManager.GetPlayerPosition();

            // If player is moving vertically, chase horizontally
            if (speed.X == 0)
            {
                if (player.X < position.X)
                {
                    position.X -= Math.Abs(speed.Y * 0.6f);
                }
                else if (player.X > position.X)
                {
                    position.X += Math.Abs(speed.Y * 0.6f);
                }
            }

            // If player is moving horizontally, chase vertically
            if (speed.Y == 0)
            {
                if (player.Y < position.Y)
                {
                    position.Y -= Math.Abs(speed.X * 0.6f);
                }
                else if (player.Y > position.Y)
                {
                    position.Y += Math.Abs(speed.X * 0.6f);
                }
            }

            base.Update(gameTime, clientBounds);
        }