Game1.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. using Microsoft.Xna.Framework;
  2. using Microsoft.Xna.Framework.Graphics;
  3. using Microsoft.Xna.Framework.Input;
  4. namespace MonoGameSceneGraph
  5. {
  6. /// <summary>
  7. /// This is the main type for your game.
  8. /// </summary>
  9. public class Game1 : Game
  10. {
  11. // graphics manager
  12. GraphicsDeviceManager graphics;
  13. // sprite batch (for 2d drawings)
  14. SpriteBatch spriteBatch;
  15. // create test nodes and a model to draw
  16. Node root;
  17. Node node;
  18. Node nodeContainer;
  19. Node nodeContainerContainer;
  20. ModelEntity entity;
  21. /// <summary>
  22. /// Init game.
  23. /// </summary>
  24. public Game1()
  25. {
  26. graphics = new GraphicsDeviceManager(this);
  27. Content.RootDirectory = "Content";
  28. }
  29. /// <summary>
  30. /// Allows the game to perform any initialization it needs to before starting to run.
  31. /// This is where it can query for any required services and load any non-graphic
  32. /// related content. Calling base.Initialize will enumerate through any components
  33. /// and initialize them as well.
  34. /// </summary>
  35. protected override void Initialize()
  36. {
  37. // TODO: Add your initialization logic here
  38. base.Initialize();
  39. }
  40. /// <summary>
  41. /// LoadContent will be called once per game and is the place to load
  42. /// all of your content.
  43. /// </summary>
  44. protected override void LoadContent()
  45. {
  46. // Create a new SpriteBatch, which can be used to draw textures.
  47. spriteBatch = new SpriteBatch(GraphicsDevice);
  48. // create nodes
  49. root = new Node();
  50. nodeContainerContainer = new Node();
  51. nodeContainer = new Node();
  52. node = new Node();
  53. // arrange them in hirerchy
  54. root.AddChildNode(nodeContainerContainer);
  55. nodeContainerContainer.AddChildNode(nodeContainer);
  56. nodeContainer.AddChildNode(node);
  57. // for debug
  58. root.Identifier = "root";
  59. nodeContainerContainer.Identifier = "ncc";
  60. nodeContainer.Identifier = "nc";
  61. node.Identifier = "n";
  62. // create entity and attach to bottom node
  63. entity = new ModelEntity(Content.Load<Model>("robot"));
  64. node.AddEntity(entity);
  65. }
  66. /// <summary>
  67. /// UnloadContent will be called once per game and is the place to unload
  68. /// game-specific content.
  69. /// </summary>
  70. protected override void UnloadContent()
  71. {
  72. // TODO: Unload any non ContentManager content here
  73. }
  74. /// <summary>
  75. /// Allows the game to run logic such as updating the world,
  76. /// checking for collisions, gathering input, and playing audio.
  77. /// </summary>
  78. /// <param name="gameTime">Provides a snapshot of timing values.</param>
  79. protected override void Update(GameTime gameTime)
  80. {
  81. if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
  82. Exit();
  83. // TODO: Add your update logic here
  84. base.Update(gameTime);
  85. }
  86. /// <summary>
  87. /// This is called when the game should draw itself.
  88. /// </summary>
  89. /// <param name="gameTime">Provides a snapshot of timing values.</param>
  90. protected override void Draw(GameTime gameTime)
  91. {
  92. // clear screen
  93. GraphicsDevice.Clear(Color.CornflowerBlue);
  94. // draw scene
  95. root.Draw();
  96. // rotate inner node on X axis
  97. node.RotationZ = node.RotationZ + 0.01f;
  98. // scale node container
  99. nodeContainer.ScaleZ = (1.0f + (float)System.Math.Cos(gameTime.TotalGameTime.TotalSeconds * 2f) / 4f);
  100. // move top node left and right
  101. nodeContainerContainer.PositionX = (float)System.Math.Sin(gameTime.TotalGameTime.TotalSeconds) * 4f;
  102. // call base draw
  103. base.Draw(gameTime);
  104. }
  105. }
  106. }