mirror of
https://github.com/Ellpeck/MLEM.git
synced 2024-12-24 01:09:23 +01:00
added simple camera
This commit is contained in:
parent
51f6f1d620
commit
8e46809d61
4 changed files with 62 additions and 2 deletions
14
MLEM.Extended/Extensions/CameraExtensions.cs
Normal file
14
MLEM.Extended/Extensions/CameraExtensions.cs
Normal file
|
@ -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));
|
||||
}
|
||||
|
||||
}
|
||||
}
|
|
@ -10,7 +10,7 @@
|
|||
<PackageProjectUrl>https://github.com/Ellpeck/MLEM</PackageProjectUrl>
|
||||
<RepositoryUrl>https://github.com/Ellpeck/MLEM</RepositoryUrl>
|
||||
<PackageLicenseUrl>https://github.com/Ellpeck/MLEM/blob/master/LICENSE</PackageLicenseUrl>
|
||||
<Version>1.0.2</Version>
|
||||
<Version>1.0.3</Version>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
|
46
MLEM/Cameras/Camera.cs
Normal file
46
MLEM/Cameras/Camera.cs
Normal file
|
@ -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)));
|
||||
}
|
||||
|
||||
}
|
||||
}
|
|
@ -10,7 +10,7 @@
|
|||
<PackageProjectUrl>https://github.com/Ellpeck/MLEM</PackageProjectUrl>
|
||||
<RepositoryUrl>https://github.com/Ellpeck/MLEM</RepositoryUrl>
|
||||
<PackageLicenseUrl>https://github.com/Ellpeck/MLEM/blob/master/LICENSE</PackageLicenseUrl>
|
||||
<Version>1.0.8</Version>
|
||||
<Version>1.0.9</Version>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
|
Loading…
Reference in a new issue