Camera2D.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. #region File Description
  2. //-----------------------------------------------------------------------------
  3. // Camera2D.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. #if IPHONE
  13. using Microsoft.Xna.Framework;
  14. using Microsoft.Xna.Framework.Audio;
  15. using Microsoft.Xna.Framework.GamerServices;
  16. using Microsoft.Xna.Framework.Graphics;
  17. using Microsoft.Xna.Framework.Input;
  18. using Microsoft.Xna.Framework.Storage;
  19. using Microsoft.Xna.Framework.Content;
  20. using Microsoft.Xna.Framework.Media;
  21. #else
  22. using Microsoft.Xna.Framework;
  23. using Microsoft.Xna.Framework.Audio;
  24. using Microsoft.Xna.Framework.GamerServices;
  25. using Microsoft.Xna.Framework.Graphics;
  26. using Microsoft.Xna.Framework.Input;
  27. using Microsoft.Xna.Framework.Storage;
  28. using Microsoft.Xna.Framework.Content;
  29. using Microsoft.Xna.Framework.Media;
  30. #endif
  31. #endregion
  32. namespace TiledSprites
  33. {
  34. public class Camera2D
  35. {
  36. #region Fields
  37. private Vector2 positionValue;
  38. private bool isMovingUsingScreenAxis;
  39. private float rotationValue;
  40. private float zoomValue;
  41. private bool cameraChanged;
  42. #endregion
  43. #region Public Properties
  44. /// <summary>
  45. /// Get/Set the postion value of the camera
  46. /// </summary>
  47. public Vector2 Position
  48. {
  49. set
  50. {
  51. if (positionValue != value)
  52. {
  53. cameraChanged = true;
  54. positionValue = value;
  55. }
  56. }
  57. get { return positionValue; }
  58. }
  59. /// <summary>
  60. /// Get/Set the rotation value of the camera
  61. /// </summary>
  62. public float Rotation
  63. {
  64. set
  65. {
  66. if (rotationValue != value)
  67. {
  68. cameraChanged = true;
  69. rotationValue = value;
  70. }
  71. }
  72. get { return rotationValue; }
  73. }
  74. /// <summary>
  75. /// Get/Set the zoom value of the camera
  76. /// </summary>
  77. public float Zoom
  78. {
  79. set
  80. {
  81. if (zoomValue != value)
  82. {
  83. cameraChanged = true;
  84. zoomValue = value;
  85. }
  86. }
  87. get { return zoomValue; }
  88. }
  89. /// <summary>
  90. /// Gets whether or not the camera has been changed since the last
  91. /// ResetChanged call
  92. /// </summary>
  93. public bool IsChanged
  94. {
  95. get { return cameraChanged; }
  96. }
  97. /// <summary>
  98. /// Set to TRUE to pan relative to the screen axis when
  99. /// the camera is rotated.
  100. /// </summary>
  101. public bool MoveUsingScreenAxis
  102. {
  103. set { isMovingUsingScreenAxis = value; }
  104. get { return isMovingUsingScreenAxis; }
  105. }
  106. #endregion
  107. #region Constructor
  108. /// <summary>
  109. /// Create a new Camera2D
  110. /// </summary>
  111. public Camera2D()
  112. {
  113. zoomValue = 1.0f;
  114. rotationValue = 0.0f;
  115. positionValue = Vector2.Zero;
  116. }
  117. #endregion
  118. #region Movement Methods
  119. /// <summary>
  120. /// Used to inform the camera that new values are updated by the application.
  121. /// </summary>
  122. public void ResetChanged()
  123. {
  124. cameraChanged = false;
  125. }
  126. /// <summary>
  127. /// Pan in the right direction. Corrects for rotation if specified.
  128. /// </summary>
  129. public void MoveRight(ref float dist)
  130. {
  131. if (dist != 0)
  132. {
  133. cameraChanged = true;
  134. if (isMovingUsingScreenAxis)
  135. {
  136. positionValue.X += (float)Math.Cos(-rotationValue) * dist;
  137. positionValue.Y += (float)Math.Sin(-rotationValue) * dist;
  138. }
  139. else
  140. {
  141. positionValue.X += dist;
  142. }
  143. }
  144. }
  145. /// <summary>
  146. /// Pan in the left direction. Corrects for rotation if specified.
  147. /// </summary>
  148. public void MoveLeft(ref float dist)
  149. {
  150. if (dist != 0)
  151. {
  152. cameraChanged = true;
  153. if (isMovingUsingScreenAxis)
  154. {
  155. positionValue.X -= (float)Math.Cos(-rotationValue) * dist;
  156. positionValue.Y -= (float)Math.Sin(-rotationValue) * dist;
  157. }
  158. else
  159. {
  160. positionValue.X += dist;
  161. }
  162. }
  163. }
  164. /// <summary>
  165. /// Pan in the up direction. Corrects for rotation if specified.
  166. /// </summary>
  167. public void MoveUp(ref float dist)
  168. {
  169. if (dist != 0)
  170. {
  171. cameraChanged = true;
  172. if (isMovingUsingScreenAxis)
  173. {
  174. //using negative distance becuase
  175. //"up" actually decreases the y value
  176. positionValue.X -= (float)Math.Sin(rotationValue) * dist;
  177. positionValue.Y -= (float)Math.Cos(rotationValue) * dist;
  178. }
  179. else
  180. {
  181. positionValue.Y -= dist;
  182. }
  183. }
  184. }
  185. /// <summary>
  186. /// Pan in the down direction. Corrects for rotation if specified.
  187. /// </summary>
  188. public void MoveDown(ref float dist)
  189. {
  190. if (dist != 0)
  191. {
  192. cameraChanged = true;
  193. if (isMovingUsingScreenAxis)
  194. {
  195. //using negative distance becuase
  196. //"up" actually decreases the y value
  197. positionValue.X += (float)Math.Sin(rotationValue) * dist;
  198. positionValue.Y += (float)Math.Cos(rotationValue) * dist;
  199. }
  200. else
  201. {
  202. positionValue.Y -= dist;
  203. }
  204. }
  205. }
  206. #endregion
  207. }
  208. }