PageRenderTime 30ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/Samples/SimpleGameExample/Game.cs

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