PageRenderTime 44ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 1ms

/Rendering/Cameras/GameCamera.cs

#
C# | 188 lines | 86 code | 17 blank | 85 comment | 14 complexity | 1c14a7055cbb012e5e31679354a91941 MD5 | raw file
Possible License(s): Apache-2.0
  1. using Delta.InputSystem;
  2. using Delta.Utilities.Datatypes;
  3. using Delta.Utilities.Helpers;
  4. namespace Delta.Rendering.Cameras
  5. {
  6. /// <summary>
  7. /// Game camera. This is our base camera used for games.
  8. /// Currently it is a combination of PathCamera and FreeCamera with
  9. /// the ability to switch between them.
  10. /// </summary>
  11. public class GameCamera : PathCamera
  12. {
  13. #region InLevelCameraMode (Public)
  14. /// <summary>
  15. /// In level camera mode
  16. /// </summary>
  17. public bool InLevelCameraMode
  18. {
  19. get
  20. {
  21. return cameraMode == 0;
  22. }
  23. }
  24. #endregion
  25. #region Private
  26. #region freeCamera (Private)
  27. /// <summary>
  28. /// Free camera we switch into
  29. /// </summary>
  30. private readonly FreeCamera freeCamera;
  31. #endregion
  32. #region cameraMode (Private)
  33. /// <summary>
  34. /// Camera mode, 0 = level camera, 1 free camera, 2 = waiting, 3 = return
  35. /// to level camera (need extra mode because we have no touch release)
  36. /// </summary>
  37. private int cameraMode;
  38. #endregion
  39. #endregion
  40. #region Constructors
  41. /// <summary>
  42. /// Create new GameCamera
  43. /// </summary>
  44. /// <param name="setPath">Matrix array containing path data.</param>
  45. public GameCamera(Matrix[] setPath)
  46. : base(setPath)
  47. {
  48. freeCamera = new FreeCamera(Vector.Zero);
  49. cameraMode = 0;
  50. AlwaysNeedsUpdate = true;
  51. // Activate this GameCamera (else freeCamera would be active)
  52. Activate();
  53. }
  54. #endregion
  55. #region Methods (Private)
  56. #region InternalRun
  57. /// <summary>
  58. /// Update the camera.
  59. /// </summary>
  60. protected override void InternalRun()
  61. {
  62. // Check if we really are active, else do nothing
  63. if (IsActive == false &&
  64. freeCamera.IsActive == false)
  65. {
  66. return;
  67. }
  68. // Always update the path camera for the camera we might want to
  69. // look at. Also important for the shadow casting!
  70. // Note: Since this is not necessarily the active camera, it will
  71. // not always affect the view (only if it is active)
  72. base.InternalRun();
  73. // Skip Input if the camera is locked!
  74. if (IsLocked)
  75. {
  76. return;
  77. }
  78. if (cameraMode == 0)
  79. {
  80. // Allow to click to activate free camera
  81. if (Input.Keyboard.SpaceIsPressed ||
  82. Input.Mouse.LeftButtonIsPressed ||
  83. Input.Touch.TouchIsPressed)
  84. {
  85. if (freeCamera.Activate())
  86. {
  87. // Switch to free camera mode
  88. cameraMode = 1;
  89. freeCamera.IsStopMoving = false;
  90. freeCamera.Position = Position;
  91. freeCamera.Target = Target;
  92. freeCamera.Yaw = 0.0f;
  93. //-MathHelper.Atan(-lookDirection.X, -lookDirection.Y);
  94. freeCamera.Pitch = -MathHelper.Asin(-lookDirection.Z);
  95. freeCamera.Roll = 90.0f -
  96. MathHelper.Atan(lookDirection.Y, lookDirection.X);
  97. //180-MathHelper.Atan(freeCamera.LookDirection.X,
  98. //freeCamera.LookDirection.Y);
  99. //90 - MathHelper.Atan(-freeCamera.LookDirection.Y,
  100. //-freeCamera.LookDirection.X);
  101. // does not matter, will be updated anyway
  102. freeCamera.LookDirection = new Vector(0, 0, 1);
  103. /*
  104. freeCamera.Pitch = 0;
  105. freeCamera.Roll = 0;
  106. freeCamera.Yaw = 0;//old:180;
  107. /*not needed anymore
  108. // Position.Z = sin(Rotation.X) -> see trigonometrical functions
  109. // Note: we consider at the moment only the x rotation, because we don't
  110. // have any rotation on y or z (with default values)
  111. //freeCamera.Roll = MathHelper.Asin(freeCamera.LookDirection.Z);
  112. freeCamera.Roll =
  113. //180-MathHelper.Atan(freeCamera.LookDirection.X,
  114. //freeCamera.LookDirection.Y);
  115. 90 - MathHelper.Atan(-freeCamera.LookDirection.Y,
  116. -freeCamera.LookDirection.X);
  117. */
  118. } // if
  119. } // if
  120. } // if
  121. else //obs: if (cameraMode == 1)
  122. {
  123. // Wait for touch release and switch to stop camera mode
  124. if (Input.Keyboard.SpaceIsPressed == false &&
  125. Input.Mouse.LeftButtonIsPressed == false &&
  126. Input.Touch.TouchIsPressed == false)
  127. {
  128. /*obs, skip all this, just return to the level camera
  129. // Stop the camera and wait for the next click
  130. FreeCamera.stopMoving = true;
  131. cameraMode = 2;
  132. }
  133. } // else if
  134. // stop camera mode, wait for click to return to level camera!
  135. else if (cameraMode == 2)
  136. {
  137. if (Input.Keyboard.SpaceIsPressed ||
  138. Input.Mouse.LeftButtonIsPressed ||
  139. Input.Touch.TouchIsPressed)
  140. {
  141. cameraMode = 3;
  142. }
  143. } // else if
  144. else // wait for touch to be released, then go back to level camera
  145. {
  146. // Wait for touch release and switch to stop camera mode
  147. if (Input.Keyboard.SpaceIsPressed == false &&
  148. Input.Mouse.LeftButtonIsPressed == false &&
  149. Input.Touch.TouchIsPressed == false)
  150. {
  151. */
  152. // Go back to the level camera
  153. Activate();
  154. cameraMode = 0;
  155. // And also restore the light direction
  156. // Setup light the same way as in SoulcraftTechDemo.cs
  157. Light.Position = Light.SoulcraftTechDemoLightDirection;
  158. Light.Direction = Light.Position;
  159. Light.Direction.Normalize();
  160. } // if
  161. } // else
  162. //// Only Update pathCamera, if it is actually current
  163. //if (cameraMode == 0)
  164. //{
  165. // base.InternalRun();
  166. //}
  167. }
  168. #endregion
  169. #endregion
  170. }
  171. }