From 2ead7c6c6be50f4a022d791885205ff4ae32b12f Mon Sep 17 00:00:00 2001 From: Ellpeck Date: Wed, 7 Aug 2019 16:32:19 +0200 Subject: [PATCH] border constraints as well as some other useful goodies --- MLEM/Cameras/Camera.cs | 26 +++++++++++++++++++++----- MLEM/MLEM.csproj | 2 +- 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/MLEM/Cameras/Camera.cs b/MLEM/Cameras/Camera.cs index ff62c70..26c472a 100644 --- a/MLEM/Cameras/Camera.cs +++ b/MLEM/Cameras/Camera.cs @@ -15,8 +15,16 @@ namespace MLEM.Cameras { 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 Vector2 Max { + get => this.Position + this.ScaledViewport; + set => this.Position = value - this.ScaledViewport; + } + public Vector2 LookingPosition { + get => this.Position + this.ScaledViewport / 2; + set => this.Position = value - this.ScaledViewport / 2; + } public Viewport Viewport => this.graphicsDevice.Viewport; + public Vector2 ScaledViewport => new Vector2(this.Viewport.Width, this.Viewport.Height) / this.Scale; private readonly bool roundPosition; private readonly GraphicsDevice graphicsDevice; @@ -26,10 +34,6 @@ namespace MLEM.Cameras { 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)); } @@ -42,5 +46,17 @@ namespace MLEM.Cameras { return (this.ToWorldPos(Vector2.Zero), this.ToWorldPos(new Vector2(this.Viewport.Width, this.Viewport.Height))); } + public void ConstrainWorldBounds(Vector2 min, Vector2 max) { + if (this.Position.X < min.X) + this.Position.X = min.X; + if (this.Position.Y < min.Y) + this.Position.Y = min.Y; + + if (this.Max.X > max.X) + this.Max = new Vector2(max.X, this.Max.Y); + if (this.Max.Y > max.Y) + this.Max = new Vector2(this.Max.X, max.Y); + } + } } \ No newline at end of file diff --git a/MLEM/MLEM.csproj b/MLEM/MLEM.csproj index d3d25b9..88b82de 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.9 + 1.0.10