Guest User

Untitled

a guest
May 15th, 2015
229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.09 KB | None | 0 0
  1. public class Camera
  2. {
  3.     public float MaxZoom { get { return zoomUpperLimit; } }
  4.     public float MinZoom { get { return zoomLowerLimit; } }
  5.  
  6.     private const float zoomUpperLimit = 1.5f;
  7.     private const float zoomLowerLimit = 0.1f;
  8.  
  9.     private float _zoom;
  10.     private Vector2 _pos;
  11.     private float _rotation;
  12.        
  13.     private Vector2 Origin;
  14.     public int ViewportWidth, ViewportHeight;
  15.  
  16.     private bool shaking;
  17.     private float shakeMagnitude;
  18.     private float shakeDuration;
  19.     private float shakeTimer;
  20.     private Vector3 shakeOffset;
  21.  
  22.     #region Properties
  23.  
  24.     public float Zoom
  25.     {
  26.         get { return _zoom; }
  27.         set
  28.         {
  29.             _zoom = value;
  30.             if (_zoom < zoomLowerLimit)
  31.                 _zoom = zoomLowerLimit;
  32.             if (_zoom > zoomUpperLimit)
  33.                 _zoom = zoomUpperLimit;
  34.         }
  35.     }
  36.  
  37.     public Rectangle Rectangle
  38.     {
  39.         get
  40.         {
  41.             int width = (int)((ViewportWidth / _zoom));
  42.             int height = (int)((ViewportHeight / _zoom));
  43.             return new Rectangle((int)(_pos.X - width / 2), (int)(_pos.Y - height / 2), width, height);
  44.         }
  45.     }
  46.  
  47.     public float Rotation
  48.     {
  49.         get { return _rotation; }
  50.         set { _rotation = value; }
  51.     }
  52.  
  53.     public void Move(Vector2 amount)
  54.     {
  55.         Position += amount;
  56.     }
  57.  
  58.     public Vector2 Position
  59.     {
  60.         get { return _pos; }
  61.         set { _pos = value; }
  62.     }
  63.  
  64.  
  65.     public Matrix GetViewMatrix(Vector2 parallax)
  66.     {
  67.         return Matrix.CreateTranslation(new Vector3(-Position * parallax, 0.0f)) *
  68.             Matrix.CreateTranslation(new Vector3(-Origin, 0.0f)) *
  69.             Matrix.CreateRotationZ(Rotation) *
  70.             Matrix.CreateScale(Zoom, Zoom, 1) *
  71.             Matrix.CreateTranslation(new Vector3(Origin, 0.0f));
  72.     }
  73.        
  74.     public Matrix View
  75.     {
  76.         get
  77.         {
  78.  
  79.             return Matrix.CreateTranslation(new Vector3(-_pos.X, -_pos.Y, 0)) *
  80.                     Matrix.CreateRotationZ(Rotation) *
  81.                     Matrix.CreateScale(new Vector3(Zoom, Zoom, 1)) *
  82.                     Matrix.CreateTranslation(new Vector3(ViewportWidth * 0.5f + shakeOffset.X, ViewportHeight * 0.5f + shakeOffset.Y, 0));
  83.         }
  84.     }
  85.  
  86.  
  87.     public Matrix RenderView
  88.     {
  89.         get
  90.         {
  91.             return Matrix.CreateScale(new Vector3(3, 3, 1)) * Matrix.CreateTranslation(new Vector3(Infinidrill.GraphicsDevice.Viewport.Width * 0.5f, Infinidrill.GraphicsDevice.Viewport.Height, 0));
  92.         }
  93.     }
  94.  
  95.     public Matrix Projection
  96.     {
  97.         get
  98.         {
  99.             return Matrix.CreateOrthographicOffCenter(0, Infinidrill.GraphicsDevice.Viewport.Width, Infinidrill.GraphicsDevice.Viewport.Height, 0, 0, 1);
  100.         }
  101.     }
  102.  
  103.     #endregion
  104.  
  105.     public Camera(Viewport viewport, float initialZoom)
  106.     {
  107.         _zoom = initialZoom;
  108.         _rotation = 0.0f;
  109.         _pos = Vector2.Zero;
  110.         ViewportWidth = viewport.Width;
  111.         ViewportHeight = viewport.Height;
  112.  
  113.         Origin = new Vector2(viewport.Width / 2.0f, viewport.Height / 2.0f);
  114.     }
  115.  
  116.     private float NextFloat()
  117.     {
  118.         return (float)Random.NextDouble() * 2f - 1f;
  119.     }
  120.  
  121.     public void Shake(float magnitude, float duration)
  122.     {
  123.         shaking = true;
  124.  
  125.         shakeMagnitude = magnitude;
  126.         shakeDuration = duration;
  127.  
  128.         shakeTimer = 0f;
  129.     }
  130.  
  131.     public void Update(GameTime gameTime)
  132.     {
  133.         if (shaking)
  134.         {
  135.             shakeTimer += (float)gameTime.ElapsedGameTime.TotalSeconds;
  136.  
  137.             GamePad.SetVibration(PlayerIndex.One, 1.0f, 1.0f);
  138.  
  139.             if (shakeTimer >= shakeDuration)
  140.             {
  141.                 shaking = false;
  142.                 shakeTimer = shakeDuration;
  143.             }
  144.  
  145.             float progress = shakeTimer / shakeDuration;
  146.             float magnitude = shakeMagnitude * (1f - (progress * progress));
  147.  
  148.             shakeOffset = new Vector3(NextFloat(), NextFloat(), NextFloat()) * magnitude;
  149.         }
  150.         else
  151.             GamePad.SetVibration(PlayerIndex.One, 0f, 0f);
  152.     }
  153.  
  154. }
Advertisement
Add Comment
Please, Sign In to add comment