Ejemplo n.º 1
0
 public override void Update(GameTime gameTime)
 {
     foreach (IActor actor in _actorRegistry.GetAll())
     {
         actor.Update(gameTime);
     }
 }
Ejemplo n.º 2
0
        public override void Update(GameTime gameTime)
        {
            // Find the ball that is closest to the paddle and can still be saved
            Ball ball;

            if (PaddleSide == PaddleSide.Left)
            {
                // Padle is on the left, so find the closest ball moving left
                ball = _actorRegistry.GetAll()
                       .OfType <Ball>()
                       .Where(b => !b.IsResetting && b.Hitbox.Right > Hitbox.Left && (b.Angle.IsBetween(90, 270)))
                       .OrderBy(b => b.Hitbox.Left)
                       .FirstOrDefault();
            }
            else
            {
                // Padle is on the right, so find the closest ball moving right
                ball = _actorRegistry.GetAll()
                       .OfType <Ball>()
                       .Where(b => !b.IsResetting && b.Hitbox.Left < Hitbox.Right && (b.Angle.IsBetween(270, 360) || b.Angle.IsBetween(0, 90)))
                       .OrderByDescending(b => b.Hitbox.Right)
                       .FirstOrDefault();
            }

            // If a ball is moving towards the paddle, calculate which way the paddle should move to stop the ball
            if (ball != null)
            {
                float actualSpeed = DifficultySpeedFactor * (Speed * GameInfo.Speed);
                float distance    = 0F;

                if (ball.Hitbox.Top < Hitbox.Top)
                {
                    // Ball is higher than the paddle, so move up
                    distance = Hitbox.Top - ball.Hitbox.Top;
                    if (distance > actualSpeed)
                    {
                        distance = actualSpeed;
                    }
                    distance = -distance;
                }
                else if (ball.Hitbox.Bottom > Hitbox.Bottom)
                {
                    // Ball is lower than the paddle, so move down
                    distance = ball.Hitbox.Bottom - Hitbox.Bottom;
                    if (distance > actualSpeed)
                    {
                        distance = actualSpeed;
                    }
                }

                // Move the paddle
                if (Math.Abs(distance) > 0F)
                {
                    Hitbox.Offset(0F, distance);
                }
            }
        }
Ejemplo n.º 3
0
        public override void Update(GameTime gameTime)
        {
            IReadOnlyList <IActor> actors = _actorRegistry.GetAll();
            int actorCount = actors.Count;

            for (int i = 0; i < actorCount; i++)
            {
                IActor firstCollider = actors[i];

                // Check the collision with the edge of the game grid.
                Vector2 outOfBoundsDistance;
                if (BoxCollisionDetection.CheckBoundsCollision(firstCollider, _gameInfo.Bounds, out outOfBoundsDistance))
                {
                    firstCollider.OnBoundsCollision(outOfBoundsDistance);
                }

                for (int j = 0; j < i; j++)
                {
                    IActor secondCollider = actors[j];

                    Vector2 intersection;

                    // Check the collision for the first collider.
                    if (BoxCollisionDetection.CheckColliderCollision(firstCollider, secondCollider, out intersection))
                    {
                        // We inform the first collider that a collision happened, allowing itself to adjust it's position or do other things.
                        firstCollider.OnColliderCollision(secondCollider, intersection);

                        // By now the first collider may or may not have adjusted itself. Either way, we need the up to date intersection data for the second collider so we can supply it to it's OnColliderCollision method.
                        BoxCollisionDetection.CheckColliderCollision(secondCollider, firstCollider, out intersection);
                        secondCollider.OnColliderCollision(firstCollider, intersection);
                    }
                }
            }
        }