예제 #1
0
        protected override void Update(GameTime gameTime)
        {
            base.Update(gameTime);

            //Get input
            HandleInput(gameTime);

            //Get time of first frame
            if (startTime == -1)
            {
                startTime = (int)gameTime.TotalGameTime.TotalSeconds;
            }

            //Countdown
            countdown = startTime + TrafficGame.Duration - (int)gameTime.TotalGameTime.TotalSeconds;
            countdownUI.Update(countdown);
            if (countdown == -3)
            {
                EndGame();
            }

            //Detect if in target lane
            int lane = Road.GetLane(player.Position.X, 0.35f);

            inTargetLane = lane == environment.road.GetHighlightAtPlayerPos();
            environment.road.SetHighlightStatus(lane);

            //Step forward time (slo-mo if player crashed or countdown finished)
            float timeScale = 1.0f;

            if (player.crashed || countdown <= 0)
            {
                timeScale = 0.25f;
            }
            world.Step((float)gameTime.ElapsedGameTime.TotalSeconds * timeScale);

            if (state == GameState.RUNNING)
            {
                if (!player.crashed)
                {
                    //Minimum of 1 point per frame.
                    //Score based on velocity, double points if in correct lane
                    //TODO: For low velocities, we may want less than 1 point per frame.
                    int deltaScore = (int)Math.Max(1, (player.Velocity.Y * gameTime.ElapsedGameTime.TotalSeconds));
                    if (inTargetLane)
                    {
                        deltaScore *= 2;
                    }
                    score += deltaScore;
                }
            }
            else if (state == GameState.STARTING)
            {
                Camera.main.Zoom = Math.Min(1, Camera.main.Zoom + 0.05f);      //Zoom out camera to normal position
                if (gameTime.TotalGameTime.TotalSeconds - stateChangeTime > 3) //Start game after 3 seconds
                {
                    state           = GameState.RUNNING;
                    stateChangeTime = gameTime.TotalGameTime.TotalSeconds;
                    player.Velocity = new Vector2(player.Velocity.X, adjustedSpeed);
                }
            }
            else if (state == GameState.RESTARTING)
            {
                Camera.main.Zoom = Math.Max(0, Camera.main.Zoom - 0.05f);      //Zoom camera in as a transition
                if (gameTime.TotalGameTime.TotalSeconds - stateChangeTime > 1) //Reset world after 1 second
                {
                    state           = GameState.STARTING;
                    stateChangeTime = gameTime.TotalGameTime.TotalSeconds;
                    environment.Reset();
                    trafficManager.Reset();
                    player.Reset();
                    score = 0;
                }
            }

            //Update game stuff
            trafficManager.Update(gameTime, player, state);
            environment.Update(gameTime, player);
            player.Update(gameTime, InputManager.LateralMovement);

            //Light & camera follow player
            Camera.main.Target = new Vector2(player.Position.X, player.Position.Y);
            Lighting.Position  = new Vector3(Lighting.Position.X, player.Position.Y + 15, Lighting.Position.Z);

            //Update GUI
            scoreUI.Update(gameTime, score);
            fpsUI.Update(gameTime);
            titleUI.Update(gameTime);
        }