GameRoot.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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 : NETScriptObject
  12. {
  13. public GameRoot()
  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 renderpath = viewport.GetRenderPath().Clone();
  25. renderpath.Append(AtomicNET.Cache.GetResource<XMLFile>("RenderPath/BloomHDR.xml"));
  26. renderpath.Append(AtomicNET.Cache.GetResource<XMLFile>("RenderPath/Blur.xml"));
  27. viewport.SetRenderPath(renderpath);
  28. Scene = new Scene();
  29. Scene.CreateComponent<Octree>();
  30. var camera = Scene.CreateChild("Camera").CreateComponent<Camera>();
  31. camera.Node.Position = new Vector3(width / 2.0f, height / 2.0f, 0.0f);
  32. camera.Orthographic = true;
  33. camera.OrthoSize = height;
  34. viewport.Scene = Scene;
  35. viewport.Camera = camera;
  36. CustomRenderer.Initialize();
  37. ParticleManager = new ParticleManager<ParticleState>(1024 * 20, ParticleState.UpdateParticle);
  38. const int maxGridPoints = 1600;
  39. float amt = (float)Math.Sqrt(ScreenBounds.Width * ScreenBounds.Height / maxGridPoints);
  40. Vector2 gridSpacing = new Vector2(amt, amt);
  41. Grid = new Grid(ScreenBounds, gridSpacing);
  42. EntityManager.Add(PlayerShip.Instance);
  43. SubscribeToEvent("Update", HandleUpdate);
  44. SubscribeToEvent("RenderPathEvent", HandleRenderPathEvent);
  45. }
  46. void HandleRenderPathEvent(uint eventType, ScriptVariantMap eventData)
  47. {
  48. if (eventData.GetString("name") != "customrender")
  49. return;
  50. CustomRenderer.Begin();
  51. Draw();
  52. CustomRenderer.End();
  53. }
  54. float deltaTime = 0.0f;
  55. void HandleUpdate(uint eventType, ScriptVariantMap eventData)
  56. {
  57. float time = eventData.GetFloat("timestep");
  58. deltaTime += time;
  59. ElapsedTime += time;// / 2.0f;
  60. if (deltaTime < 1.0f / 60.0f)
  61. return;
  62. deltaTime = 0.0f;
  63. ShipInput.Update();
  64. if (!paused)
  65. {
  66. PlayerStatus.Update();
  67. EntityManager.Update();
  68. EnemySpawner.Update();
  69. ParticleManager.Update();
  70. Grid.Update();
  71. }
  72. }
  73. void Draw()
  74. {
  75. EntityManager.Draw();
  76. Grid.Draw();
  77. ParticleManager.Draw();
  78. }
  79. // GodMode by default as the game is really hard :)
  80. public static bool GodMode = true;
  81. public static Scene Scene { get; private set; }
  82. public static float ElapsedTime { get; private set; }
  83. public static Vector2 ScreenSize { get; private set; }
  84. public static IntRect ScreenBounds { get; private set; }
  85. public static Grid Grid { get; private set; }
  86. public static ParticleManager<ParticleState> ParticleManager { get; private set; }
  87. bool paused = false;
  88. }
  89. }