コード例 #1
0
        public override void Update(float dt)
        {
            base.Update(dt);

            if (Input2.GamePad0.Select.Press)
            {
                Director.Instance.ReplaceScene(new MenuScene());
            }
            // something!
            //Update the physics simulation
            m_physics.Simulate();

            bool ballIsInContact = false;

            //Now check if the ball hit either paddle, and if so, play the sound
            if (m_physics.QueryContact((uint)PongPhysics.BODIES.Ball, (uint)PongPhysics.BODIES.Player1) ||
                m_physics.QueryContact((uint)PongPhysics.BODIES.Ball, (uint)PongPhysics.BODIES.Ai) ||
                m_physics.QueryContact((uint)PongPhysics.BODIES.Ball, (uint)PongPhysics.BODIES.Player2))
            {
                ballIsInContact = true;
                // This sound is annoying, so it is commented out!
                // if(m_pongBlipSoundPlayer.Status == SoundStatus.Stopped)
                // m_pongBlipSoundPlayer.Play();
            }

            //Check if the ball went off the top or bottom of the screen and update score accordingly
            Results result = Results.StillPlaying;
            bool    scored = false;

            if (m_ball.Position.Y > Director.Instance.GL.Context.GetViewport().Height + m_ball.Scale.Y / 2)
            {
                result = m_scoreboard.AddScore(true);
                scored = true;

                m_playerScore = m_scoreboard.m_playerScore;
            }
            if (m_ball.Position.Y < 0 - m_ball.Scale.Y / 2)
            {
                result = m_scoreboard.AddScore(false);
                scored = true;
            }

            // Did someone win?  If so, show the GameOver scene
            if (result == Results.AiWin)
            {
                Director.Instance.ReplaceScene(new GameOverScene(false, m_playerScore, m_client));
            }
            if (result == Results.PlayerWin)
            {
                Director.Instance.ReplaceScene(new GameOverScene(true, m_playerScore, m_client));
            }

            //If someone did score, but game isn't over, reset the ball position to the middle of the screen
            if (scored == true)
            {
                ResetBall();
            }

            //Finally a sanity check to make sure the ball didn't leave the field.
            var ballPB = m_physics.SceneBodies[(int)PongPhysics.BODIES.Ball];

            if (ballPB.Position.X < -(m_ball.Scale.X / 2f) / PongPhysics.PtoM ||
                ballPB.Position.X > (Director.Instance.GL.Context.GetViewport().Width) / PongPhysics.PtoM)
            {
                ResetBall();
            }
            else if (!ballIsInContact)
            {
                m_ball.CheckVelocity();
            }
        }