Exemple #1
0
        private void ReinitializeGame()
        {
            // Remove game object instances
            for (int i = Collectables.Count - 1; i >= 0; i--)
            {
                Collectables.RemoveAt(i);
            }
            for (int i = Balls.Count - 1; i >= 0; i--)
            {
                Balls.RemoveAt(i);
            }

            // Reinitialize Player paddle
            PlayerPaddle.Width  = Paddle.defaultWidth;
            PlayerPaddle.Height = Paddle.defaultHeight;
            PlayerPaddle.X      = GameAreaWidth / 2 - PlayerPaddle.Width / 2;
            PlayerPaddle.Y      = GameAreaHeight - 50;

            _currentTick = 0;

            Balls.Add(new PlayerBall(0, 0));
        }
Exemple #2
0
        /// <summary>
        /// Checks interactions between balls and other game instances (death triggers, player paddle, walls, bricks).
        /// TODO: Could be optimized - when ball is moving from a certain position in a certain direction,
        /// not all bricks have to be checked for collision.
        /// </summary>
        private void CheckBalls()
        {
            for (int i = 0; i < Balls.Count; i++)
            {
                var ball = Balls[i];
                if (ball.IsPinned)
                {
                    Vector pinnedPosition = PlayerPaddle.GetPinnedPosition();
                    ball.X = pinnedPosition.X - ball.Width / 2;
                    ball.Y = pinnedPosition.Y - ball.Height;
                    continue;
                }
                ball.UpdatePosition();

                Vector hitDirection = new Vector();

                // Interaction with player paddle
                if (ball.Intersect(PlayerPaddle, ref hitDirection))
                {
                    ball.Bounce(ref hitDirection);
                    if (PlayerPaddle.CurrentPlayerDirection == Paddle.PlayerDirection.left)
                    {
                        ball.DeflectX(-0.85);
                    }
                    else if (PlayerPaddle.CurrentPlayerDirection == Paddle.PlayerDirection.right)
                    {
                        ball.DeflectX(0.85);
                    }
                }

                // Interaction with walls
                foreach (var wall in CurrentLevel.Walls)
                {
                    if (ball.Intersect(wall, ref hitDirection))
                    {
                        ball.Bounce(ref hitDirection);
                    }
                }

                // Interaction with death triggers
                foreach (var trigger in CurrentLevel.DeathTriggers)
                {
                    if (ball.Intersect(trigger, ref hitDirection))
                    {
                        Balls.Remove(ball);
                        if (Balls.Count < 1)
                        {
                            for (int j = Collectables.Count - 1; j >= 0; j--)
                            {
                                Collectables.RemoveAt(i);
                            }
                            RemoveLife();
                        }
                        continue;
                    }
                }

                // Interaction with bricks
                foreach (var brickRow in CurrentLevel.BrickRows)
                {
                    for (int j = brickRow.Count - 1; j >= 0; j--)
                    {
                        Brick brick = brickRow[j];

                        if (ball.Intersect(brick, ref hitDirection))
                        {
                            brick.Hit(ball.Damage);
                            if (!brick.Alive())
                            {
                                CurrentPlayerStats.Score += brick.BrickPoints;

                                int rnd = _randomGenerator.Next(0, 100);
                                if (rnd < 10)
                                {
                                    Collectables.Add(new Collectable(Collectable.CollectableType.clone_all, brick.X, brick.Y));
                                }
                                else if (rnd > 10 && rnd < 15)
                                {
                                    Collectables.Add(new Collectable(Collectable.CollectableType.increase_width, brick.X, brick.Y));
                                }
                                else if (rnd > 15 && rnd < 20)
                                {
                                    Collectables.Add(new Collectable(Collectable.CollectableType.decrease_width, brick.X, brick.Y));
                                }
                                else if (rnd > 20 && rnd < 25)
                                {
                                    Collectables.Add(new Collectable(Collectable.CollectableType.decrease_speed, brick.X, brick.Y));
                                }
                                else if (rnd > 25 && rnd < 30)
                                {
                                    Collectables.Add(new Collectable(Collectable.CollectableType.add_life, brick.X, brick.Y));
                                }
                                else if (rnd > 30 && rnd < 35)
                                {
                                    Collectables.Add(new Collectable(Collectable.CollectableType.increase_damage, brick.X, brick.Y));
                                }


                                brickRow.Remove(brick);

                                if (CurrentLevel.IsAllEmpty())
                                {
                                    NextLevel();
                                }
                            }
                            ball.Bounce(ref hitDirection);
                        }
                    }
                }
            }
        }