xemerau

Camera.cs

Jul 7th, 2012
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.05 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Microsoft.Xna.Framework;
  6. using Microsoft.Xna.Framework.Input;
  7. using Microsoft.Xna.Framework.Graphics;
  8.  
  9. namespace Sidescroller
  10. {
  11.     class Camera
  12.     {
  13.         #region Members
  14.         //The viewport that we want to use (to hold dimensions)
  15.         public Viewport View
  16.         {
  17.             get;
  18.             private set;
  19.         }
  20.  
  21.         //Where to position the camera (0x0 will be the center of the viewport
  22.         public Vector2 Position
  23.         {
  24.             get;
  25.             private set;
  26.         }
  27.  
  28.         //The zoom scalar (1.0f = 100% zoom level)
  29.         public float Zoom
  30.         {
  31.             get;
  32.             private set;
  33.         }
  34.  
  35.         //Camera's transform matrix
  36.         public Matrix Transform
  37.         {
  38.             get;
  39.             private set;
  40.         }
  41.  
  42.         //Center of the focus for the camera
  43.         public Vector2 FocusPoint
  44.         {
  45.             get;
  46.             private set;
  47.         }
  48.         #endregion
  49.  
  50.         #region Methods
  51.         //Constructor
  52.         public Camera(Viewport view, Vector2 position)
  53.         {
  54.             View = view;
  55.             Position = position;
  56.             Zoom = 1.0f;
  57.             FocusPoint = new Vector2(view.Width / 2, view.Height / 2);
  58.         }
  59.  
  60.         //Update Method
  61.         public void Update(KeyboardState keyboardState)
  62.         {
  63.             //Resets the camera, incase the map is moved off the viewport and becomes lost.
  64.             if (keyboardState.IsKeyDown(Keys.R))
  65.                 Reset();
  66.  
  67.             //Zooms the camera in and out with Z and X, respectivly
  68.             if (keyboardState.IsKeyDown(Keys.Z))
  69.                 Zoom += 0.002f;
  70.             if (keyboardState.IsKeyDown(Keys.X))
  71.                 Zoom -= 0.002f;
  72.  
  73.             //Moving the camera with WASD
  74.             Vector2 Movement = Vector2.Zero;
  75.             if (keyboardState.IsKeyDown(Keys.W))
  76.                 Movement.Y -= 5.0f / Zoom;
  77.             if (keyboardState.IsKeyDown(Keys.S))
  78.                 Movement.Y += 5.0f / Zoom;
  79.             if (keyboardState.IsKeyDown(Keys.A))
  80.                 Movement.X -= 5.0f / Zoom;
  81.             if (keyboardState.IsKeyDown(Keys.D))
  82.                 Movement.X += 5.0f / Zoom;
  83.  
  84.             //Apply the movement vector
  85.             Position = Vector2.Add(Position, Movement);
  86.  
  87.             /* Create a transform matrix through position, scale, rotation, and translation to the focus point
  88.              * We use Math.Pow on the zoom to speed up or slow down the zoom.  Both X and Y will have the same zoom levels
  89.              * so there will be no stretching.
  90.              */
  91.             Transform = Matrix.CreateScale(new Vector3((float)Math.Pow(Zoom, 10), (float)Math.Pow(Zoom, 10), 0)) *
  92.                 Matrix.CreateTranslation(new Vector3(FocusPoint.X, FocusPoint.Y, 0));
  93.  
  94.         }
  95.  
  96.         //Reset Method
  97.         private void Reset()
  98.         {
  99.             Position = Vector2.Zero;
  100.             Zoom = 1.0f;
  101.         }
  102.         #endregion
  103.     }
  104. }
Advertisement
Add Comment
Please, Sign In to add comment