GameRoot.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. //---------------------------------------------------------------------------------
  2. // Ported to the Atomic Game Engine
  3. // Originally written for XNA by Michael Hoffman
  4. // Find the full tutorial at: http://gamedev.tutsplus.com/series/vector-shooter-xna/
  5. //----------------------------------------------------------------------------------
  6. using System;
  7. using AtomicEngine;
  8. using AtomicPlayer;
  9. namespace AtomicBlaster
  10. {
  11. public class GameRoot : AppDelegate
  12. {
  13. public override void Start()
  14. {
  15. Art.Load();
  16. var graphics = AtomicNET.GetSubsystem<Graphics>();
  17. float width = graphics.Width;
  18. float height = graphics.Height;
  19. ScreenSize = new Vector2(width, height);
  20. ScreenBounds = new IntRect(0, 0, (int)ScreenSize.X, (int)ScreenSize.Y);
  21. var renderer = AtomicNET.GetSubsystem<Renderer>();
  22. var viewport = renderer.GetViewport(0);
  23. renderer.HDRRendering = true;
  24. var cache = GetSubsystem<ResourceCache>();
  25. var renderpath = viewport.GetRenderPath().Clone();
  26. renderpath.Append(cache.GetResource<XMLFile>("RenderPath/BloomHDR.xml"));
  27. renderpath.Append(cache.GetResource<XMLFile>("RenderPath/Blur.xml"));
  28. viewport.SetRenderPath(renderpath);
  29. Scene = new Scene();
  30. Scene.CreateComponent<Octree>();
  31. var camera = Scene.CreateChild("Camera").CreateComponent<Camera>();
  32. camera.Node.Position = new Vector3(width / 2.0f, height / 2.0f, 0.0f);
  33. camera.Orthographic = true;
  34. camera.OrthoSize = height;
  35. viewport.Scene = Scene;
  36. viewport.Camera = camera;
  37. CustomRenderer.Initialize();
  38. ParticleManager = new ParticleManager<ParticleState>(1024 * 20, ParticleState.UpdateParticle);
  39. #if ATOMIC_DESKTOP
  40. const int maxGridPoints = 1600;
  41. #else
  42. const int maxGridPoints = 400;
  43. #endif
  44. float amt = (float)Math.Sqrt(ScreenBounds.Width * ScreenBounds.Height / maxGridPoints);
  45. Vector2 gridSpacing = new Vector2(amt, amt);
  46. IntRect expandedBounds = ScreenBounds;
  47. expandedBounds.Inflate((int)gridSpacing.X, (int)gridSpacing.Y);
  48. Grid = new Grid(expandedBounds, gridSpacing);
  49. EntityManager.Add(PlayerShip.Instance);
  50. SubscribeToEvent("Update", HandleUpdate);
  51. SubscribeToEvent("RenderPathEvent", HandleRenderPathEvent);
  52. }
  53. void HandleRenderPathEvent(uint eventType, ScriptVariantMap eventData)
  54. {
  55. if (eventData.GetString("name") != "customrender")
  56. return;
  57. CustomRenderer.Begin();
  58. Draw();
  59. CustomRenderer.End();
  60. }
  61. float deltaTime = 0.0f;
  62. void HandleUpdate(uint eventType, ScriptVariantMap eventData)
  63. {
  64. float time = eventData.GetFloat("timestep");
  65. ElapsedTime += time;
  66. #if !ATOMIC_IOS
  67. deltaTime += time;
  68. if (deltaTime < 1.0f / 60.0f)
  69. return;
  70. deltaTime = 0.0f;
  71. #endif
  72. ShipInput.Update();
  73. if (!paused)
  74. {
  75. PlayerStatus.Update();
  76. EntityManager.Update();
  77. EnemySpawner.Update();
  78. ParticleManager.Update();
  79. Grid.Update();
  80. }
  81. }
  82. void Draw()
  83. {
  84. EntityManager.Draw();
  85. Grid.Draw();
  86. ParticleManager.Draw();
  87. }
  88. // GodMode by default as the game is really hard :)
  89. public static bool GodMode = true;
  90. public static Scene Scene { get; private set; }
  91. public static float ElapsedTime { get; private set; }
  92. public static Vector2 ScreenSize { get; private set; }
  93. public static IntRect ScreenBounds { get; private set; }
  94. public static Grid Grid { get; private set; }
  95. public static ParticleManager<ParticleState> ParticleManager { get; private set; }
  96. bool paused = false;
  97. }
  98. }