Camera2D.cs 5.6 KB

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