diff options
author | Thomas Leyh <leyh.thomas@web.de> | 2016-07-24 08:14:18 +0200 |
---|---|---|
committer | Thomas Leyh <leyh.thomas@web.de> | 2016-07-24 08:14:18 +0200 |
commit | ced3d03bdb3ce866d832e03fb212865140905a9a (patch) | |
tree | 2a16c2063a46d3c354ce1585029dda3124f6ad93 /V3/Camera/CameraCentered.cs | |
parent | 0394dccaf06e1009e591a6ff4d645895574724c1 (diff) | |
download | V3-release.tar.gz V3-release.tar.bz2 |
Diffstat (limited to 'V3/Camera/CameraCentered.cs')
-rw-r--r-- | V3/Camera/CameraCentered.cs | 57 |
1 files changed, 57 insertions, 0 deletions
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 +{ + /// <summary> + /// This is the Camera Class for a player-centered camera mode. Does not go outside the map. + /// </summary> + // 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; + } + + /// <summary> + /// Updates the position of the camera related to the player's position. + /// </summary> + 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 |