PageRenderTime 27ms CodeModel.GetById 18ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

/Samples/CarGame2D/CarGame.cs

#
C# | 148 lines | 98 code | 12 blank | 38 comment | 9 complexity | 0a2fdc3e16b257e1d4bc5b91ddfb50a2 MD5 | raw file
  1using System;
  2using Delta.Engine;
  3using Delta.Rendering.Basics.Fonts;
  4using Delta.Rendering.Basics.Materials;
  5using Delta.Utilities.Datatypes;
  6using Delta.Utilities.Helpers;
  7using Delta.InputSystem;
  8
  9namespace CarGame2D
 10{
 11	/// <summary>
 12	/// Handles most of the game logic and drawing.
 13	/// </summary>
 14	public class CarGame
 15	{
 16		#region Private
 17		/// <summary>
 18		/// The track to drive on.
 19		/// </summary>
 20		private Track track;
 21		/// <summary>
 22		/// The car controlled by the player.
 23		/// </summary>
 24		private Car playerCar;
 25		/// <summary>
 26		/// The time in seconds we're already driving.
 27		/// </summary>
 28		private float elapsedTotalTime;
 29		/// <summary>
 30		/// The background material.
 31		/// </summary>
 32		private Material2D background;
 33		/// <summary>
 34		/// The number of finished rounds.
 35		/// </summary>
 36		private int finishedRounds;
 37		/// <summary>
 38		/// The best time a round was finished in.
 39		/// </summary>
 40		private float bestRoundTime;
 41		/// <summary>
 42		/// The time since the round started.
 43		/// </summary>
 44		private float elapsedRoundTime;
 45		#endregion
 46
 47		#region Constructor
 48		/// <summary>
 49		/// Create a new car game by the specified track.
 50		/// </summary>
 51		/// <param name="setTrack">Track to drive on.</param>
 52		public CarGame(Track setTrack)
 53		{
 54			track = setTrack;
 55			playerCar = new Car(track.TrackStart, track);
 56			background = new Material2D("Background")
 57			{
 58				DrawLayer = Delta.Rendering.Enums.RenderLayer.Background,
 59			};
 60
 61			finishedRounds = MathHelper.InvalidIndex;
 62
 63			RegisterInputCommands();
 64		}
 65		#endregion
 66
 67		#region RegisterInputCommands
 68		/// <summary>
 69		/// Register input commands
 70		/// </summary>
 71		private void RegisterInputCommands()
 72		{
 73			// Allow to quit game easily with Escape, Back, etc.
 74			Input.Commands[Command.QuitTest].Add(delegate
 75			{
 76				Application.Quit();
 77			});
 78		}
 79		#endregion
 80
 81		#region Draw
 82		/// <summary>
 83		/// Update and draw the car game.
 84		/// </summary>
 85		public void Draw()
 86		{
 87			elapsedTotalTime += Time.Delta;
 88			elapsedRoundTime += Time.Delta;
 89
 90			// Draw background 4x4 times to be more sharp
 91			for (int x = 0; x < 4; x++)
 92			{
 93				for (int y = 0; y < 4; y++)
 94				{
 95					background.Draw(new Rectangle(0.25f * x, 0.25f * y, 0.25f, 0.25f));
 96				}
 97			}
 98
 99			Font.Default.Draw(
100				"Total Time: " +
101				MathHelper.Round(elapsedTotalTime, 2).ToString("00.00") + "s",
102				new Rectangle(0.0f, ScreenSpace.DrawArea.Top, 1.0f,
103					Font.Default.LineHeight));
104			if (finishedRounds > 0)
105			{
106				Font.Default.Draw(
107					"Best Round Time: " +
108					MathHelper.Round(bestRoundTime, 2).ToString("00.00") + "s",
109					new Rectangle(0.0f,
110						ScreenSpace.DrawArea.Top + Font.Default.LineHeight, 1.0f,
111						Font.Default.LineHeight));
112				Font.Default.Draw(
113					"Rounds finished: " + finishedRounds,
114					new Rectangle(0.0f,
115						ScreenSpace.DrawArea.Top + Font.Default.LineHeight * 2, 1.0f,
116						Font.Default.LineHeight));
117			}
118			track.Draw();
119
120			// Update the players car and check for a finished round.
121			int lastNextIndex = playerCar.NextTrackIndex;
122			playerCar.Update();
123			if (lastNextIndex != playerCar.NextTrackIndex &&
124				playerCar.NextTrackIndex == 1)
125			{
126				if (finishedRounds == 0)
127				{
128					bestRoundTime = elapsedRoundTime;
129				}
130				else
131				{
132					if (elapsedRoundTime < bestRoundTime)
133					{
134						bestRoundTime = elapsedRoundTime;
135					}
136				}
137
138				finishedRounds++;
139				elapsedRoundTime = 0f;
140			}
141
142			playerCar.Draw();
143
144			//track.DebugDraw();
145		}
146		#endregion
147	}
148}