StarWarriorGame.cs 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. #region File description
  2. // --------------------------------------------------------------------------------------------------------------------
  3. // <copyright file="StarWarriorGame.cs" company="GAMADU.COM">
  4. // Copyright © 2013 GAMADU.COM. All rights reserved.
  5. //
  6. // Redistribution and use in source and binary forms, with or without modification, are
  7. // permitted provided that the following conditions are met:
  8. //
  9. // 1. Redistributions of source code must retain the above copyright notice, this list of
  10. // conditions and the following disclaimer.
  11. //
  12. // 2. Redistributions in binary form must reproduce the above copyright notice, this list
  13. // of conditions and the following disclaimer in the documentation and/or other materials
  14. // provided with the distribution.
  15. //
  16. // THIS SOFTWARE IS PROVIDED BY GAMADU.COM 'AS IS' AND ANY EXPRESS OR IMPLIED
  17. // WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  18. // FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GAMADU.COM OR
  19. // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  20. // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  22. // ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  23. // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  24. // ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. //
  26. // The views and conclusions contained in the software and documentation are those of the
  27. // authors and should not be interpreted as representing official policies, either expressed
  28. // or implied, of GAMADU.COM.
  29. // </copyright>
  30. // <summary>
  31. // This is the main type for your game.
  32. // </summary>
  33. // --------------------------------------------------------------------------------------------------------------------
  34. #endregion File description
  35. namespace StarWarrior
  36. {
  37. #region Using statements
  38. using System;
  39. using Artemis;
  40. using Artemis.System;
  41. using Microsoft.Xna.Framework;
  42. using Microsoft.Xna.Framework.Graphics;
  43. using Microsoft.Xna.Framework.Input;
  44. using StarWarrior.Components;
  45. using StarWarrior.Templates;
  46. #endregion
  47. /// <summary>This is the main type for Star Warrior.</summary>
  48. public class StarWarriorGame : Game
  49. {
  50. /// <summary>The one second.</summary>
  51. private static readonly TimeSpan OneSecond = TimeSpan.FromSeconds(1);
  52. /// <summary>The graphics.</summary>
  53. private readonly GraphicsDeviceManager graphics;
  54. /// <summary>The elapsed time.</summary>
  55. private TimeSpan elapsedTime;
  56. /// <summary>The font.</summary>
  57. private SpriteFont font;
  58. /// <summary>The frame counter.</summary>
  59. private int frameCounter;
  60. /// <summary>The frame rate.</summary>
  61. private int frameRate;
  62. /// <summary>The sprite batch.</summary>
  63. private SpriteBatch spriteBatch;
  64. /// <summary>The entityWorld.</summary>
  65. private EntityWorld entityWorld;
  66. /// <summary>Initializes a new instance of the <see cref="StarWarriorGame" /> class.</summary>
  67. public StarWarriorGame()
  68. {
  69. this.elapsedTime = TimeSpan.Zero;
  70. this.graphics = new GraphicsDeviceManager(this)
  71. {
  72. IsFullScreen = false,
  73. PreferredBackBufferHeight = 720,
  74. PreferredBackBufferWidth = 1280,
  75. PreferredBackBufferFormat = SurfaceFormat.Color,
  76. PreferMultiSampling = false,
  77. PreferredDepthStencilFormat = DepthFormat.None
  78. };
  79. #if DEBUG
  80. this.graphics.SynchronizeWithVerticalRetrace = false;
  81. #else
  82. this.graphics.SynchronizeWithVerticalRetrace = true;
  83. #endif
  84. this.IsFixedTimeStep = false; //this wont work when vsync is ON
  85. this.Content.RootDirectory = "Content";
  86. }
  87. /// <summary>This is called when the game should draw itself.</summary>
  88. /// <param name="gameTime">Provides a snapshot of timing values.</param>
  89. protected override void Draw(GameTime gameTime)
  90. {
  91. string fps = string.Format("fps: {0}", this.frameRate);
  92. #if DEBUG
  93. string entityCount = string.Format("Active entities: {0}", this.entityWorld.EntityManager.ActiveEntities.Count);
  94. string removedEntityCount = string.Format("Removed entities: {0}", this.entityWorld.EntityManager.TotalRemoved);
  95. string totalEntityCount = string.Format("Total entities: {0}", this.entityWorld.EntityManager.TotalCreated);
  96. #endif
  97. this.GraphicsDevice.Clear(Color.Black);
  98. this.spriteBatch.Begin();
  99. this.entityWorld.Draw();
  100. this.spriteBatch.DrawString(this.font, fps, new Vector2(32, 32), Color.Yellow);
  101. #if DEBUG
  102. this.spriteBatch.DrawString(this.font, entityCount, new Vector2(32, 62), Color.Yellow);
  103. this.spriteBatch.DrawString(this.font, removedEntityCount, new Vector2(32, 92), Color.Yellow);
  104. this.spriteBatch.DrawString(this.font, totalEntityCount, new Vector2(32, 122), Color.Yellow);
  105. #endif
  106. this.spriteBatch.End();
  107. }
  108. /// <summary>Allows the game to perform any initialization it needs to before starting to run.
  109. /// This is where it can query for any required services and load any non-graphic
  110. /// related content. Calling base.Initialize will enumerate through any components
  111. /// and initialize them as well.</summary>
  112. protected override void Initialize()
  113. {
  114. // Create a new SpriteBatch, which can be used to draw textures.
  115. this.spriteBatch = new SpriteBatch(this.GraphicsDevice);
  116. this.font = this.Content.Load<SpriteFont>("myFont");
  117. this.entityWorld = new EntityWorld();
  118. EntitySystem.BlackBoard.SetEntry("ContentManager", this.Content);
  119. EntitySystem.BlackBoard.SetEntry("GraphicsDevice", this.GraphicsDevice);
  120. EntitySystem.BlackBoard.SetEntry("SpriteBatch", this.spriteBatch);
  121. EntitySystem.BlackBoard.SetEntry("SpriteFont", this.font);
  122. EntitySystem.BlackBoard.SetEntry("EnemyInterval", 500);
  123. #if XBOX
  124. this.entityWorld.InitializeAll( System.Reflection.Assembly.GetExecutingAssembly());
  125. #else
  126. this.entityWorld.InitializeAll(true);
  127. #endif
  128. this.InitializePlayerShip();
  129. this.InitializeEnemyShips();
  130. base.Initialize();
  131. }
  132. /// <summary>Allows the game to run logic such as updating the entityWorld,
  133. /// checking for collisions, gathering input, and playing audio.</summary>
  134. /// <param name="gameTime">Provides a snapshot of timing values.</param>
  135. protected override void Update(GameTime gameTime)
  136. {
  137. if (Keyboard.GetState().IsKeyDown(Keys.Escape) ||
  138. GamePad.GetState(PlayerIndex.One).IsButtonDown(Buttons.Back))
  139. {
  140. this.Exit();
  141. }
  142. this.entityWorld.Update();
  143. ++this.frameCounter;
  144. this.elapsedTime += gameTime.ElapsedGameTime;
  145. if (this.elapsedTime > OneSecond)
  146. {
  147. this.elapsedTime -= OneSecond;
  148. this.frameRate = this.frameCounter;
  149. this.frameCounter = 0;
  150. }
  151. }
  152. /// <summary>The initialize enemy ships.</summary>
  153. private void InitializeEnemyShips()
  154. {
  155. Random random = new Random();
  156. for (int index = 0; 2 > index; ++index)
  157. {
  158. Entity entity = this.entityWorld.CreateEntityFromTemplate(EnemyShipTemplate.Name);
  159. entity.GetComponent<TransformComponent>().X = random.Next(this.GraphicsDevice.Viewport.Width - 100) + 50;
  160. entity.GetComponent<TransformComponent>().Y = random.Next((int)((this.GraphicsDevice.Viewport.Height * 0.75) + 0.5)) + 50;
  161. entity.GetComponent<VelocityComponent>().Speed = 0.05f;
  162. entity.GetComponent<VelocityComponent>().Angle = random.Next() % 2 == 0 ? 0 : 180;
  163. entity.Refresh();
  164. }
  165. }
  166. /// <summary>The initialize player ship.</summary>
  167. private void InitializePlayerShip()
  168. {
  169. Entity entity = this.entityWorld.CreateEntity();
  170. entity.Group = "SHIPS";
  171. entity.AddComponentFromPool<TransformComponent>();
  172. entity.AddComponent(new SpatialFormComponent("PlayerShip"));
  173. entity.AddComponent(new HealthComponent(30));
  174. entity.GetComponent<TransformComponent>().X = this.GraphicsDevice.Viewport.Width * 0.5f;
  175. entity.GetComponent<TransformComponent>().Y = this.GraphicsDevice.Viewport.Height - 50;
  176. entity.Tag = "PLAYER";
  177. entity.Refresh();
  178. }
  179. }
  180. }