From 8e46809d61914695580483b5031d19084102d86a Mon Sep 17 00:00:00 2001 From: Ellpeck Date: Wed, 7 Aug 2019 01:21:32 +0200 Subject: [PATCH] added simple camera --- MLEM.Extended/Extensions/CameraExtensions.cs | 14 ++++++ MLEM.Extended/MLEM.Extended.csproj | 2 +- MLEM/Cameras/Camera.cs | 46 ++++++++++++++++++++ MLEM/MLEM.csproj | 2 +- 4 files changed, 62 insertions(+), 2 deletions(-) create mode 100644 MLEM.Extended/Extensions/CameraExtensions.cs create mode 100644 MLEM/Cameras/Camera.cs diff --git a/MLEM.Extended/Extensions/CameraExtensions.cs b/MLEM.Extended/Extensions/CameraExtensions.cs new file mode 100644 index 0000000..63915ba --- /dev/null +++ b/MLEM.Extended/Extensions/CameraExtensions.cs @@ -0,0 +1,14 @@ +using Microsoft.Xna.Framework; +using MonoGame.Extended; +using Camera = MLEM.Cameras.Camera; + +namespace MLEM.Extended.Extensions { + public static class CameraExtensions { + + public static RectangleF GetVisibleArea(this Camera camera) { + var start = camera.ToWorldPos(Vector2.Zero); + return new RectangleF(start, camera.ToWorldPos(new Vector2(camera.Viewport.Width, camera.Viewport.Height) - start)); + } + + } +} \ No newline at end of file diff --git a/MLEM.Extended/MLEM.Extended.csproj b/MLEM.Extended/MLEM.Extended.csproj index 93d1c41..0cfc7ef 100644 --- a/MLEM.Extended/MLEM.Extended.csproj +++ b/MLEM.Extended/MLEM.Extended.csproj @@ -10,7 +10,7 @@ https://github.com/Ellpeck/MLEM https://github.com/Ellpeck/MLEM https://github.com/Ellpeck/MLEM/blob/master/LICENSE - 1.0.2 + 1.0.3 diff --git a/MLEM/Cameras/Camera.cs b/MLEM/Cameras/Camera.cs new file mode 100644 index 0000000..ff62c70 --- /dev/null +++ b/MLEM/Cameras/Camera.cs @@ -0,0 +1,46 @@ +using Microsoft.Xna.Framework; +using Microsoft.Xna.Framework.Graphics; +using MLEM.Extensions; + +namespace MLEM.Cameras { + public class Camera { + + public Vector2 Position; + public float Scale; + public Matrix ViewMatrix { + get { + var pos = -this.Position * this.Scale; + if (this.roundPosition) + pos = pos.Floor(); + return Matrix.CreateScale(this.Scale, this.Scale, 1) * Matrix.CreateTranslation(new Vector3(pos, 0)); + } + } + public Vector2 LookingPosition => this.Position + new Vector2(this.Viewport.Width / 2, this.Viewport.Height / 2) / this.Scale; + public Viewport Viewport => this.graphicsDevice.Viewport; + + private readonly bool roundPosition; + private readonly GraphicsDevice graphicsDevice; + + public Camera(GraphicsDevice graphicsDevice, bool roundPosition = true) { + this.graphicsDevice = graphicsDevice; + this.roundPosition = roundPosition; + } + + public void LookAt(Vector2 worldPos) { + this.Position = worldPos - new Vector2(this.Viewport.Width / 2, this.Viewport.Height / 2) / this.Scale; + } + + public Vector2 ToWorldPos(Vector2 pos) { + return Vector2.Transform(pos, Matrix.Invert(this.ViewMatrix)); + } + + public Vector2 ToCameraPos(Vector2 pos) { + return Vector2.Transform(pos, this.ViewMatrix); + } + + public (Vector2 topLeft, Vector2 bottomRight) GetVisibleArea() { + return (this.ToWorldPos(Vector2.Zero), this.ToWorldPos(new Vector2(this.Viewport.Width, this.Viewport.Height))); + } + + } +} \ No newline at end of file diff --git a/MLEM/MLEM.csproj b/MLEM/MLEM.csproj index 46b1bb5..d3d25b9 100644 --- a/MLEM/MLEM.csproj +++ b/MLEM/MLEM.csproj @@ -10,7 +10,7 @@ https://github.com/Ellpeck/MLEM https://github.com/Ellpeck/MLEM https://github.com/Ellpeck/MLEM/blob/master/LICENSE - 1.0.8 + 1.0.9