Game1.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using Microsoft.Xna.Framework;
  5. using Microsoft.Xna.Framework.Audio;
  6. using Microsoft.Xna.Framework.Content;
  7. using Microsoft.Xna.Framework.GamerServices;
  8. using Microsoft.Xna.Framework.Graphics;
  9. using Microsoft.Xna.Framework.Input;
  10. using Microsoft.Xna.Framework.Media;
  11. using Artemis;
  12. using StarWarrior.Components;
  13. using StarWarrior.Systems;
  14. using StarWarrior.Primitives;
  15. namespace StarWarrior
  16. {
  17. /// <summary>
  18. /// This is the main type for your game
  19. /// </summary>
  20. ///
  21. public class Game1 : Microsoft.Xna.Framework.Game
  22. {
  23. GraphicsDeviceManager graphics;
  24. SpriteBatch spriteBatch;
  25. private EntityWorld world;
  26. private EntitySystem renderSystem;
  27. private EntitySystem hudRenderSystem;
  28. private EntitySystem controlSystem;
  29. private EntitySystem movementSystem;
  30. private EntitySystem enemyShooterSystem;
  31. private EntitySystem enemyShipMovementSystem;
  32. private EntitySystem collisionSystem;
  33. private EntitySystem healthBarRenderSystem;
  34. private EntitySystem enemySpawnSystem;
  35. private EntitySystem expirationSystem;
  36. private SpriteFont font;
  37. private GamePool pool;
  38. int frameRate,frameCounter;
  39. TimeSpan elapsedTime = TimeSpan.Zero;
  40. public Game1()
  41. {
  42. graphics = new GraphicsDeviceManager(this);
  43. graphics.SynchronizeWithVerticalRetrace = false;
  44. this.IsFixedTimeStep = false;
  45. //graphics.IsFullScreen = false;
  46. graphics.PreferredBackBufferHeight = 600;
  47. graphics.PreferredBackBufferWidth = 800;
  48. Content.RootDirectory = "Content";
  49. }
  50. /// <summary>
  51. /// Allows the game to perform any initialization it needs to before starting to run.
  52. /// This is where it can query for any required services and load any non-graphic
  53. /// related content. Calling base.Initialize will enumerate through any components
  54. /// and initialize them as well.
  55. /// </summary>
  56. ///
  57. private void RemovedComponent(Entity e,Component c)
  58. {
  59. if (c != null)
  60. {
  61. pool.AddComponent(c.GetType(), c);
  62. }
  63. }
  64. protected override void Initialize()
  65. {
  66. // Create a new SpriteBatch, which can be used to draw textures.
  67. Type[] types = new Type[] {typeof(Enemy),typeof(Expires),typeof(Health),typeof(SpatialForm),typeof(Transform),typeof(Velocity),typeof(Weapon)};
  68. pool = new GamePool(100,types);
  69. pool.Initialize();
  70. spriteBatch = new SpriteBatch(GraphicsDevice);
  71. world = new EntityWorld();
  72. world.GetEntityManager().RemovedComponentEvent += new RemovedComponentHandler(RemovedComponent);
  73. world.SetPool(pool);
  74. font = Content.Load<SpriteFont>("Arial");
  75. SystemManager systemManager = world.GetSystemManager();
  76. renderSystem = systemManager.SetSystem(new RenderSystem(GraphicsDevice,spriteBatch,Content),ExecutionType.Draw);
  77. hudRenderSystem = systemManager.SetSystem(new HudRenderSystem(spriteBatch, font), ExecutionType.Draw);
  78. controlSystem = systemManager.SetSystem(new MovementSystem(spriteBatch), ExecutionType.Update,1);
  79. movementSystem = systemManager.SetSystem(new PlayerShipControlSystem(spriteBatch),ExecutionType.Update);
  80. enemyShooterSystem = systemManager.SetSystem(new EnemyShipMovementSystem(spriteBatch), ExecutionType.Update,1);
  81. enemyShipMovementSystem = systemManager.SetSystem(new EnemyShooterSystem(), ExecutionType.Update);
  82. collisionSystem = systemManager.SetSystem(new CollisionSystem(), ExecutionType.Update,1);
  83. healthBarRenderSystem = systemManager.SetSystem(new HealthBarRenderSystem(spriteBatch, font), ExecutionType.Draw);
  84. enemySpawnSystem = systemManager.SetSystem(new EnemySpawnSystem(500, spriteBatch), ExecutionType.Update);
  85. expirationSystem = systemManager.SetSystem(new ExpirationSystem(), ExecutionType.Update);
  86. systemManager.InitializeAll();
  87. InitPlayerShip();
  88. InitEnemyShips();
  89. base.Initialize();
  90. }
  91. private void InitEnemyShips() {
  92. Random r = new Random();
  93. for (int i = 0; 2 > i; i++) {
  94. Entity e = EntityFactory.CreateEnemyShip(world);
  95. e.GetComponent<Transform>().SetLocation(r.Next(GraphicsDevice.Viewport.Width), r.Next(400)+50);
  96. e.GetComponent<Velocity>().SetVelocity(0.05f);
  97. e.GetComponent<Velocity>().SetAngle(r.Next() % 2 == 0 ? 0 : 180);
  98. e.Refresh();
  99. }
  100. }
  101. private void InitPlayerShip() {
  102. Entity e = world.CreateEntity();
  103. e.SetGroup("SHIPS");
  104. e.AddComponent(pool.TakeComponent<Transform>());
  105. e.AddComponent(pool.TakeComponent<SpatialForm>());
  106. e.AddComponent(pool.TakeComponent<Health>());
  107. e.GetComponent<SpatialForm>().SetSpatialFormFile("PlayerShip");
  108. e.GetComponent<Health>().SetHealth(30);
  109. e.GetComponent<Transform>().SetCoords(new Vector3(GraphicsDevice.Viewport.Width / 2, GraphicsDevice.Viewport.Height - 50, 0));
  110. e.Refresh();
  111. world.GetTagManager().Register("PLAYER", e);
  112. }
  113. /// <summary>
  114. /// LoadContent will be called once per game and is the place to load
  115. /// all of your content.
  116. /// </summary>
  117. protected override void LoadContent()
  118. {
  119. }
  120. DateTime dt = DateTime.Now;
  121. /// <summary>
  122. /// Allows the game to run logic such as updating the world,
  123. /// checking for collisions, gathering input, and playing audio.
  124. /// </summary>
  125. /// <param name="gameTime">Provides a snapshot of timing values.</param>
  126. protected override void Update(GameTime gameTime)
  127. {
  128. TimeSpan elapsed = DateTime.Now - dt;
  129. dt = DateTime.Now;
  130. frameCounter++;
  131. world.LoopStart();
  132. world.SetDelta(elapsed.Milliseconds);
  133. world.GetSystemManager().UpdateAsynchronous(ExecutionType.Update);
  134. //world.GetSystemManager().UpdateSynchronous(ExecutionType.Update);
  135. elapsedTime += elapsed;
  136. if (elapsedTime > TimeSpan.FromSeconds(1))
  137. {
  138. elapsedTime -= TimeSpan.FromSeconds(1);
  139. frameRate = frameCounter;
  140. frameCounter = 0;
  141. }
  142. base.Update(gameTime);
  143. }
  144. /// <summary>
  145. /// This is called when the game should draw itself.
  146. /// </summary>
  147. /// <param name="gameTime">Provides a snapshot of timing values.</param>
  148. protected override void Draw(GameTime gameTime)
  149. {
  150. string fps = string.Format("fps: {0}", frameRate);
  151. GraphicsDevice.Clear(Color.Black);
  152. spriteBatch.Begin();
  153. spriteBatch.DrawString(font, fps, new Vector2(32,32), Color.Yellow);
  154. world.GetSystemManager().UpdateSynchronous(ExecutionType.Draw);
  155. spriteBatch.End();
  156. base.Draw(gameTime);
  157. }
  158. }
  159. }