Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public sealed class Camera2D : GameComponent, ICamera
- {
- bool updateViewRequired = false;
- float zoom;
- public float Zoom
- {
- get { return zoom; }
- set
- {
- zoom = value;
- UpdateZoomViewPort();
- }
- }
- float rotation;
- public float Rotation
- {
- get { return rotation; }
- set
- {
- rotation = value;
- UpdateRotation();
- }
- }
- public Vector2 Position { get; private set; }
- Viewport viewPort;
- Vector3 viewPortCenter;
- Matrix scaleMatrix;
- Matrix rotationMatrix;
- Matrix translationMatrix;
- Matrix focusPointMatrix;
- public Matrix View { get; private set; }
- int zoomWidth;
- int zoomHeight;
- public Camera2D(Game game, float zoom)
- : base(game)
- {
- }
- public Camera2D(Game game)
- : base(game)
- {
- UpdateViewport();
- }
- public void UpdateViewport()
- {
- viewPort = Game.GraphicsDevice.Viewport;
- viewPortCenter = new Vector3(viewPort.Width * 0.5f, viewPort.Height * 0.5f, 0);
- // optimiert für eine breite von 1680! wenn schmäler, dann mehr zoomen!
- focusPointMatrix = Matrix.CreateTranslation(viewPortCenter);
- Zoom = (float)Math.Round(viewPort.Width / 16.8 / 100, 2);
- Rotation = -0.5f;
- Position = Vector2.Zero;
- UpdateTranslation();
- updateViewRequired = true;
- }
- public void MoveBy(Vector2 direction)
- {
- Position += direction;
- UpdateTranslation();
- }
- public void MoveTo(Vector2 position)
- {
- if(Vector2.Distance(position, Position) < 0.1f) return;
- Position = position;
- UpdateTranslation();
- }
- public override void Update(GameTime gameTime)
- {
- if (!updateViewRequired) return;
- View = translationMatrix *
- rotationMatrix *
- scaleMatrix *
- focusPointMatrix;
- }
- private void UpdateZoomViewPort()
- {
- zoomWidth = (int)(viewPort.Width * (1 / Zoom) * 1.5f);
- zoomHeight = (int)(viewPort.Height * (1 / Zoom) * 2.5f);
- scaleMatrix = Matrix.CreateScale(new Vector3(Zoom, Zoom, Zoom));
- updateViewRequired = true;
- }
- private void UpdateRotation()
- {
- rotationMatrix = Matrix.CreateRotationZ(Rotation);
- Up = Vector2.Transform(-Vector2.UnitY, Matrix.CreateRotationZ(-Rotation));
- updateViewRequired = true;
- }
- private void UpdateTranslation()
- {
- translationMatrix = Matrix.CreateTranslation(new Vector3(-Position.X, -Position.Y, 0));
- updateViewRequired = true;
- }
- public Vector2 Up
- {
- get; private set;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment