SimpleAnimation.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. #region File Description
  2. //-----------------------------------------------------------------------------
  3. // SimpleAnimation.cs
  4. //
  5. // Microsoft XNA Community Game Platform
  6. // Copyright (C) Microsoft Corporation. All rights reserved.
  7. //-----------------------------------------------------------------------------
  8. #endregion
  9. #region Using Statements
  10. using System;
  11. using Microsoft.Xna.Framework;
  12. using Microsoft.Xna.Framework.Content;
  13. using Microsoft.Xna.Framework.Graphics;
  14. using Microsoft.Xna.Framework.Input;
  15. #endregion
  16. namespace SimpleAnimation
  17. {
  18. /// <summary>
  19. /// Sample showing how to apply simple animation to a rigid body tank model.
  20. /// </summary>
  21. public class SimpleAnimationGame : Microsoft.Xna.Framework.Game
  22. {
  23. #region Fields
  24. GraphicsDeviceManager graphics;
  25. Tank tank;
  26. #endregion
  27. #region Initialization
  28. public SimpleAnimationGame()
  29. {
  30. graphics = new GraphicsDeviceManager(this);
  31. Content.RootDirectory = "Content";
  32. #if WINDOWS_PHONE
  33. // Frame rate is 30 fps by default for Windows Phone.
  34. TargetElapsedTime = TimeSpan.FromTicks(333333);
  35. graphics.IsFullScreen = true;
  36. #endif
  37. tank = new Tank();
  38. }
  39. /// <summary>
  40. /// Load your graphics content.
  41. /// </summary>
  42. protected override void LoadContent()
  43. {
  44. tank.Load(Content);
  45. }
  46. #endregion
  47. #region Update and Draw
  48. /// <summary>
  49. /// Allows the game to run logic.
  50. /// </summary>
  51. protected override void Update(GameTime gameTime)
  52. {
  53. HandleInput();
  54. float time = (float)gameTime.TotalGameTime.TotalSeconds;
  55. // Update the animation properties on the tank object. In a real game
  56. // you would probably take this data from user inputs or the physics
  57. // system, rather than just making everything rotate like this!
  58. tank.WheelRotation = time * 5;
  59. tank.SteerRotation = (float)Math.Sin(time * 0.75f) * 0.5f;
  60. tank.TurretRotation = (float)Math.Sin(time * 0.333f) * 1.25f;
  61. tank.CannonRotation = (float)Math.Sin(time * 0.25f) * 0.333f - 0.333f;
  62. tank.HatchRotation = MathHelper.Clamp((float)Math.Sin(time * 2) * 2, -1, 0);
  63. base.Update(gameTime);
  64. }
  65. /// <summary>
  66. /// This is called when the game should draw itself.
  67. /// </summary>
  68. protected override void Draw(GameTime gameTime)
  69. {
  70. GraphicsDevice device = graphics.GraphicsDevice;
  71. device.Clear(Color.DarkGray);
  72. // Calculate the camera matrices.
  73. float time = (float)gameTime.TotalGameTime.TotalSeconds;
  74. Matrix rotation = Matrix.CreateRotationY(time * 0.1f);
  75. Matrix view = Matrix.CreateLookAt(new Vector3(1000, 500, 0),
  76. new Vector3(0, 150, 0),
  77. Vector3.Up);
  78. Matrix projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver4,
  79. device.Viewport.AspectRatio,
  80. 10,
  81. 10000);
  82. // Draw the tank model.
  83. tank.Draw(rotation, view, projection);
  84. base.Draw(gameTime);
  85. }
  86. #endregion
  87. #region Handle Input
  88. /// <summary>
  89. /// Handles input for quitting the game.
  90. /// </summary>
  91. private void HandleInput()
  92. {
  93. KeyboardState currentKeyboardState = Keyboard.GetState();
  94. GamePadState currentGamePadState = GamePad.GetState(PlayerIndex.One);
  95. // Check for exit.
  96. if (currentKeyboardState.IsKeyDown(Keys.Escape) ||
  97. currentGamePadState.Buttons.Back == ButtonState.Pressed)
  98. {
  99. Exit();
  100. }
  101. }
  102. #endregion
  103. }
  104. #region Entry Point
  105. /// <summary>
  106. /// The main entry point for the application.
  107. /// </summary>
  108. static class Program
  109. {
  110. static void Main()
  111. {
  112. using (SimpleAnimationGame game = new SimpleAnimationGame())
  113. {
  114. game.Run();
  115. }
  116. }
  117. }
  118. #endregion
  119. }