Exemple #1
0
        /// <summary>
        /// When the device is loaded for the first time or when it resets come here to check
        /// the caps of the device for lighting and set up the Projection and view.
        /// </summary>
        /// <param name="sender">Device</param>
        /// <param name="e">usally null, either way we dont care about it.</param>
        private void OnDeviceReset(object sender, EventArgs e)
        {
            device.Transform.Projection = Matrix.PerspectiveFovLH((float)Math.PI / 4, (float)this.Width / (float)this.Height, 1.0f, 1000.0f);

            device.Transform.View = Matrix.LookAtLH(new Vector3(0.0f, 9.5f, 17.0f), new Vector3(), new Vector3(0, 1, 0));

            //try to set up a texture minify filter, pick anisotropic first
            if (device.DeviceCaps.TextureFilterCaps.SupportsMinifyAnisotropic)
            {
                device.SamplerState[0].MinFilter = TextureFilter.Anisotropic;
            }
            else if (device.DeviceCaps.TextureFilterCaps.SupportsMinifyLinear)
            {
                device.SamplerState[0].MinFilter = TextureFilter.Linear;
            }

            //do the same thing for magnify filter
            if (device.DeviceCaps.TextureFilterCaps.SupportsMagnifyAnisotropic)
            {
                device.SamplerState[0].MagFilter = TextureFilter.Anisotropic;
            }
            else if (device.DeviceCaps.TextureFilterCaps.SupportsMagnifyLinear)
            {
                device.SamplerState[0].MagFilter = TextureFilter.Linear;
            }

            //Do we have enough support for lights?
            if ((device.DeviceCaps.VertexProcessingCaps.SupportsDirectionalLights) && (device.DeviceCaps.MaxActiveLights > 1))
            {
                //first light
                device.Lights[0].Type      = LightType.Directional;
                device.Lights[0].Diffuse   = Color.White;
                device.Lights[0].Direction = new Vector3(1, -1, -1);
                device.Lights[0].Enabled   = true;

                //second light
                device.Lights[1].Type      = LightType.Directional;
                device.Lights[1].Diffuse   = Color.White;
                device.Lights[1].Direction = new Vector3(-1, 1, -1);
                device.Lights[1].Enabled   = true;
            }
            else
            {
                //no light support, just use ambient lighting.
                device.RenderState.Ambient = Color.White;
            }

            roadMesh = LoadMesh(device, @"..\..\road.x", ref roadMaterials, ref roadTextures);

            //create teh car
            car = new Car(device);

            //create the obstacles
            obstacles = new Obstacles();
        }
Exemple #2
0
        private void OnFrameUpdate()
        {
            if ((isGameOver) || (!hasGameStarted))
            {
                return;
            }

            // First, get the elapsed time
            elapsedTime = Utility.Timer(DirectXTimer.GetElapsedTime);

            RoadDepth0 += (RoadSpeed * elapsedTime);
            RoadDepth1 += (RoadSpeed * elapsedTime);

            // We have two "roads" here a top and bottom, check to see where they are located and if
            //they exceed their bounds, then swap them.
            if (RoadDepth0 > 75.0f)
            {
                RoadDepth0 = RoadDepth1 - 100.0f;
                AddObstacles(RoadDepth0);
            }

            if (RoadDepth1 > 75.0f)
            {
                RoadDepth1 = RoadDepth0 - 100.0f;
                AddObstacles(RoadDepth1);
            }

            // Remove any obstacles that are past the car
            // Increase the score for each one, and also increase
            // the road speed to make the game harder.
            Obstacles removeObstacles = new Obstacles();

            foreach (Obstacle o in obstacles)
            {
                if (o.Depth > car.Diameter - (Car.Depth * 2))
                {
                    // Add this obstacle to our list to remove
                    removeObstacles.Add(o);
                    // Increase roadspeed
                    RoadSpeed += RoadSpeedIncrement;

                    // Make sure the road speed stays below max
                    if (RoadSpeed >= MaximumRoadSpeed)
                    {
                        RoadSpeed = MaximumRoadSpeed;
                    }

                    // Increase the car speed as well
                    car.IncrementSpeed();

                    // Add the new score
                    score += (int)(RoadSpeed * (RoadSpeed / car.Speed));
                }
            }
            // Remove the obstacles in the list
            foreach (Obstacle o in removeObstacles)
            {
                obstacles.Remove(o);
                // May as well dispose it as well
                o.Dispose();
            }
            removeObstacles.Clear();

            // Move our obstacles
            foreach (Obstacle o in obstacles)
            {
                // Update the obstacle, check to see if it hits the car
                o.Update(elapsedTime, RoadSpeed);
                if (o.IsHittingCar(car.Location, car.Diameter))
                {
                    // If it does hit the car, the game is over.
                    isGameOver   = true;
                    gameOverTick = System.Environment.TickCount;
                    // Stop our timer
                    Utility.Timer(DirectXTimer.Stop);
                    // Check to see if we want to add this to our high scores list
                    CheckHighScore();
                }
            }
            car.Update(elapsedTime);
        }