예제 #1
0
        public override void Update(DeviceContext context, TargetBase target, DeviceManager deviceManager, Point screenSize, float dt, float elapsedTime)
        {
            if (AfterStartFreeze)
            {
                // Player moving
                _playerPos =
                    _playerPos.Add(_playerDir.Mul(_playerSpd).Mul(dt)).Clamp(GamesParams.Margin0.Add(_padSize.Half()),
                                                                             GamesParams.Margin1.Sub(_padSize.Half()));

                // Ball moving
                var oldBallPos = _ballPos;
                _ballPos = _ballPos.Add(_ballDir.Mul(_ballSpd).Mul(dt));

                // Ball collision with borders
                if (!_ballPos.IsInside(GamesParams.Margin0.Add(_ballSize.Half()),
                                       GamesParams.Margin1.Sub(_ballSize.Half())))
                {
                    if (_ballPos.X - GamesParams.MarginX0 < _ballPos.Y - GamesParams.MarginY0 ||
                        GamesParams.MarginX1 - _ballPos.X < _ballPos.Y - GamesParams.MarginY0)
                        _ballDir.X = -_ballDir.X;
                    else
                        _ballDir.Y = -_ballDir.Y;

                    if (_ballPos.Y > GamesParams.MarginY1 - 0.01f)
                    {
                        _playerPos =
                            new Point(0.5f, GamesParams.MarginY1).Clamp(GamesParams.Margin0.Add(_padSize.Half()),
                                                                        GamesParams.Margin1.Sub(_padSize.Half()));
                        _ballPos = _playerPos.Sub(new Point(0, 0.1f));
                        _ballDir = new Point(0.0f, -1.0f).AddNoise().Normalise();
                        _gameManager.Die();
                        return;
                    }
                }

                _ballPos = _ballPos.Clamp(GamesParams.Margin0.Add(_ballSize.Half()),
                                          GamesParams.Margin1.Sub(_ballSize.Half()));

                // Ball collision with player pad
                if (_ballPos.IsInside(_playerPos.Sub(_padSize.Half()).Sub(_ballSize.Half()),
                                      _playerPos.Add(_padSize.Half()).Add(_ballSize.Half())))
                {
                    _ballDir.Y = -1;
                    _ballDir.X = _ballPos.Sub(_playerPos).X/_padSize.Half().X;
                    _ballDir = _ballDir.Normalise();

                    _ballSpd += _ballSpdInc;
                }

                // Ball collision with player pad
                foreach (var block in _blockPos)
                {
                    if (_ballPos.IsInside(block.Sub(_blockSize.Half()).Sub(_ballSize.Half()),
                                          block.Add(_blockSize.Half()).Add(_ballSize.Half())))
                    {
                        if (oldBallPos.X < block.Sub(_blockSize.Half()).Sub(_ballSize.Half()).X ||
                            oldBallPos.X > block.Add(_blockSize.Half()).Add(_ballSize.Half()).X)
                            _ballDir.X = -_ballDir.X;
                        else
                            _ballDir.Y = -_ballDir.Y;

                        block.X = -100;
                        block.Y = -100;

                        _gameManager.AddPoints(100);
                    }
                }
            }

            // Drawing
            var anyBlock = false;
            foreach (var block in _blockPos)
            {
                if (block.X < 0)
                    continue;

                anyBlock = true;
                var box = block.ToBox(_blockSize);
                context.FillRectangle(screenSize.ApplyTo(box), GamesParams.EnemyColor);
            }

            if (!anyBlock)
            {
                NewGame();
                _gameManager.Win(3000);
                return;
            }

            var playerBox = _playerPos.ToBox(_padSize);
            var ballBox = _ballPos.ToBox(_ballSize);
            context.FillRectangle(screenSize.ApplyTo(playerBox), GamesParams.PlayerColor);
            context.FillRectangle(screenSize.ApplyTo(ballBox), GamesParams.AdditionalColor);

            base.Update(context, target, deviceManager, screenSize, dt, elapsedTime);
        }
예제 #2
0
파일: Pong.cs 프로젝트: Maskl/MetroRetro
        public override void Update(DeviceContext context, TargetBase target, DeviceManager deviceManager, Point screenSize, float dt, float elapsedTime)
        {
            if (AfterStartFreeze)
            {
                // Player moving
                _playerPos =
                    _playerPos.Add(_playerDir.Mul(_playerSpd).Mul(dt)).Clamp(GamesParams.Margin0.Add(_padSize.Half()),
                                                                             GamesParams.Margin1.Sub(_padSize.Half()));

                // Ball moving
                _ballPos = _ballPos.Add(_ballDir.Mul(_ballSpd).Mul(dt));

                // Ball collision with borders
                if (!_ballPos.IsInside(GamesParams.Margin0.Add(_ballSize.Half()),
                                       GamesParams.Margin1.Sub(_ballSize.Half())))
                {
                    _ballDir.Y = -_ballDir.Y;

                    if (_ballPos.X < GamesParams.MarginX0 + 0.001f)
                    {
                        NewGame();
                        _gameManager.Die();
                        return;
                    }

                    if (_ballPos.X > GamesParams.MarginX1 - 0.001f)
                    {
                        _gameManager.Win(500);
                        _ballPos = new Point(0.5f, 0.5f);
                        _ballDir = new Point(1.0f, 0.0f).AddNoise().Normalise();
                        return;
                    }
                }

                _ballPos = _ballPos.Clamp(GamesParams.Margin0.Add(_ballSize.Half()),
                                          GamesParams.Margin1.Sub(_ballSize.Half()));

                // Ball collision with player pad
                if (_ballPos.IsInside(_playerPos.Sub(_padSize.Half()).Sub(_ballSize.Half()),
                                      _playerPos.Add(_padSize.Half()).Add(_ballSize.Half())))
                {
                    _ballDir.X = 1;
                    _ballDir.Y = _ballPos.Sub(_playerPos).Y/_padSize.Half().Y;
                    _ballDir = _ballDir.Normalise();

                    _ballSpd += _ballSpdInc;

                    _gameManager.AddPoints(75);
                }

                // Ball collision with enemy pad
                if (_ballPos.IsInside(_enemyPos.Sub(_padSize.Half()).Sub(_ballSize.Half()),
                                      _enemyPos.Add(_padSize.Half()).Add(_ballSize.Half())))
                {
                    _ballDir.X = -1;
                    _ballDir.Y = _ballPos.Sub(_enemyPos).Y/_padSize.Half().Y;
                    _ballDir = _ballDir.Normalise();

                    _ballSpd += _ballSpdInc;
                }

                // Enemy moving
                if (Math.Abs(_ballPos.Y - _enemyPos.Y) < _padSize.Div(3).Y)
                {
                    _enemyDir.Y = 0;
                }
                else
                {
                    if (_ballPos.Y > _enemyPos.Y)
                        _enemyDir.Y = 1;
                    else
                        _enemyDir.Y = -1;

                    _enemyPos =
                        _enemyPos.Add(_enemyDir.Mul(_enemySpd).Mul(dt)).Clamp(GamesParams.Margin0.Add(_padSize.Half()),
                                                                              GamesParams.Margin1.Sub(_padSize.Half()));
                }
            }

            // Drawing
            var playerBox = _playerPos.ToBox(_padSize);
            var enemyBox = _enemyPos.ToBox(_padSize);
            var ballBox = _ballPos.ToBox(_ballSize);

            context.FillRectangle(screenSize.ApplyTo(playerBox), GamesParams.PlayerColor);
            context.FillRectangle(screenSize.ApplyTo(enemyBox), GamesParams.EnemyColor);

            context.FillRectangle(screenSize.ApplyTo(ballBox), GamesParams.AdditionalColor);

            base.Update(context, target, deviceManager, screenSize, dt, elapsedTime);
        }
예제 #3
0
        public override void Update(DeviceContext context, TargetBase target, DeviceManager deviceManager, Point screenSize, float dt, float elapsedTime)
        {
            if (AfterStartFreeze)
            {
                _timeToNextPossibleJump -= dt;

                if (_jumpTimer >= 0)
                {
                    _jumpTimer += dt * JumpSpeed;
                    _playerPos.Y = GroundY - _playerSize.Y / 2 - (float)Math.Sin(_jumpTimer) * JumpHeight;

                    if (_jumpTimer > Math.PI)
                    {
                        _playerPos.Y = GroundY - _playerSize.Y / 2;
                        _jumpTimer = -1;
                    }
                }
                else
                {
                    _canWin -= dt;
                    if (_canWin < 0)
                    {
                        _canWin = 1000;
                        _gameManager.Win(300);
                        return;
                    }
                }

                // Hole moving
                _holePos = _holePos.Add(_holeDir.Mul(_holeSpd).Mul(dt));
                if (_holePos.X + _holeSize.X / 2 <= GamesParams.MarginX0)
                {
                    AddNewObstacle();
                }
                if (_holePos.X < _playerPos.X && _canWin > 100)
                    _canWin = 0.5f;

                // Ball collision with player pad
                if (_holePos.IsInside(_playerPos.Sub(_playerSize.Half()).Sub(_holeSize.Half()),
                                      _playerPos.Add(_playerSize.Half()).Add(_holeSize.Half())))
                {
                    NewGame();
                    _gameManager.Die();
                    return;
                }
            }

            // Drawing
            context.FillRectangle(screenSize.ApplyTo(new RectangleF(GamesParams.MarginX0, GroundY, GamesParams.MarginX1, GamesParams.MarginY1)), GamesParams.ObstaclesColor);

            var holeBox = _holePos.ToBox(_holeSize);
            if (holeBox.Left < GamesParams.MarginX1)
            {
                if (holeBox.Left < GamesParams.MarginX0)
                    holeBox.Left = GamesParams.MarginX0;

                if (holeBox.Right > GamesParams.MarginX1)
                    holeBox.Right = GamesParams.MarginX1;

                context.FillRectangle(screenSize.ApplyTo(holeBox), _holeIsStone ? GamesParams.ObstaclesColor : GamesParams.BackgroundColor);
            }
            var playerBox = _playerPos.ToBox(_playerSize);
            context.FillRectangle(screenSize.ApplyTo(playerBox), GamesParams.PlayerColor);

            base.Update(context, target, deviceManager, screenSize, dt, elapsedTime);
        }