123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220 |
- #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
- /// <summary>
- /// Get/Set the postion value of the camera
- /// </summary>
- public Vector2 Position
- {
- set
- {
- if (positionValue != value)
- {
- cameraChanged = true;
- positionValue = value;
- }
- }
- get { return positionValue; }
- }
- /// <summary>
- /// Get/Set the rotation value of the camera
- /// </summary>
- public float Rotation
- {
- set
- {
- if (rotationValue != value)
- {
- cameraChanged = true;
- rotationValue = value;
- }
- }
- get { return rotationValue; }
- }
- /// <summary>
- /// Get/Set the zoom value of the camera
- /// </summary>
- public float Zoom
- {
- set
- {
- if (zoomValue != value)
- {
- cameraChanged = true;
- zoomValue = value;
- }
- }
- get { return zoomValue; }
- }
- /// <summary>
- /// Gets whether or not the camera has been changed since the last
- /// ResetChanged call
- /// </summary>
- public bool IsChanged
- {
- get { return cameraChanged; }
- }
- /// <summary>
- /// Set to TRUE to pan relative to the screen axis when
- /// the camera is rotated.
- /// </summary>
- public bool MoveUsingScreenAxis
- {
- set { isMovingUsingScreenAxis = value; }
- get { return isMovingUsingScreenAxis; }
- }
- #endregion
- #region Constructor
- /// <summary>
- /// Create a new Camera2D
- /// </summary>
- public Camera2D()
- {
- zoomValue = 1.0f;
- rotationValue = 0.0f;
- positionValue = Vector2.Zero;
- }
- #endregion
- #region Movement Methods
-
- /// <summary>
- /// Used to inform the camera that new values are updated by the application.
- /// </summary>
- public void ResetChanged()
- {
- cameraChanged = false;
- }
- /// <summary>
- /// Pan in the right direction. Corrects for rotation if specified.
- /// </summary>
- 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;
- }
- }
- }
- /// <summary>
- /// Pan in the left direction. Corrects for rotation if specified.
- /// </summary>
- 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;
- }
- }
- }
- /// <summary>
- /// Pan in the up direction. Corrects for rotation if specified.
- /// </summary>
- 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;
- }
- }
- }
- /// <summary>
- /// Pan in the down direction. Corrects for rotation if specified.
- /// </summary>
- 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
- }
- }
|