recrownedgtk/RecrownedAthenaeum/Camera/Camera2D.cs

42 lines
1.4 KiB
C#

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System;
namespace RecrownedAthenaeum.Camera
{
public class Camera2D
{
public float Zoom;
public Vector2 Position;
public Matrix Matrix { get; private set; }
public GraphicsDevice graphicsDevice;
public Camera2D(GraphicsDevice graphicsDevice)
{
this.graphicsDevice = graphicsDevice;
Zoom = 1f;
Position.X = this.graphicsDevice.Viewport.Width * 0.5f;
Position.Y = this.graphicsDevice.Viewport.Height * 0.5f;
}
public void Update()
{
Rectangle bounds = graphicsDevice.Viewport.Bounds;
Matrix =
Matrix.CreateTranslation(new Vector3(-Position.X, -Position.Y, 0)) *
Matrix.CreateScale(Zoom) *
Matrix.CreateTranslation(new Vector3(bounds.Width * 0.5f, bounds.Height * 0.5f, 0f));
}
public void LinearInterpolationToPosition(float alpha, Vector2 targetPosition, float delta)
{
if (alpha <= 0 && alpha > 1f) throw new ArgumentException("Alpha can't be greater than 1f, less than or equal to 0.");
Vector2 distance = targetPosition - Position;
distance *= (float)(1.0f - Math.Pow(1 - alpha, delta / 0.02f));
Position += distance;
}
}
}