Exemplo n.º 1
0
        private void Update(double elapsedTime)
        {
            int lifeImagesCount = _lifeImages.Count;

            for (int i = lifeImagesCount; i < Program.lifes; i++)
            {
                Text lifeImage = new Text("|", _font, (uint)_lifeImagesSize);
                MainMenu.SetTextOriginToMiddle(lifeImage);
                lifeImage.Position = _lifeImagesPosition + (_lifeImagesDistance + Vector2D.Right() * lifeImage.GetGlobalBounds().Width) * i + (Vector2D.Right() * lifeImage.GetGlobalBounds().Width) / 2;
                _lifeImages.Add(lifeImage);
            }

            bool rectangleObject1Collided = false;
            bool rectangleObject2Collided = false;

            if (_player1 != null)
            {
                rectangleObject1Collided = _player1.PongCollision(_circleObject);
            }
            if (_player2 != null)
            {
                rectangleObject2Collided = _player2.PongCollision(_circleObject);
            }

            int numberOfBrickCollisions           = 0;
            List <RectangleObject> collidedBricks = new List <RectangleObject>();

            Vector2D oldCircleDirection = _circleObject.Direction;

            foreach (RectangleObject brick in _bricks)
            {
                if (brick.NewReflectionCollision(_circleObject, _animationManager))
                {
                    brick.Life--;

                    if (brick.Life >= 0)
                    {
                        brick.ChangeToPrefab(RectangleObject.rectangleShapePrefabs[brick.Life]);
                    }

                    numberOfBrickCollisions++;
                    collidedBricks.Add(brick);

                    Vector2D originalPosition = Vector2D.Zero();

                    foreach (RectangleObject brickPrefab in _bricksPrefab)
                    {
                        if (brickPrefab.ID == brick.ID)
                        {
                            originalPosition = brickPrefab.Rectangle.Position;
                        }
                    }

                    _animationManager.RemoveAnimationsToObject(brick.Rectangle);

                    Vector2D animationPosition = brick.Rectangle.Position + oldCircleDirection * 5;
                    _animationManager.AddAnimation(new Animation(brick.Rectangle.Position, new Transformable[] { brick.Rectangle }, 200, 0, animationPosition));
                    _animationManager.AddAnimation(new Animation(animationPosition, new Transformable[] { brick.Rectangle }, 200, 200, originalPosition));
                }
            }

            if (numberOfBrickCollisions > 0)
            {
                if (_player1 != null && _player2 != null)
                {
                    if (_lastHit.Equals(_player1))
                    {
                        _score1 += numberOfBrickCollisions;
                    }

                    if (_lastHit.Equals(_player2))
                    {
                        _score2 += numberOfBrickCollisions;
                    }
                }
                else
                {
                    if (_player1 != null)
                    {
                        _score1++;
                    }

                    if (_player2 != null)
                    {
                        _score2++;
                    }
                }

                foreach (RectangleObject brick in collidedBricks)
                {
                    if (brick.Life < 0)
                    {
                        if (_random.Next(0, _probabilityOfPowerUp) == 0)
                        {
                            PowerUp newPowerUp = _prefabPowerUps[_random.Next(0, _prefabPowerUps.Count)].Clone();


                            double speed = 0.3f;
                            newPowerUp.Spawn(new Vector2D(brick.Rectangle.Position.X, brick.Rectangle.Position.Y), new Vector2D(0, 1), speed);

                            _powerUps.Add(newPowerUp);
                        }

                        _bricks.Remove(brick);
                    }
                }
            }


            if (_bricks.Count == 0 && _loadedLevel == _persistenceManager.LoadLevelList()[_persistenceManager.LoadLevelList().Count - 1])
            {
                Program.windowState = Program.WindowState.GameOver;
            }
            else if (_bricks.Count == 0 && (rectangleObject1Collided || rectangleObject2Collided))
            {
                _loadedLevel  = _persistenceManager.LoadLevelList()[_levelID++];
                _bricks       = _persistenceManager.LoadLevel(_loadedLevel);
                _bricksPrefab = _persistenceManager.LoadLevel(_loadedLevel);
                for (int i = 0; i < _bricks.Count; i++)
                {
                    _bricks[i].Rectangle.Position       += _brickPosition;
                    _bricksPrefab[i].Rectangle.Position += _brickPosition;
                }
            }


            List <PowerUp> usedPowerUps = new List <PowerUp>();

            foreach (PowerUp powerUp in _powerUps)
            {
                powerUp.Update(elapsedTime);

                RectangleObject collidedRectangle = null;
                if (_player1 != null)
                {
                    if (_player1.PowerUpCollision(powerUp))
                    {
                        collidedRectangle = _player1;
                    }
                }

                if (_player2 != null)
                {
                    if (_player2.PowerUpCollision(powerUp))
                    {
                        collidedRectangle = _player2;
                    }
                }

                if (collidedRectangle != null)
                {
                    powerUp.ExecutePowerUp(collidedRectangle);
                    usedPowerUps.Add(powerUp);
                }
                else if (powerUp.Position.X < 0 || powerUp.Position.X > Program.windowSize.X)
                {
                    usedPowerUps.Add(powerUp);
                }
            }

            foreach (PowerUp powerUp in usedPowerUps)
            {
                _powerUps.Remove(powerUp);
            }

            usedPowerUps.Clear();

            _stateManager.Update(elapsedTime);

            _circleObject.Update(elapsedTime);

            bool resetCircleObject = false;

            if (_circleObject.CheckOutOfField((int)Program.windowSize.X, (int)Program.windowSize.Y))
            {
                Program.lifes--;
                _lifeImages.RemoveAt(_lifeImages.Count - 1);
                if (Program.lifes <= 0)
                {
                    Program.windowState = Program.WindowState.GameOver;
                }

                resetCircleObject = true;
                _circleObject.ResetPosition(_circlePosition);
                _circleObject.Direction = new Vector2D(-_circleObject.Direction.X, _circleObject.Direction.Y);
                _circleObject.Update(0);
                _powerUps.Clear();
            }



            if ((rectangleObject1Collided || rectangleObject2Collided) && _player1 != null && _player2 != null)
            {
                /*Vector2D savedPos = _player1.Rectangle.Position;
                 * _player1.Rectangle.Position = _player2.Rectangle.Position;
                 * _player2.Rectangle.Position = savedPos;*/

                _animationManager.AddAnimation(new Animation(_player1.Rectangle.Position, new Transformable[] { _player2.Rectangle }, 100, 0, false));
                _animationManager.AddAnimation(new Animation(_player2.Rectangle.Position, new Transformable[] { _player1.Rectangle }, 100, 0, false));

                if (rectangleObject1Collided)
                {
                    _lastHit = _player1;
                }
                if (rectangleObject2Collided)
                {
                    _lastHit = _player2;
                }
            }



            _aiLeft.Update(elapsedTime, rectangleObject2Collided, rectangleObject1Collided, resetCircleObject);
            _aiRight.Update(elapsedTime, rectangleObject1Collided, rectangleObject2Collided, resetCircleObject);


            if (_score1 < 10)
            {
                _scoreLeftText.DisplayedString = "0" + _score1.ToString();
            }
            else
            {
                _scoreLeftText.DisplayedString = _score1.ToString();
            }

            if (_score2 < 10)
            {
                _scoreRightText.DisplayedString = "0" + _score2.ToString();
            }
            else
            {
                _scoreRightText.DisplayedString = _score2.ToString();
            }


            _animationManager.Update(elapsedTime);
        }
Exemplo n.º 2
0
        private void Update(double elapsedTime)
        {
            if (_nextPowerUps.Count < 100)
            {
                int i = Random(0, _prefabPowerUps.Count);
                if (i != _nextPowerUps[_nextPowerUps.Count - 1])
                {
                    _nextPowerUps.Add(i);
                }
            }

            bool rectangleObject1Collided = _rectangleObject1.Collision(_circleObject, elapsedTime);
            bool rectangleObject2Collided = _rectangleObject2.Collision(_circleObject, elapsedTime);


            _circleObject.Update(elapsedTime);

            bool resetCircleObject = false;

            if (_circleObject.CheckOutOfField((int)_windowSize.X, (int)_windowSize.Y))
            {
                if (_circleObject.Position.X > _windowSize.X / 2)
                {
                    _scoreLeft++;
                }
                else
                {
                    _scoreRight++;
                }

                resetCircleObject = true;
                _circleObject.ResetPosition(_circlePosition);
                _circleObject.Direction = new Vector2f(-_circleObject.Direction.X, _circleObject.Direction.Y);
                _circleObject.Update(0);
                _powerUps.Clear();
            }

            if ((rectangleObject1Collided || rectangleObject2Collided) && Random(0, _probabilityOfPowerUp) == 0)
            {
                RectangleObject collidedRectangle;
                if (rectangleObject1Collided)
                {
                    collidedRectangle = _rectangleObject2;
                }
                else
                {
                    collidedRectangle = _rectangleObject1;
                }

                PowerUp newPowerUp = _prefabPowerUps[_nextPowerUps[0]].Clone();

                _nextPowerUps.RemoveAt(0);

                float speed = 0.3f;
                if (_windowSize.X / 2 < _circleObject.Position.X)
                {
                    newPowerUp.Spawn(new Vector2f(_rectangleObject2.Position.X - _rectangleObject2.Size.X - newPowerUp.Size.X, _circleObject.Position.Y), new Vector2f(-1, 0), speed, collidedRectangle);
                }
                else
                {
                    newPowerUp.Spawn(new Vector2f(_rectangleObject1.Position.X + _rectangleObject1.Size.X + newPowerUp.Size.X, _circleObject.Position.Y), new Vector2f(1, 0), speed, collidedRectangle);
                }

                _powerUps.Add(newPowerUp);
            }

            List <PowerUp> usedPowerUps = new List <PowerUp>();

            foreach (PowerUp powerUp in _powerUps)
            {
                powerUp.Update(elapsedTime);

                if (_rectangleObject1.PowerUpCollision(powerUp) || _rectangleObject2.PowerUpCollision(powerUp))
                {
                    powerUp.Execute();
                    usedPowerUps.Add(powerUp);
                }
                else if (powerUp.Position.X < 0 || powerUp.Position.X > _windowSize.X)
                {
                    usedPowerUps.Add(powerUp);
                }
            }

            foreach (PowerUp powerUp in usedPowerUps)
            {
                _powerUps.Remove(powerUp);
            }

            usedPowerUps.Clear();


            _aiLeft.Update(elapsedTime, rectangleObject2Collided, rectangleObject1Collided, resetCircleObject);
            _aiRight.Update(elapsedTime, rectangleObject1Collided, rectangleObject2Collided, resetCircleObject);


            if (_scoreLeft < 10)
            {
                _scoreLeftText.DisplayedString = "0" + _scoreLeft.ToString();
            }
            else
            {
                _scoreLeftText.DisplayedString = _scoreLeft.ToString();
            }

            if (_scoreRight < 10)
            {
                _scoreRightText.DisplayedString = "0" + _scoreRight.ToString();
            }
            else
            {
                _scoreRightText.DisplayedString = _scoreRight.ToString();
            }
        }