using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; using System; namespace RecrownedAthenaeum.Render { /// /// A virtual 2D camera that wraps the normal . Default projection is orthographic. /// public class Camera2D : BasicCamera { /// /// The width of the view of the camera. /// public int viewWidth; /// /// The height of the view of the camera. /// public int viewHeight; /// /// The 2D position. /// public Vector2 Position { get { return new Vector2(position.X, position.Y); } set { position.X = value.X; position.Y = value.Y; } } /// /// Places camera in the center given the corner position. /// public Vector2 ConrnerPosition { set { position.X = value.X + viewWidth / 2f; position.Y = value.Y + viewHeight / 2f; } } /// /// A 2D camera from the generic . /// /// Width of camera view. /// Height of camera view. /// A basic effect that will be updated with the correct matrice information everytime is called. Can be null and thus will not be used. public Camera2D(int width, int height, BasicEffect basicEffect = null) : base(basicEffect) { this.viewWidth = width; this.viewHeight = height; upDirection = Vector3.Down; ConrnerPosition = new Vector2(0, 0); Apply(); } /// /// Applies for 2D. /// Sets where the camera is looking for the view matrix to the position of the camera. /// public override void Apply() { projectionMatrix = Matrix.CreateOrthographic(viewWidth, viewHeight, 0, 1); position.Z = 0; lookAt = new Vector3(Position, 1f); base.Apply(); } /// /// Lerps to the given position. /// /// The multiplier for difference in distance. /// The target position to lerp to. /// Time between this frame and the previous frame. 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; } /// /// Moves the camera. /// Apply needs to be called. /// /// Magnitude of how much to move per axis. public void MoveCamera(Vector2 move) { Position += move; } } }