Game1.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. // create entity and attach to bottom node
  58. entity = new ModelEntity(Content.Load<Model>("robot"));
  59. node.AddEntity(entity);
  60. }
  61. /// <summary>
  62. /// UnloadContent will be called once per game and is the place to unload
  63. /// game-specific content.
  64. /// </summary>
  65. protected override void UnloadContent()
  66. {
  67. // TODO: Unload any non ContentManager content here
  68. }
  69. /// <summary>
  70. /// Allows the game to run logic such as updating the world,
  71. /// checking for collisions, gathering input, and playing audio.
  72. /// </summary>
  73. /// <param name="gameTime">Provides a snapshot of timing values.</param>
  74. protected override void Update(GameTime gameTime)
  75. {
  76. if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
  77. Exit();
  78. // TODO: Add your update logic here
  79. base.Update(gameTime);
  80. }
  81. /// <summary>
  82. /// This is called when the game should draw itself.
  83. /// </summary>
  84. /// <param name="gameTime">Provides a snapshot of timing values.</param>
  85. protected override void Draw(GameTime gameTime)
  86. {
  87. // clear screen
  88. GraphicsDevice.Clear(Color.CornflowerBlue);
  89. // draw scene
  90. root.Draw();
  91. // rotate inner node on X axis
  92. node.RotationZ = node.RotationZ + 0.01f;
  93. // scale node container
  94. nodeContainer.ScaleZ = (1.0f + (float)System.Math.Cos(gameTime.TotalGameTime.TotalSeconds * 2f) / 4f);
  95. // move top node left and right
  96. nodeContainerContainer.PositionX = (float)System.Math.Sin(gameTime.TotalGameTime.TotalSeconds) * 4f;
  97. // call base draw
  98. base.Draw(gameTime);
  99. }
  100. }
  101. }