/Rendering/Cameras/GameCamera.cs
# · C# · 188 lines · 86 code · 17 blank · 85 comment · 14 complexity · 1c14a7055cbb012e5e31679354a91941 MD5 · raw file
- using Delta.InputSystem;
- using Delta.Utilities.Datatypes;
- using Delta.Utilities.Helpers;
-
- namespace Delta.Rendering.Cameras
- {
- /// <summary>
- /// Game camera. This is our base camera used for games.
- /// Currently it is a combination of PathCamera and FreeCamera with
- /// the ability to switch between them.
- /// </summary>
- public class GameCamera : PathCamera
- {
- #region InLevelCameraMode (Public)
- /// <summary>
- /// In level camera mode
- /// </summary>
- public bool InLevelCameraMode
- {
- get
- {
- return cameraMode == 0;
- }
- }
- #endregion
-
- #region Private
-
- #region freeCamera (Private)
- /// <summary>
- /// Free camera we switch into
- /// </summary>
- private readonly FreeCamera freeCamera;
- #endregion
-
- #region cameraMode (Private)
- /// <summary>
- /// Camera mode, 0 = level camera, 1 free camera, 2 = waiting, 3 = return
- /// to level camera (need extra mode because we have no touch release)
- /// </summary>
- private int cameraMode;
- #endregion
-
- #endregion
-
- #region Constructors
- /// <summary>
- /// Create new GameCamera
- /// </summary>
- /// <param name="setPath">Matrix array containing path data.</param>
- public GameCamera(Matrix[] setPath)
- : base(setPath)
- {
- freeCamera = new FreeCamera(Vector.Zero);
- cameraMode = 0;
- AlwaysNeedsUpdate = true;
- // Activate this GameCamera (else freeCamera would be active)
- Activate();
- }
- #endregion
-
- #region Methods (Private)
-
- #region InternalRun
- /// <summary>
- /// Update the camera.
- /// </summary>
- protected override void InternalRun()
- {
- // Check if we really are active, else do nothing
- if (IsActive == false &&
- freeCamera.IsActive == false)
- {
- return;
- }
-
- // Always update the path camera for the camera we might want to
- // look at. Also important for the shadow casting!
- // Note: Since this is not necessarily the active camera, it will
- // not always affect the view (only if it is active)
- base.InternalRun();
-
- // Skip Input if the camera is locked!
- if (IsLocked)
- {
- return;
- }
-
- if (cameraMode == 0)
- {
- // Allow to click to activate free camera
- if (Input.Keyboard.SpaceIsPressed ||
- Input.Mouse.LeftButtonIsPressed ||
- Input.Touch.TouchIsPressed)
- {
- if (freeCamera.Activate())
- {
- // Switch to free camera mode
- cameraMode = 1;
- freeCamera.IsStopMoving = false;
-
- freeCamera.Position = Position;
- freeCamera.Target = Target;
-
- freeCamera.Yaw = 0.0f;
- //-MathHelper.Atan(-lookDirection.X, -lookDirection.Y);
- freeCamera.Pitch = -MathHelper.Asin(-lookDirection.Z);
- freeCamera.Roll = 90.0f -
- MathHelper.Atan(lookDirection.Y, lookDirection.X);
- //180-MathHelper.Atan(freeCamera.LookDirection.X,
- //freeCamera.LookDirection.Y);
- //90 - MathHelper.Atan(-freeCamera.LookDirection.Y,
- //-freeCamera.LookDirection.X);
-
- // does not matter, will be updated anyway
- freeCamera.LookDirection = new Vector(0, 0, 1);
- /*
- freeCamera.Pitch = 0;
- freeCamera.Roll = 0;
- freeCamera.Yaw = 0;//old:180;
- /*not needed anymore
- // Position.Z = sin(Rotation.X) -> see trigonometrical functions
- // Note: we consider at the moment only the x rotation, because we don't
- // have any rotation on y or z (with default values)
- //freeCamera.Roll = MathHelper.Asin(freeCamera.LookDirection.Z);
- freeCamera.Roll =
- //180-MathHelper.Atan(freeCamera.LookDirection.X,
- //freeCamera.LookDirection.Y);
- 90 - MathHelper.Atan(-freeCamera.LookDirection.Y,
- -freeCamera.LookDirection.X);
- */
- } // if
- } // if
- } // if
- else //obs: if (cameraMode == 1)
- {
- // Wait for touch release and switch to stop camera mode
- if (Input.Keyboard.SpaceIsPressed == false &&
- Input.Mouse.LeftButtonIsPressed == false &&
- Input.Touch.TouchIsPressed == false)
- {
- /*obs, skip all this, just return to the level camera
- // Stop the camera and wait for the next click
- FreeCamera.stopMoving = true;
- cameraMode = 2;
- }
- } // else if
- // stop camera mode, wait for click to return to level camera!
- else if (cameraMode == 2)
- {
- if (Input.Keyboard.SpaceIsPressed ||
- Input.Mouse.LeftButtonIsPressed ||
- Input.Touch.TouchIsPressed)
- {
- cameraMode = 3;
- }
- } // else if
- else // wait for touch to be released, then go back to level camera
- {
- // Wait for touch release and switch to stop camera mode
- if (Input.Keyboard.SpaceIsPressed == false &&
- Input.Mouse.LeftButtonIsPressed == false &&
- Input.Touch.TouchIsPressed == false)
- {
- */
- // Go back to the level camera
- Activate();
- cameraMode = 0;
-
- // And also restore the light direction
- // Setup light the same way as in SoulcraftTechDemo.cs
- Light.Position = Light.SoulcraftTechDemoLightDirection;
- Light.Direction = Light.Position;
- Light.Direction.Normalize();
- } // if
- } // else
-
- //// Only Update pathCamera, if it is actually current
- //if (cameraMode == 0)
- //{
- // base.InternalRun();
- //}
- }
- #endregion
-
- #endregion
- }
- }