GameCamera.cs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. #region File Description
  2. //-----------------------------------------------------------------------------
  3. // GameCamera.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 System.Collections.Generic;
  12. using System.Text;
  13. using Microsoft.Xna.Framework;
  14. using Microsoft.Xna.Framework.Content;
  15. using RobotGameData.Helper;
  16. #endregion
  17. namespace RobotGameData.Camera
  18. {
  19. /// <summary>
  20. /// It's a tremble camera
  21. /// </summary>
  22. public class GameCamera : CameraBase
  23. {
  24. #region Fields
  25. /// <summary>
  26. /// The Camera's trembling amount
  27. /// </summary>
  28. Vector3 trembleOffset = Vector3.Zero;
  29. float trembleCircle = 0.0f;
  30. float trembleTime = 0.0f;
  31. float trembleAccTime = 0.0f;
  32. #endregion
  33. #region Properties
  34. public Vector3 TrembleOffset
  35. {
  36. get { return this.trembleOffset; }
  37. }
  38. #endregion
  39. /// <summary>
  40. /// Constructor.
  41. /// </summary>
  42. public GameCamera()
  43. : base()
  44. {
  45. trembleOffset = Vector3.Zero;
  46. trembleCircle = 0.0f;
  47. trembleTime = 0.0f;
  48. trembleAccTime = 0.0f;
  49. }
  50. /// <summary>
  51. /// Update the tremble time and position
  52. /// </summary>
  53. protected override void OnUpdate(GameTime gameTime)
  54. {
  55. // Calculates trembling time
  56. if (trembleAccTime < trembleTime)
  57. {
  58. trembleAccTime += (float)gameTime.ElapsedGameTime.TotalSeconds;
  59. // Set to randomize trembling position
  60. trembleOffset.X = HelperMath.RandomNormal() * HelperMath.RandomNormal2()
  61. * trembleCircle;
  62. trembleOffset.Y = HelperMath.RandomNormal() * HelperMath.RandomNormal2()
  63. * trembleCircle;
  64. trembleOffset.Z = 0.0f;
  65. }
  66. // Stop trembling time
  67. else if (trembleAccTime >= trembleTime)
  68. {
  69. trembleAccTime = 0.0f;
  70. trembleTime = 0.0f;
  71. }
  72. base.OnUpdate(gameTime);
  73. }
  74. /// <summary>
  75. /// Tremble the camera
  76. /// </summary>
  77. /// <param name="rTime">tremble time</param>
  78. /// <param name="rCircle">tremble amount</param>
  79. public void SetTremble(float rTime, float rCircle)
  80. {
  81. trembleAccTime = 0.0f;
  82. trembleTime = rTime;
  83. trembleCircle = rCircle;
  84. }
  85. }
  86. }