/// <summary> /// Create new replay for a specific track number. /// </summary> /// <param name="setTrackNum">Set track number</param> public Replay(int setTrackNum, bool createNew, Track track) { trackNum = setTrackNum; // if creating new, we're done if (createNew == true) { return; } bool replayFileFound = false; FileHelper.StorageContainerMRE.WaitOne(); FileHelper.StorageContainerMRE.Reset(); try { StorageDevice storageDevice = FileHelper.XnaUserDevice; if ((storageDevice != null) && storageDevice.IsConnected) { IAsyncResult async = storageDevice.BeginOpenContainer("RacingGame", null, null); async.AsyncWaitHandle.WaitOne(); using (StorageContainer container = storageDevice.EndOpenContainer(async)) { async.AsyncWaitHandle.Close(); if (container.FileExists(ReplayFilenames[trackNum])) { using (Stream stream = container.OpenFile(ReplayFilenames[trackNum], FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) { using (BinaryReader reader = new BinaryReader(stream)) { // Load total lap time lapTime = reader.ReadSingle(); // Load matrix values int numOfMatrixValues = reader.ReadInt32(); for (int num = 0; num < numOfMatrixValues; num++) trackMatrixValues.Add( FileHelper.ReadMatrix(reader)); // Load checkpoint times int numOfCheckpointTimes = reader.ReadInt32(); for (int num = 0; num < numOfCheckpointTimes; num++) checkpointTimes.Add(reader.ReadSingle()); } } } } } } catch (Exception exc) { System.Diagnostics.Trace.WriteLine("Settings Load Failure: " + exc.ToString()); } FileHelper.StorageContainerMRE.Set(); // Load if possible if (!replayFileFound && File.Exists(Path.Combine( Directories.ContentDirectory, ReplayFilenames[trackNum]))) { using (Stream stream = TitleContainer.OpenStream( "Content\\" + ReplayFilenames[trackNum])) { using (BinaryReader reader = new BinaryReader(stream)) { // Load total lap time lapTime = reader.ReadSingle(); // Load matrix values int numOfMatrixValues = reader.ReadInt32(); for (int num = 0; num < numOfMatrixValues; num++) trackMatrixValues.Add( FileHelper.ReadMatrix(reader)); // Load checkpoint times int numOfCheckpointTimes = reader.ReadInt32(); for (int num = 0; num < numOfCheckpointTimes; num++) checkpointTimes.Add(reader.ReadSingle()); } } } if (!replayFileFound) { // Create new default replay for this track! // Get all track positions based on the current top highscore time! lapTime = Highscores.GetTopLapTime(trackNum); int numOfMatrixValues = 1 + (int)(lapTime / TrackMatrixIntervals); float lastTrackPos = 0.0f; int oldTrackSegmentNumber = 0; // Go twice as long and abort when we reach the finish line! for (int num = 0; num < numOfMatrixValues * 2; num++) { // See Landscape.TestRenderLandscape for more details. float carTrackPos = 0.00001f + ((float)num / (float)(numOfMatrixValues - 1));// * //not needed, scaled in GetTrackPositionMatrix: track.Length; float difference = carTrackPos - lastTrackPos; carTrackPos = lastTrackPos + difference * 0.1f; lastTrackPos = carTrackPos; float roadWidth, nextRoadWidth; Matrix carMatrix = track.GetTrackPositionMatrix(carTrackPos, out roadWidth, out nextRoadWidth); // Store trackMatrixValues.Add(carMatrix); // Also check if we passed a checkpoint int trackSegmentNumber = (int)(carTrackPos * track.NumberOfSegments); // Segment changed if (trackSegmentNumber != oldTrackSegmentNumber) { // Check if we passed a checkpoint. for (int checkpointNum = 0; checkpointNum < track. CheckpointSegmentPositions.Count; checkpointNum++) { // We have to check if we are between the old // and the current track segement numbers, we // might skip one or two in 200ms. if (track.CheckpointSegmentPositions[checkpointNum] > oldTrackSegmentNumber && track.CheckpointSegmentPositions[checkpointNum] <= trackSegmentNumber) { // We passed that checkpoint, add the simulated time checkpointTimes.Add( lapTime * (float)num / (float)(numOfMatrixValues - 1)); break; } } } oldTrackSegmentNumber = trackSegmentNumber; // Reached finish? if (carTrackPos >= 1.0f) // Then abort, do not add more. break; } // Add the final checkpoint for the laptime checkpointTimes.Add(lapTime); } }
/// <summary> /// Create new replay for a specific track number. /// </summary> /// <param name="setTrackNum">Set track number</param> public Replay(int setTrackNum, bool createNew, Track track) { trackNum = setTrackNum; // If not creating new, load it from file! if (createNew == false) { // Load if possible if (File.Exists(Path.Combine( Directories.ContentDirectory, ReplayFilenames[trackNum]))) { FileStream stream = FileHelper.LoadGameContentFile( "Content\\"+ReplayFilenames[trackNum]); BinaryReader reader = new BinaryReader(stream); // Load total lap time lapTime = reader.ReadSingle(); // Load matrix values int numOfMatrixValues = reader.ReadInt32(); for (int num = 0; num < numOfMatrixValues; num++) trackMatrixValues.Add( FileHelper.ReadMatrix(reader)); // Load checkpoint times int numOfCheckpointTimes = reader.ReadInt32(); for (int num = 0; num < numOfCheckpointTimes; num++) checkpointTimes.Add(reader.ReadSingle()); // Loading complete, close. stream.Close(); } // if else { // Create new default replay for this track! // Get all track positions based on the current top highscore time! lapTime = Highscores.GetTopLapTime(trackNum); int numOfMatrixValues = 1 + (int)(lapTime / TrackMatrixIntervals); float lastTrackPos = 0.0f; int oldTrackSegmentNumber = 0; // Go twice as long and abort when we reach the finish line! for (int num = 0; num < numOfMatrixValues*2; num++) { // See Landscape.TestRenderLandscape for more details. float carTrackPos = 0.00001f + ((float)num / (float)(numOfMatrixValues - 1));// * //not needed, scaled in GetTrackPositionMatrix: track.Length; float difference = carTrackPos - lastTrackPos; carTrackPos = lastTrackPos + difference * 0.1f; lastTrackPos = carTrackPos; float roadWidth, nextRoadWidth; Matrix carMatrix = track.GetTrackPositionMatrix(carTrackPos, out roadWidth, out nextRoadWidth); //Log.Write("create num=" + num + // "\ncarTrackPos=" + carTrackPos + // "\ncarMatrix=" + carMatrix); // Interpolate carPos a little //not good: //carPos = Vector3.SmoothStep(carPos, carMatrix.Translation, 0.25f); //carMatrix.Translation = carPos; // Store trackMatrixValues.Add(carMatrix); // Also check if we passed a checkpoint int trackSegmentNumber = (int)(carTrackPos * track.NumberOfSegments); // Segment changed if (trackSegmentNumber != oldTrackSegmentNumber) { // Check if we passed a checkpoint. for (int checkpointNum = 0; checkpointNum < track. CheckpointSegmentPositions.Count; checkpointNum++) { // We have to check if we are between the old and the current // track segement numbers, we might skip one or two in 200ms. if (track.CheckpointSegmentPositions[checkpointNum] > oldTrackSegmentNumber && track.CheckpointSegmentPositions[checkpointNum] <= trackSegmentNumber) { // We passed that checkpoint, add the simulated time checkpointTimes.Add( lapTime * (float)num / (float)(numOfMatrixValues - 1)); break; } // if } // for } // if oldTrackSegmentNumber = trackSegmentNumber; // Reached finish? if (carTrackPos >= 1.0f) // Then abort, do not add more. break; } // for (all matrix values) // Add the final checkpoint for the laptime checkpointTimes.Add(lapTime); } // else (not loading) } // if (createNew == false) }
/// <summary> /// Reload level /// </summary> /// <param name="setLevel">Level</param> internal void ReloadLevel(RacingGameManager.Level setLevel) { level = setLevel; // Load track based on the level selection, do this after // we got all the height data because the track might be adjusted. if (track == null) track = new Track("Track" + level.ToString(), this); else track.Reload("Track" + level.ToString(), this); // Load replay for this track to show best player bestReplay = new Replay((int)level, false, track); newReplay = new Replay((int)level, true, track); // Kill brake tracks brakeTracksVertices.Clear(); brakeTracksVerticesArray = null; // Set car at start pos SetCarToStartPosition(); }
/// <summary> /// Dispose /// </summary> /// <param name="someObject">Some object</param> public static void Dispose(ref Track someObject) { if (someObject != null) someObject.Dispose(); someObject = null; }
public static void TestTrackScreenshot() { Track track = null; Model testModel = null; TestGame.Start( delegate { track = new Track( //"TrackWithHelpers", null); //"TrackAdvanced", null); "TrackBeginner", null); testModel = new Model("Car"); }, delegate { RacingGameManager.Player.SetCarPosition( track.points[(int)(track.points.Count*0.93f)].pos + new Vector3(0, -50, 0), new Vector3(0, 1, 0), new Vector3(0, 0, 1)); track.roadMaterial.ambientColor = Color.Gray; track.Render(); /* testModel.RenderCar(0, Color.White, Matrix.CreateRotationX(Input.MousePos.X/400.0f) * Matrix.CreateTranslation( track.points[(int)(track.points.Count * 0.95f)].pos)); */ }); }
/// <summary> /// Test render track /// </summary> //[Test] public static void TestRenderTrack() { Track track = null; Model testModel = null; TestGame.Start( delegate { track = new Track( //"TrackWithHelpers", null); //"TrackAdvanced", null); "TrackBeginner", null); testModel = new Model("AlphaPalm2"); }, delegate { //RacingGameManager.Player.SetCarPosition(track.points[0].pos, // new Vector3(0, 1, 0), new Vector3(0, 0, 1)); //ShowGroundGrid(); //ShowTrackLines(track); ShowUpVectors(track); track.Render(); //testModel.Render(track.StartPosition); }); }