From ced3d03bdb3ce866d832e03fb212865140905a9a Mon Sep 17 00:00:00 2001 From: Thomas Leyh Date: Sun, 24 Jul 2016 08:14:18 +0200 Subject: Add project files. --- V3/Camera/CameraCentered.cs | 57 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 V3/Camera/CameraCentered.cs (limited to 'V3/Camera/CameraCentered.cs') diff --git a/V3/Camera/CameraCentered.cs b/V3/Camera/CameraCentered.cs new file mode 100644 index 0000000..cd04209 --- /dev/null +++ b/V3/Camera/CameraCentered.cs @@ -0,0 +1,57 @@ +using Microsoft.Xna.Framework; +using V3.Objects; + +namespace V3.Camera +{ + /// + /// This is the Camera Class for a player-centered camera mode. Does not go outside the map. + /// + // ReSharper disable once ClassNeverInstantiated.Global + public sealed class CameraCentered : ICamera + { + public int MapPixelWidth { get; set; } + public int MapPixelHeight { get; set; } + private int mMaxX; + private int mMaxY; + public Matrix Transform { get; set; } + private Point mCamCenter; + public Vector2 Location { get { return mCamCenter.ToVector2(); } set { mCamCenter = value.ToPoint(); } } + public Point ScreenSize => new Point(mGraphicsDeviceManager.GraphicsDevice.Viewport.Width, mGraphicsDeviceManager.GraphicsDevice.Viewport.Height); + public Rectangle ScreenRectangle => new Rectangle(mCamCenter, ScreenSize); + + private readonly GraphicsDeviceManager mGraphicsDeviceManager; + + public CameraCentered(GraphicsDeviceManager graphicsDeviceManager) + { + mGraphicsDeviceManager = graphicsDeviceManager; + } + + private int ScreenWidth() + { + return mGraphicsDeviceManager.GraphicsDevice.Viewport.Width; + } + + private int ScreenHeight() + { + return mGraphicsDeviceManager.GraphicsDevice.Viewport.Height; + } + + /// + /// Updates the position of the camera related to the player's position. + /// + public void Update(ICreature player) + { + /*The center of the camera is by default in the upper left corner. To get the player in center, + simply substract the half of the screen values. Does not go outside the map. + */ + mMaxX = MapPixelWidth - ScreenWidth(); + mMaxY = MapPixelHeight / 2 - ScreenHeight(); + + mCamCenter = new Point(MathHelper.Clamp((int)(player.Position.X - ScreenWidth() * 0.5f), 0, mMaxX), + MathHelper.Clamp((int)(player.Position.Y - ScreenHeight() * 0.5f), 0, mMaxY)); + + //Transform matrix for the camera to make sure it is moving. + Transform = Matrix.CreateTranslation(new Vector3(-mCamCenter.ToVector2(), 0)); + } + } +} \ No newline at end of file -- cgit v1.2.1