Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Camera
- {
- public float MaxZoom { get { return zoomUpperLimit; } }
- public float MinZoom { get { return zoomLowerLimit; } }
- private const float zoomUpperLimit = 1.5f;
- private const float zoomLowerLimit = 0.1f;
- private float _zoom;
- private Vector2 _pos;
- private float _rotation;
- private Vector2 Origin;
- public int ViewportWidth, ViewportHeight;
- private bool shaking;
- private float shakeMagnitude;
- private float shakeDuration;
- private float shakeTimer;
- private Vector3 shakeOffset;
- #region Properties
- public float Zoom
- {
- get { return _zoom; }
- set
- {
- _zoom = value;
- if (_zoom < zoomLowerLimit)
- _zoom = zoomLowerLimit;
- if (_zoom > zoomUpperLimit)
- _zoom = zoomUpperLimit;
- }
- }
- public Rectangle Rectangle
- {
- get
- {
- int width = (int)((ViewportWidth / _zoom));
- int height = (int)((ViewportHeight / _zoom));
- return new Rectangle((int)(_pos.X - width / 2), (int)(_pos.Y - height / 2), width, height);
- }
- }
- public float Rotation
- {
- get { return _rotation; }
- set { _rotation = value; }
- }
- public void Move(Vector2 amount)
- {
- Position += amount;
- }
- public Vector2 Position
- {
- get { return _pos; }
- set { _pos = value; }
- }
- public Matrix GetViewMatrix(Vector2 parallax)
- {
- return Matrix.CreateTranslation(new Vector3(-Position * parallax, 0.0f)) *
- Matrix.CreateTranslation(new Vector3(-Origin, 0.0f)) *
- Matrix.CreateRotationZ(Rotation) *
- Matrix.CreateScale(Zoom, Zoom, 1) *
- Matrix.CreateTranslation(new Vector3(Origin, 0.0f));
- }
- public Matrix View
- {
- get
- {
- return Matrix.CreateTranslation(new Vector3(-_pos.X, -_pos.Y, 0)) *
- Matrix.CreateRotationZ(Rotation) *
- Matrix.CreateScale(new Vector3(Zoom, Zoom, 1)) *
- Matrix.CreateTranslation(new Vector3(ViewportWidth * 0.5f + shakeOffset.X, ViewportHeight * 0.5f + shakeOffset.Y, 0));
- }
- }
- public Matrix RenderView
- {
- get
- {
- return Matrix.CreateScale(new Vector3(3, 3, 1)) * Matrix.CreateTranslation(new Vector3(Infinidrill.GraphicsDevice.Viewport.Width * 0.5f, Infinidrill.GraphicsDevice.Viewport.Height, 0));
- }
- }
- public Matrix Projection
- {
- get
- {
- return Matrix.CreateOrthographicOffCenter(0, Infinidrill.GraphicsDevice.Viewport.Width, Infinidrill.GraphicsDevice.Viewport.Height, 0, 0, 1);
- }
- }
- #endregion
- public Camera(Viewport viewport, float initialZoom)
- {
- _zoom = initialZoom;
- _rotation = 0.0f;
- _pos = Vector2.Zero;
- ViewportWidth = viewport.Width;
- ViewportHeight = viewport.Height;
- Origin = new Vector2(viewport.Width / 2.0f, viewport.Height / 2.0f);
- }
- private float NextFloat()
- {
- return (float)Random.NextDouble() * 2f - 1f;
- }
- public void Shake(float magnitude, float duration)
- {
- shaking = true;
- shakeMagnitude = magnitude;
- shakeDuration = duration;
- shakeTimer = 0f;
- }
- public void Update(GameTime gameTime)
- {
- if (shaking)
- {
- shakeTimer += (float)gameTime.ElapsedGameTime.TotalSeconds;
- GamePad.SetVibration(PlayerIndex.One, 1.0f, 1.0f);
- if (shakeTimer >= shakeDuration)
- {
- shaking = false;
- shakeTimer = shakeDuration;
- }
- float progress = shakeTimer / shakeDuration;
- float magnitude = shakeMagnitude * (1f - (progress * progress));
- shakeOffset = new Vector3(NextFloat(), NextFloat(), NextFloat()) * magnitude;
- }
- else
- GamePad.SetVibration(PlayerIndex.One, 0f, 0f);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment