2018-11-30 02:41:06 +00:00
|
|
|
|
using Microsoft.Xna.Framework;
|
|
|
|
|
using Microsoft.Xna.Framework.Graphics;
|
|
|
|
|
using System;
|
|
|
|
|
|
2018-12-04 13:45:09 +00:00
|
|
|
|
namespace RecrownedAthenaeum.Camera
|
2018-11-30 02:41:06 +00:00
|
|
|
|
{
|
2019-01-14 06:34:35 +00:00
|
|
|
|
/// <summary>
|
2019-02-06 06:12:39 +00:00
|
|
|
|
/// A virtual 2D camera that wraps the normal <see cref="Camera3D"/>. Default projection is orthographic.
|
2019-01-14 06:34:35 +00:00
|
|
|
|
/// </summary>
|
2019-02-06 06:12:39 +00:00
|
|
|
|
public class Camera2D : Camera3D
|
2018-11-30 02:41:06 +00:00
|
|
|
|
{
|
2019-01-14 06:34:35 +00:00
|
|
|
|
/// <summary>
|
2019-02-06 06:11:22 +00:00
|
|
|
|
/// The 2D position.
|
2019-01-14 06:34:35 +00:00
|
|
|
|
/// </summary>
|
2019-02-06 06:11:22 +00:00
|
|
|
|
public Vector2 Position { get { return new Vector2(position.X, position.Y); } set { position.X = value.X; position.Y = value.Y; } }
|
2019-01-14 06:34:35 +00:00
|
|
|
|
|
2019-02-11 05:28:48 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Places camera in the center given the corner position.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public Vector2 CenterPosition { set { position.X = value.X + graphicsDevice.Viewport.Width / 2f; position.Y = value.Y + graphicsDevice.Viewport.Height / 2f; } }
|
|
|
|
|
|
2019-01-14 06:34:35 +00:00
|
|
|
|
/// <summary>
|
2019-02-06 06:12:39 +00:00
|
|
|
|
/// A 2D camera from the generic <see cref="Camera3D"/>.
|
2019-01-14 06:34:35 +00:00
|
|
|
|
/// </summary>
|
2019-02-08 05:44:39 +00:00
|
|
|
|
/// <param name="graphicsDevice">The graphics device to use if not using the one in <see cref="Configuration"/>.</param>
|
|
|
|
|
public Camera2D(GraphicsDevice graphicsDevice = null) : base(graphicsDevice)
|
2018-11-30 02:41:06 +00:00
|
|
|
|
{
|
2019-02-11 05:28:48 +00:00
|
|
|
|
projectionMatrix = Matrix.CreateOrthographic(this.graphicsDevice.Viewport.Width, this.graphicsDevice.Viewport.Height, 0, 1);
|
|
|
|
|
CenterPosition = new Vector2(0, 0);
|
2019-02-25 04:44:02 +00:00
|
|
|
|
upDirection = Vector3.Down;
|
2019-02-11 05:28:48 +00:00
|
|
|
|
Apply();
|
|
|
|
|
}
|
2019-01-22 01:56:51 +00:00
|
|
|
|
|
2019-02-11 05:28:48 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Applies for 2D.
|
|
|
|
|
/// Sets where the camera is looking for the view matrix to the position of the camera.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public override void Apply()
|
|
|
|
|
{
|
2019-02-25 04:44:02 +00:00
|
|
|
|
position.Z = 0;
|
2019-02-11 05:28:48 +00:00
|
|
|
|
lookAt = new Vector3(Position, 1f);
|
|
|
|
|
base.Apply();
|
2018-11-30 02:41:06 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-01-14 06:34:35 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Lerps to the given position.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="alpha">The multiplier for difference in distance.</param>
|
|
|
|
|
/// <param name="targetPosition">The target position to lerp to.</param>
|
|
|
|
|
/// <param name="delta">Time between this frame and the previous frame.</param>
|
2018-11-30 02:41:06 +00:00
|
|
|
|
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;
|
2019-02-06 06:11:22 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-02-11 05:28:48 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Moves the camera.
|
|
|
|
|
/// Apply needs to be called.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="move">Magnitude of how much to move per axis.</param>
|
|
|
|
|
public void MoveCamera(Vector2 move)
|
2019-02-06 06:11:22 +00:00
|
|
|
|
{
|
2019-02-11 05:28:48 +00:00
|
|
|
|
Position += move;
|
2018-11-30 02:41:06 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|