PageRenderTime 103ms CodeModel.GetById 40ms app.highlight 9ms RepoModel.GetById 51ms app.codeStats 0ms

/Samples/SimpleGameExample/Game.cs

#
C# | 196 lines | 117 code | 10 blank | 69 comment | 6 complexity | 9823bde1a4baa72bc12f754461f43019 MD5 | raw file
  1using System.Collections.Generic;
  2using Delta.Engine;
  3using Delta.Engine.Dynamic;
  4using Delta.Rendering.Basics.Materials;
  5using Delta.Rendering.Basics.Fonts;
  6using Delta.Utilities.Datatypes;
  7using Delta.InputSystem;
  8using Delta.Utilities.Helpers;
  9
 10namespace SimpleGameExample
 11{
 12	/// <summary>
 13	/// Game class, which is the entry point for this game. Manages all the game
 14	/// logic and displays everything. More complex games do this differently.
 15	/// </summary>
 16	public class Game : DynamicModule
 17	{
 18		#region Constants
 19		/// <summary>
 20		/// Update the trail 60 times per second.
 21		/// </summary>
 22		private const int NumberOfUpdatesPerSecond = 60;
 23		/// <summary>
 24		/// Keep the trail for 2 seconds.
 25		/// </summary>
 26		private const int TrailLengthInSeconds = 2;
 27		/// <summary>
 28		/// Interpolate the trail to make it look more smooth than the input
 29		/// allowes (which might jump great distances).
 30		/// </summary>
 31		private const int NumberOfInterpolations = 3;
 32		#endregion
 33
 34		#region Variables
 35		/// <summary>
 36		/// Just load the DeltaEngineLogo image from the SimpleGameExample sample
 37		/// content project. Or if that is not available the DeltaEngineLogo image
 38		/// fallback content project, which is always available.
 39		/// </summary>
 40		private readonly Material2DColored gameObject =
 41			new Material2DColored("DeltaEngineLogo");
 42		/// <summary>
 43		/// Welcome text, which can be changed once a UIClick happened.
 44		/// </summary>
 45		private string welcomeText =
 46			"Press Space, GamePad, Click or Touch to see the hidden message";
 47		/// <summary>
 48		/// Start drawing in the middle, but allow moving around with cursors or
 49		/// any input device returning position change (mouse, touch, gamepad).
 50		/// </summary>
 51		private Point drawLocation = Point.Half;
 52
 53		/// <summary>
 54		/// Last positions for the trail that is slowly fading out.
 55		/// </summary>
 56		readonly List<Point> lastPositions = new List<Point>();
 57		/// <summary>
 58		/// Last times (in milliseconds) for the trail to reconstruct the size
 59		/// and rotation values.
 60		/// </summary>
 61		readonly List<long> lastTimeMs = new List<long>();
 62		/// <summary>
 63		/// Last colors for the trail to make everything a bit more colorful.
 64		/// </summary>
 65		readonly List<Color> lastColors = new List<Color>();
 66		/// <summary>
 67		/// Current color used for drawing the trail.
 68		/// </summary>
 69		private Color currentColor = Color.Random;
 70		/// <summary>
 71		/// Next color we are fading to for the trail.
 72		/// </summary>
 73		private Color nextColor = Color.Random;
 74		#endregion
 75
 76		#region Constructor
 77		/// <summary>
 78		/// Constructor, do additional initialization code here if needed.
 79		/// </summary>
 80		public Game()
 81			: base("Simple Game", typeof(Application))
 82		{
 83			// Note: Normally in a game you would define those commands via the
 84			// InputSettings.xml in the ContentManager, don't use the commands here!
 85			Input.Commands[Command.UIClick].Add(delegate
 86			{
 87				Application.BackgroundColor = Color.DarkBlue;
 88				welcomeText = "You made it! Yay. Now write some game code :)";
 89			});
 90			CommandDelegate moveDrawLocation = delegate(CommandTrigger command)
 91			{
 92				//Log.Info("Position=" + command.Position + ", from " + command.Button);
 93				drawLocation = command.Position;//.Movement / 10.0f;
 94			};
 95			Input.Commands[Command.CameraRotateLeft].Add(moveDrawLocation);
 96			Input.Commands[Command.CameraRotateRight].Add(moveDrawLocation);
 97			Input.Commands[Command.CameraRotateUp].Add(moveDrawLocation);
 98			Input.Commands[Command.CameraRotateDown].Add(moveDrawLocation);
 99			Input.Commands[Command.QuitTest].Add(delegate
100			{
101				Application.Quit();
102			});
103		}
104		#endregion
105
106		#region Run
107		/// <summary>
108		/// Run game loop, called every frame to do all game logic updating.
109		/// Note: put your game logic here, it will be executed each frame.
110		/// There is no difference between update code and render code as all
111		/// rendering will happen optimized at the end of the frame anyway!
112		/// </summary>
113		public override void Run()
114		{
115			// Show FPS
116			Font.DrawTopLeftInformation("Fps: " + Time.Fps);
117
118			// Draw the trail and then the current object
119			for (int num = 0; num < lastPositions.Count; num++)
120			{
121				DrawGameObject(lastPositions[num], lastTimeMs[num], lastColors[num],
122					num / (float)lastPositions.Count);
123			}
124			DrawGameObject(drawLocation, Time.Milliseconds, Color.White, 1.0f);
125
126			// Change color every second
127			if (Time.EverySecond)
128			{
129				currentColor = nextColor;
130				nextColor = Color.Random;
131			}
132
133			// Remember position for the trail 30 times per second.
134			if (Time.CheckEvery(1.0f / NumberOfUpdatesPerSecond))
135			{
136				while (lastPositions.Count >
137					NumberOfUpdatesPerSecond * TrailLengthInSeconds)
138				{
139					lastPositions.RemoveAt(0);
140					lastTimeMs.RemoveAt(0);
141					lastColors.RemoveAt(0);
142				}
143				float colorPerecentage = (Time.Milliseconds % 1000) / 1000.0f;
144				Color newColor = Color.Lerp(currentColor, nextColor,
145					colorPerecentage);
146				// Add few interpolated entries between this one and the last, this
147				// will make the output look much more smooth :)
148				if (lastPositions.Count >= NumberOfInterpolations)
149				{
150					// This way the output looks more smooth :)
151					Point p1 = drawLocation;
152					Point p2 = lastPositions[
153						lastPositions.Count - NumberOfInterpolations];
154					long t1 = Time.Milliseconds;
155					long t2 = lastTimeMs[
156						lastTimeMs.Count - NumberOfInterpolations];
157					for (int num = 1; num < NumberOfInterpolations; num++)
158					{
159						float interpolation = num / (float)NumberOfInterpolations;
160						lastPositions.Add(Point.Lerp(p1, p2, interpolation));
161						lastTimeMs.Add(MathHelper.Lerp(t1, t2, interpolation));
162						lastColors.Add(newColor);
163					}
164				}
165				lastPositions.Add(drawLocation);
166				lastTimeMs.Add(Time.Milliseconds);
167				lastColors.Add(newColor);
168			}
169
170			// And a simple text message
171			Font.Default.Draw(welcomeText,
172				new Rectangle(0.0f,
173					ScreenSpace.DrawArea.Bottom - Font.Default.LineHeight, 1.0f,
174					Font.Default.LineHeight));
175		}
176		#endregion
177
178		#region DrawGameObject
179		/// <summary>
180		/// Draw game object, also used for the trail.
181		/// </summary>
182		/// <param name="position">Current position</param>
183		/// <param name="milliseconds">Time for this object or trail</param>
184		/// <param name="color">Color for the trail</param>
185		/// <param name="alpha">Alpha for drawing</param>
186		private void DrawGameObject(Point position, long milliseconds, Color color,
187			float alpha)
188		{
189			gameObject.BlendColor = new Color(color, alpha);
190			gameObject.Draw(Rectangle.FromCenter(position, gameObject.Size + 0.5f *
191				gameObject.Size * MathHelper.Sin(milliseconds / 20.0f)),
192				milliseconds / 10.0f);
193		}
194		#endregion
195	}
196}