Exemplo n.º 1
0
        public Vector2 NextPosition(float scaleSpeed)
        {
            _position = Pong.Add(_position, Pong.Multiply(_velocity, scaleSpeed));

            if (_position.X <= _topBounds.X) //Score
            {
                HitScoreEvent();
                _position.X  = _topBounds.X;
                _velocity.X *= -1;
            }

            if (_position.Y <= _topBounds.Y)
            {
                HitWallEvent();
                _position.Y  = _topBounds.Y;
                _velocity.Y *= -1;
            }

            if (_position.X >= _bottomBounds.X) //Score
            {
                HitScoreEvent();
                _position.X  = _bottomBounds.X;
                _velocity.X *= -1;
            }

            if (_position.Y >= _bottomBounds.Y)
            {
                HitWallEvent();
                _position.Y  = _bottomBounds.Y;
                _velocity.Y *= -1;
            }

            Random _rnd = new Random((int)DateTime.Now.Ticks);

            _redPaddleVelocity  = _rnd.Next(5, 10) * 100;
            _redPaddleDirection = (_redPaddle.Y + (_currentGame.Sprites.RedPaddle.Size.Y / 2)) < _position.Y;
            _redPaddle.Y       += scaleSpeed * _redPaddleVelocity * (_redPaddleDirection ? 1 : -1);

            if (_redPaddle.Y < 0)
            {
                _redPaddle.Y = 0;
            }
            else if (_redPaddle.Y + _currentGame.Sprites.RedPaddle.Size.Y > Pong.StandardDimentions.Y)
            {
                _redPaddle.Y = Pong.StandardDimentions.Y - _currentGame.Sprites.RedPaddle.Size.Y;
            }

            _bluePaddleVelocity  = _rnd.Next(5, 10) * 100;
            _bluePaddleDirection = (_bluePaddle.Y + (_currentGame.Sprites.BluePaddle.Size.Y / 2)) < _position.Y;
            _bluePaddle.Y       += scaleSpeed * _bluePaddleVelocity * (_bluePaddleDirection ? 1 : -1);

            if (_bluePaddle.Y < 0)
            {
                _bluePaddle.Y = 0;
            }
            else if (_bluePaddle.Y + _currentGame.Sprites.BluePaddle.Size.Y > Pong.StandardDimentions.Y)
            {
                _bluePaddle.Y = Pong.StandardDimentions.Y - _currentGame.Sprites.BluePaddle.Size.Y;
            }

            BoundingBox _redPaddleBound = new BoundingBox(
                new Vector3(_redPaddle, 0),
                new Vector3(Pong.Add(_redPaddle, _currentGame.Sprites.RedPaddle.Size), 0)
                );

            BoundingBox _bluePaddleBound = new BoundingBox(
                new Vector3(_bluePaddle, 0),
                new Vector3(Pong.Add(_bluePaddle, _currentGame.Sprites.BluePaddle.Size), 0)
                );

            BoundingBox _ballBounds = new BoundingBox(
                new Vector3(_position, 0),
                new Vector3(Pong.Add(_position, _currentGame.Sprites.Ball.Size), 0)
                );

            if (_ballBounds.Intersects(_redPaddleBound))
            {
                HitPaddleEvent();
                _position.X  = _redPaddle.X + _currentGame.Sprites.RedPaddle.Size.X;
                _velocity.X *= -1;
            }

            if (_ballBounds.Intersects(_bluePaddleBound))
            {
                HitPaddleEvent();
                _position.X  = _bluePaddle.X - _currentGame.Sprites.Ball.Size.X;
                _velocity.X *= -1;
            }
            return(_position);
        }
Exemplo n.º 2
0
        public Vector2 NextPosition(float scaleSpeed)
        {
            _position = Pong.Add(_position, Pong.Multiply(_velocity, scaleSpeed));

            if (_position.X <= _topBounds.X) //Score
            {
                if (HitScoreEvent != null)
                {
                    HitScoreEvent(PlayerIndex.Two);
                }
                _position.X  = _topBounds.X;
                _velocity.X *= -1;
            }

            if (_position.Y <= _topBounds.Y)
            {
                if (HitWallEvent != null)
                {
                    HitWallEvent();
                }
                _position.Y  = _topBounds.Y;
                _velocity.Y *= -1;
            }

            if (_position.X >= _bottomBounds.X) //Score
            {
                if (HitScoreEvent != null)
                {
                    HitScoreEvent(PlayerIndex.One);
                }
                _position.X  = _bottomBounds.X;
                _velocity.X *= -1;
            }

            if (_position.Y >= _bottomBounds.Y)
            {
                if (HitWallEvent != null)
                {
                    HitWallEvent();
                }
                _position.Y  = _bottomBounds.Y;
                _velocity.Y *= -1;
            }

            _redPaddleVelocity = 1000;
            _redPaddle.Y      += scaleSpeed * _redPaddleVelocity * (int)_redPaddleDirection;

            if (_redPaddle.Y < 0)
            {
                _redPaddle.Y = 0;
            }
            else if (_redPaddle.Y + _currentGame.Sprites.RedPaddle.Size.Y > Pong.StandardDimentions.Y)
            {
                _redPaddle.Y = Pong.StandardDimentions.Y - _currentGame.Sprites.RedPaddle.Size.Y;
            }

            if (GameMode == GameMode.SinglePlayer)
            {
                _bluePaddleVelocity  = new Random((int)DateTime.Now.Ticks).Next(5, 10) * 100;
                _bluePaddleDirection = ((_bluePaddle.Y + (_currentGame.Sprites.BluePaddle.Size.Y / 2)) < _position.Y) ?
                                       PaddleDirection.Down :
                                       PaddleDirection.Up;
            }
            else
            {
                _bluePaddleVelocity = 1000;
            }
            _bluePaddle.Y += scaleSpeed * _bluePaddleVelocity * (int)_bluePaddleDirection;

            if (_bluePaddle.Y < 0)
            {
                _bluePaddle.Y = 0;
            }
            else if (_bluePaddle.Y + _currentGame.Sprites.BluePaddle.Size.Y > Pong.StandardDimentions.Y)
            {
                _bluePaddle.Y = Pong.StandardDimentions.Y - _currentGame.Sprites.BluePaddle.Size.Y;
            }

            BoundingBox _redPaddleBound = new BoundingBox(
                new Vector3(_redPaddle, 0),
                new Vector3(Pong.Add(_redPaddle, _currentGame.Sprites.RedPaddle.Size), 0)
                );

            BoundingBox _bluePaddleBound = new BoundingBox(
                new Vector3(_bluePaddle, 0),
                new Vector3(Pong.Add(_bluePaddle, _currentGame.Sprites.BluePaddle.Size), 0)
                );

            BoundingBox _ballBounds = new BoundingBox(
                new Vector3(_position, 0),
                new Vector3(Pong.Add(_position, _currentGame.Sprites.Ball.Size), 0)
                );

            if (_ballBounds.Intersects(_redPaddleBound))
            {
                //switch (_redPaddleDirection)
                //{
                //    case PaddleDirection.Up:
                //        if (_velocity.Y <= 0)
                //            _velocity.Y /= _velocityMultiplier;
                //        else
                //            _velocity.Y *= _velocityMultiplier;
                //        break;

                //    case PaddleDirection.Down:
                //        if (_velocity.Y >= 0)
                //            _velocity.Y /= _velocityMultiplier;
                //        else
                //            _velocity.Y *= _velocityMultiplier;
                //        break;
                //}

                if (HitPaddleEvent != null)
                {
                    HitPaddleEvent();
                }
                _position.X  = _redPaddle.X + _currentGame.Sprites.RedPaddle.Size.X;
                _velocity.X *= -1;
            }

            if (_ballBounds.Intersects(_bluePaddleBound))
            {
                //switch (_bluePaddleDirection)
                //{
                //    case PaddleDirection.Up:
                //        if (_velocity.Y <= 0)
                //            _velocity.Y /= _velocityMultiplier;
                //        else
                //            _velocity.Y *= _velocityMultiplier;
                //        break;

                //    case PaddleDirection.Down:
                //        if (_velocity.Y >= 0)
                //            _velocity.Y /= _velocityMultiplier;
                //        else
                //            _velocity.Y *= _velocityMultiplier;
                //        break;
                //}

                if (HitPaddleEvent != null)
                {
                    HitPaddleEvent();
                }
                _position.X  = _bluePaddle.X - _currentGame.Sprites.Ball.Size.X;
                _velocity.X *= -1;
            }

            return(_position);
        }