PageRenderTime 131ms CodeModel.GetById 50ms app.highlight 5ms RepoModel.GetById 55ms app.codeStats 0ms

/Rendering/Cameras/GameCamera.cs

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