/ShEngineSamples/Scenes/DefferedScene.cs

https://bitbucket.org/shishov/shengine · C# · 116 lines · 89 code · 22 blank · 5 comment · 2 complexity · bac6216186d1092f0628d48cb48fcc71 MD5 · raw file

  1. #region
  2. using System;
  3. using Microsoft.Xna.Framework;
  4. using Microsoft.Xna.Framework.Graphics;
  5. using Microsoft.Xna.Framework.Input;
  6. using NetComponentEntitySystem;
  7. using ShEngine;
  8. using ShEngine.Common;
  9. using ShEngine.EntitySystem;
  10. using ShEngine.EntitySystem.Components.Volume;
  11. using ShEngine.EntitySystem.Scripts;
  12. using ShEngine.EntitySystem.Systems;
  13. using ShEngineSamples.Components;
  14. using ShEngineSamples.Systems;
  15. using DirectionalLight = ShEngine.EntitySystem.Components.Volume.DirectionalLight;
  16. #endregion
  17. namespace ShEngineSamples.Scenes
  18. {
  19. internal class DefferedScene : Scene
  20. {
  21. private EntityWorld _world;
  22. public DefferedScene(ShGame game, string name)
  23. : base(game, name)
  24. {
  25. }
  26. public override void Initialize()
  27. {
  28. _world = new EntityWorld();
  29. var graphicsManager = GameServices.GetService<GraphicsDeviceManager>();
  30. graphicsManager.PreferredBackBufferWidth = 1600;
  31. graphicsManager.PreferredBackBufferHeight = 900;
  32. graphicsManager.SynchronizeWithVerticalRetrace = false;
  33. graphicsManager.ApplyChanges();
  34. Game.IsFixedTimeStep = false;
  35. var camera = new Entity(_world);
  36. camera.AddComponent<Transform>();
  37. camera.AddComponent(new Camera());
  38. camera.AddScript(new FreeCamera(new Vector3(10, 10, 10), Vector3.Zero));
  39. _world.AddEntity(camera);
  40. var graphics = GameServices.GetService<GraphicsDevice>();
  41. var light = new Entity(_world);
  42. light.AddComponent(new Transform(new Vector3(-8, 10, 0)));
  43. light.AddComponent(new PointLight(graphics, 500, Color.Orange.ToVector4(), 0.5f, true, 256));
  44. light.AddComponent(new Rotatable(0.004f));
  45. _world.AddEntity(light);
  46. var light1 = new Entity(_world);
  47. light1.AddComponent(new Transform(new Vector3(-20, 100, 0)));
  48. light1.AddComponent(new PointLight(graphics, 100, Color.Blue.ToVector4(), 1f, true, 256));
  49. light1.AddComponent(new Rotatable(0.008f));
  50. //_world.AddEntity(light1);
  51. var sun = new Entity(_world);
  52. sun.AddComponent(new DirectionalLight(new Vector3(-1, -1, 0), Color.Orange, 0.5f));
  53. _world.AddEntity(sun);
  54. //var flashlight = new Entity(_world);
  55. var t = new Transform(camera.GetComponent<Transform>())
  56. {
  57. Local = Matrix.CreateScale(1f/10f) * Matrix.CreateTranslation(new Vector3(0.5f, -0.5f, -1.5f))
  58. };
  59. //flashlight.AddComponent(t);
  60. //flashlight.AddComponent(new ModelComponent(AssetManager.GetXna<Model>("Models/flashlight")));
  61. //_world.AddEntity(flashlight);
  62. var cookie = AssetManager.GetXna<Texture2D>("Deffered/DefaultCookie");
  63. var light2 = new Entity(_world);
  64. var transform = new Transform(t)
  65. {
  66. Local = Matrix.CreateScale(100) * Matrix.CreateTranslation(new Vector3(0, 0, -2f))
  67. };
  68. light2.AddComponent(transform);
  69. light2.AddComponent(new SpotLight(graphics, Color.White.ToVector4(), 0.5f, true, 256, cookie, MathHelper.PiOver4));
  70. _world.AddEntity(light2);
  71. _world.AddSystem(new DefferedRenderer(), ExecutionMode.Synchronous, GameLoopType.DrawLoop);
  72. _world.AddSystem(new RotationSystem(), ExecutionMode.Synchronous, GameLoopType.UpdateLoop);
  73. var scene = new Entity(_world);
  74. var sceneModel = AssetManager.GetXna<Model>("Models/barrel");
  75. scene.AddComponent(new ModelComponent(sceneModel));
  76. scene.AddComponent(new Transform());
  77. _world.AddEntity(scene);
  78. GameServices.GetService<ShGame>().Disposed += _world.Destroy;
  79. }
  80. public override void Draw(GameTime time)
  81. {
  82. _world.Draw(time.ElapsedGameTime.Milliseconds);
  83. }
  84. public override void Update(GameTime time)
  85. {
  86. Mouse.SetPosition(0, 0);
  87. if (Input.IsNewKeyPress(Keys.Escape))
  88. Game.Exit();
  89. if (Input.IsNewKeyPress(Keys.F))
  90. GameServices.GetService<GraphicsDeviceManager>().ToggleFullScreen();
  91. _world.Update(time.ElapsedGameTime.Milliseconds);
  92. }
  93. }
  94. }