#region File Description //----------------------------------------------------------------------------- // Camera2D.cs // // Microsoft XNA Community Game Platform // Copyright (C) Microsoft Corporation. All rights reserved. //----------------------------------------------------------------------------- #endregion #region Using Statements using System; using System.Collections.Generic; #if IPHONE using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Audio; using Microsoft.Xna.Framework.GamerServices; using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework.Input; using Microsoft.Xna.Framework.Storage; using Microsoft.Xna.Framework.Content; using Microsoft.Xna.Framework.Media; #else using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Audio; using Microsoft.Xna.Framework.GamerServices; using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework.Input; using Microsoft.Xna.Framework.Storage; using Microsoft.Xna.Framework.Content; using Microsoft.Xna.Framework.Media; #endif #endregion namespace TiledSprites { public class Camera2D { #region Fields private Vector2 positionValue; private bool isMovingUsingScreenAxis; private float rotationValue; private float zoomValue; private bool cameraChanged; #endregion #region Public Properties /// /// Get/Set the postion value of the camera /// public Vector2 Position { set { if (positionValue != value) { cameraChanged = true; positionValue = value; } } get { return positionValue; } } /// /// Get/Set the rotation value of the camera /// public float Rotation { set { if (rotationValue != value) { cameraChanged = true; rotationValue = value; } } get { return rotationValue; } } /// /// Get/Set the zoom value of the camera /// public float Zoom { set { if (zoomValue != value) { cameraChanged = true; zoomValue = value; } } get { return zoomValue; } } /// /// Gets whether or not the camera has been changed since the last /// ResetChanged call /// public bool IsChanged { get { return cameraChanged; } } /// /// Set to TRUE to pan relative to the screen axis when /// the camera is rotated. /// public bool MoveUsingScreenAxis { set { isMovingUsingScreenAxis = value; } get { return isMovingUsingScreenAxis; } } #endregion #region Constructor /// /// Create a new Camera2D /// public Camera2D() { zoomValue = 1.0f; rotationValue = 0.0f; positionValue = Vector2.Zero; } #endregion #region Movement Methods /// /// Used to inform the camera that new values are updated by the application. /// public void ResetChanged() { cameraChanged = false; } /// /// Pan in the right direction. Corrects for rotation if specified. /// public void MoveRight(ref float dist) { if (dist != 0) { cameraChanged = true; if (isMovingUsingScreenAxis) { positionValue.X += (float)Math.Cos(-rotationValue) * dist; positionValue.Y += (float)Math.Sin(-rotationValue) * dist; } else { positionValue.X += dist; } } } /// /// Pan in the left direction. Corrects for rotation if specified. /// public void MoveLeft(ref float dist) { if (dist != 0) { cameraChanged = true; if (isMovingUsingScreenAxis) { positionValue.X -= (float)Math.Cos(-rotationValue) * dist; positionValue.Y -= (float)Math.Sin(-rotationValue) * dist; } else { positionValue.X += dist; } } } /// /// Pan in the up direction. Corrects for rotation if specified. /// public void MoveUp(ref float dist) { if (dist != 0) { cameraChanged = true; if (isMovingUsingScreenAxis) { //using negative distance becuase //"up" actually decreases the y value positionValue.X -= (float)Math.Sin(rotationValue) * dist; positionValue.Y -= (float)Math.Cos(rotationValue) * dist; } else { positionValue.Y -= dist; } } } /// /// Pan in the down direction. Corrects for rotation if specified. /// public void MoveDown(ref float dist) { if (dist != 0) { cameraChanged = true; if (isMovingUsingScreenAxis) { //using negative distance becuase //"up" actually decreases the y value positionValue.X += (float)Math.Sin(rotationValue) * dist; positionValue.Y += (float)Math.Cos(rotationValue) * dist; } else { positionValue.Y -= dist; } } } #endregion } }