Beispiel #1
0
        public override void Update(ref TouchHandler touchHandler_, GameTime gameTime_)
        {
            // Update in game state
            // ================

            // Update touch input handler
            touchHandler_.Update();

            // Create lists to contain touches for each player
            List <List <TouchLocation> > playerTouches = new List <List <TouchLocation> >();

            for (int p = 0; p < m_players.Count; ++p)
            {
                playerTouches.Add(new List <TouchLocation>());
            }

            // Iterate through each player and sort out which touches are for which player
            foreach (TouchLocation tl in touchHandler_.GetTouches())
            {
                for (int index = 0; index < playerTouches.Count; ++index)
                {
                    if (m_players[index].m_controlScheme.GetTouchZone().IsInsideZone(tl.Position))
                    {
                        playerTouches[index].Add(tl);
                    }
                }
            }

            //only want to do this when the main game isnt running
            if (!PlayersReady)
            {
                //check if a players ready up button has been pressed
                foreach (TouchLocation tl in touchHandler_.GetTouches())
                {
                    for (int index = 0; index < m_players.Count; ++index)
                    {
                        if (m_players[index].m_ReadyButton.Activated(tl.Position))
                        {
                            m_players[index].m_ReadyUp = true;
                            m_players[index].m_ReadyButton.m_sprite.SetTexture(TextureHandler.readySelectedButton);
                        }
                    }
                }

                //Set to 0 every run, so that it doesnt count multiple touches from same person
                NoPlayersReady = 0;

                foreach (Player p in m_players)
                {
                    if (p.m_ReadyUp == true)
                    {
                        NoPlayersReady += 1;
                    }
                }

                //When all players have readied up, start main game
                if (NoPlayersReady == m_players.Count)
                {
                    countdownBeeps.IsLooped = false;
                    countdownBeeps.Play();
                    PlayersReady = true;
                }
                h_trackHandler.Update(m_players, ref m_rankings);
            }
            else if (PlayersReady)
            {
                startTimer.Update(gameTime_.ElapsedGameTime.Milliseconds);
                if (startTimer.timerFinished)
                {
                    for (int i = 0; i < m_players.Count; ++i)
                    {
                        if (m_players[i].m_currentLap == Settings.m_number_laps)
                        {
                            m_players[i].SetFinished(true);
                            m_players[i].GetVehicle().SetToSensor();
                            m_players[i].AddFinishTime(gameTime_.ElapsedGameTime.Milliseconds);
                            if (m_winner == 0)
                            {
                                m_winner = m_players[i].GetID();
                            }
                        }
                        if (!m_players[i].GetFinished() || (m_players[i].GetFinished() && m_players[i].GetFinishTime() < 500))
                        {
                            bool left  = false;
                            bool right = false;
                            bool brake = false;
                            if (Keyboard.GetState().IsKeyDown(Keys.Left))
                            {
                                left = true;
                            }
                            if (Keyboard.GetState().IsKeyDown(Keys.Right))
                            {
                                right = true;
                            }
                            if (Keyboard.GetState().IsKeyDown(Keys.Space) || Keyboard.GetState().IsKeyDown(Keys.Down))
                            {
                                brake = true;
                            }

                            m_players[i].KeyboardMove(left, right, brake);
                            m_players[i].Update(playerTouches[i], m_rankings[i]);
                            m_players[i].AddRaceTime(gameTime_.ElapsedGameTime.Milliseconds);
                        }
                    }
                }
                // Victory state

                finished = true;
                foreach (Player p in m_players)
                {
                    if (!p.GetFinished())
                    {
                        finished = false;
                        break;
                    }
                }
                if (finished)
                {
                    m_currentExecutionState = ExecutionState.CHANGING;
                }

                // Update game objects

                float turn = 0;
                if (Keyboard.GetState().IsKeyDown(Keys.Left))
                {
                    turn--;
                }
                if (Keyboard.GetState().IsKeyDown(Keys.Right))
                {
                    turn++;
                }
                bool braked = Keyboard.GetState().IsKeyDown(Keys.Down);


                h_trackHandler.Update(m_players, ref m_rankings);

                // Update sprites
                foreach (AnimatedSprite anim in m_animatedSprites)
                {
                    // If currently animating
                    if (anim.GetCurrentState() == 1)
                    {
                        // Change the frame
                        if (gameTime_.TotalGameTime.TotalMilliseconds - anim.GetLastTime() > anim.GetFrameTime())
                        {
                            anim.ChangeFrame(gameTime_.TotalGameTime.TotalMilliseconds);
                        }
                    }
                }

                foreach (CheckpointContainer cp in h_trackHandler.m_checkpoints)
                {
                    cp.GetEntity().UpdateSprites();
                }

                //this set the sprite for the rankings, to the players to the corresponding rank
                for (int i = 0; i < m_rankings.Count; i++)
                {
                    m_rankingSprites[i].SetTexture(m_players[m_rankings[i] - 1].GetVehicle().m_vehicleBody.GetSprite().GetTexture());
                }

                // Update particles
                m_particles = GraphicsHandler.UpdatePFX(gameTime_.ElapsedGameTime.TotalMilliseconds);

                fs_world.Step((float)gameTime_.ElapsedGameTime.TotalMilliseconds * .001f);
            }
        }