示例#1
0
        public override void Update(GameTime gameTime)
        {
            ObjectManager obj = ObjectManager.GetInstance();
            Paddle        p1  = obj.GetPaddle();
            KeyboardState kb  = Keyboard.GetState();

            if (!_isInPlay)
            {
                SetVelocity(p1.GetVelocity());

                if (kb.IsKeyDown(Keys.Space))
                {
                    SetVelocity(_movementSpeed, -_movementSpeed);
                    _isInPlay = true;
                }
            }
            else
            {
                //Pause the ball // TODO: make a cheat
                if (kb.IsKeyDown(Keys.P))
                {
                    if (GetVelocity() == Vector2.Zero)
                    {
                        SetVelocity(_oldVelocity);
                    }
                    else
                    {
                        _oldVelocity = GetVelocity();
                        SetVelocity(Vector2.Zero);
                    }
                }

                // Wall Collision
                // Right wall
                if ((GetPosition().X + GetBoundingBox().Width) >= obj.WindowBounds.Width)
                {
                    SetVelocity(GetVelocity().X * -1, GetVelocity().Y);
                }
                // Left wall
                if (GetPosition().X <= 0)
                {
                    SetVelocity(GetVelocity().X * -1, GetVelocity().Y);
                }
                // Top wall
                if (GetPosition().Y <= 0)
                {
                    SetVelocity(GetVelocity().X, GetVelocity().Y * -1);
                }
                // Bottom wall
                if ((GetPosition().Y + GetBoundingBox().Height) >= obj.WindowBounds.Height || kb.IsKeyDown(Keys.R))
                {
                    // reset ball
                    _isInPlay = false;
                    SetVelocity(Vector2.Zero);
                    SetPosition(GetStartPosition());
                    _fRotation  = 1.0f;
                    _fSpinSpeed = 0.1f;
                    // reset paddle
                    p1.SetPosition(p1.GetStartPosition());
                }

                // Paddle Collision
                Rectangle currentRect = GetCollisionBox();
                Rectangle paddleRect  = p1.GetCollisionBox();

                if (currentRect.Intersects(paddleRect)) //TODO seriously ??? sad... just sad...
                {
                    if (GetOldPosition().X < GetPosition().X)
                    {
                        SetVelocity(GetVelocity().X, GetVelocity().Y * -1);
                    }
                    else if (GetOldPosition().X > GetPosition().X)
                    {
                        SetVelocity(GetVelocity().X, GetVelocity().Y * -1);
                    }
                    else
                    {
                        SetVelocity(GetVelocity() * -1);
                    }
                }

                // Brick Collision
                foreach (Brick brick in obj.GetBricks())
                {
                    if (brick.GetCollisionBox().Intersects(GetCollisionBox())) // TODO this is terrible collision detection
                    {
                        SetVelocity(GetVelocity() * -1);
                        brick.IsVisable = false;
                        brick.IsActive  = false;
                        break;
                    }
                }
            }

            // Update the position
            base.Update(gameTime);
            // Update the rotation
            _fRotation += _fSpinSpeed;
        }