Advertisement
Spectrewiz

Camera

Jul 5th, 2012
346
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.31 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.Audio;
  7. using Microsoft.Xna.Framework.Content;
  8. using Microsoft.Xna.Framework.GamerServices;
  9. using Microsoft.Xna.Framework.Graphics;
  10. using Microsoft.Xna.Framework.Input;
  11. using Microsoft.Xna.Framework.Media;
  12.  
  13. namespace ZombieShooter
  14. {
  15.     public class Camera
  16.     {
  17.         public Vector2 position;
  18.         private float rotation;
  19.         private float zoom;
  20.         public int maxZoom = 0;
  21.         public int minZoom = 4;
  22.         private Matrix m_transform;
  23.         public static float zoomAmount = 0.1f;
  24.  
  25.         public Camera()
  26.         {
  27.             position = new Vector2(Game.stage.Width / 2, Game.stage.Height / 2);
  28.             rotation = 0.0f;
  29.             zoom = 1.0f;
  30.  
  31.             float wRatio = (float)Game.stage.Width / (float)Game.screen.Width;
  32.             float hRatio = (float)Game.stage.Height / (float)Game.screen.Height;
  33.             bool lowesRatio = (wRatio < hRatio) ? true : false;
  34.  
  35.             float rLength = (lowesRatio) ? Game.stage.Width : Game.stage.Height;
  36.             float sLength = (lowesRatio) ? Game.screen.Width : Game.screen.Height;
  37.  
  38.             float tempZoom = 1.0f;
  39.             int c = 0;
  40.             while (sLength * tempZoom < rLength)
  41.             {
  42.                 tempZoom *= 1.0f + (zoomAmount * 2);
  43.                 c++;
  44.             }
  45.  
  46.  
  47.             maxZoom = c;
  48.             if (maxZoom > 9) maxZoom = 9;
  49.         }
  50.  
  51.         public void Update()
  52.         {
  53.             position = Player.player.position;
  54.         }
  55.  
  56.         public Vector2 Position
  57.         {
  58.             get { return position; }
  59.             set { position = value; }
  60.         }
  61.  
  62.         public float Rotation
  63.         {
  64.             get { return rotation; }
  65.             set { rotation = value; }
  66.         }
  67.  
  68.         public float Zoom
  69.         {
  70.             get { return zoom; }
  71.             set
  72.             {
  73.                 zoom = value;
  74.                 if (zoom < 1.0f - (zoomAmount * maxZoom)) zoom = 1.0f - (zoomAmount * maxZoom);
  75.                 if (zoom > 1.0f + (zoomAmount * minZoom)) zoom = 1.0f + (zoomAmount * minZoom);
  76.             }
  77.         }
  78.  
  79.         public void Move(Vector2 amount)
  80.         {
  81.             position += amount;
  82.         }
  83.  
  84.         public Matrix Transform(GraphicsDevice graphics)
  85.         {
  86.             float ViewPortWidth = graphics.Viewport.Width;
  87.             float ViewPortHeight = graphics.Viewport.Height;
  88.  
  89.             m_transform = Matrix.CreateTranslation(new Vector3(-position.X, -position.Y, 0)) *
  90.                 Matrix.CreateRotationZ(rotation) * Matrix.CreateScale(zoom) *
  91.                 Matrix.CreateTranslation(new Vector3(ViewPortWidth * 0.5f, ViewPortHeight * 0.5f, 0));
  92.  
  93.             return m_transform;
  94.         }
  95.  
  96.         public static Vector2 globalToLocal(Vector2 pos)
  97.         {
  98.             pos -= (Game.getCameraPosition() - new Vector2(Game.screen.Width /*(Game.screen is just a rectangle that fills up the screen)*/ / 2, Game.screen.Height / 2));
  99.             return pos;
  100.         }
  101.  
  102.         public static Vector2 localToGLobal(Vector2 pos)
  103.         {
  104.             pos += (Game.getCameraPosition() - new Vector2(Game.screen.Width / 2, Game.screen.Height / 2));
  105.             return pos;
  106.         }
  107.     }
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement