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/CameraScrolling.cs | 96 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 96 insertions(+) create mode 100644 V3/Camera/CameraScrolling.cs (limited to 'V3/Camera/CameraScrolling.cs') diff --git a/V3/Camera/CameraScrolling.cs b/V3/Camera/CameraScrolling.cs new file mode 100644 index 0000000..72e4911 --- /dev/null +++ b/V3/Camera/CameraScrolling.cs @@ -0,0 +1,96 @@ +using Microsoft.Xna.Framework; +using Microsoft.Xna.Framework.Input; +using V3.Objects; +using MathHelper = Microsoft.Xna.Framework.MathHelper; +using Vector2 = Microsoft.Xna.Framework.Vector2; + +namespace V3.Camera +{ + /// + /// This is the camera class for a map-scrolling camera mode. Does not move over the map. + /// + // ReSharper disable once ClassNeverInstantiated.Global + public sealed class CameraScrolling : ICamera + { + public int MapPixelWidth { get; set; } + public int MapPixelHeight { get; set; } + private const int MinOffset = 10; + private const int CameraSpeed = 10; + private readonly GraphicsDeviceManager mGraphicsDeviceManager; + private Point mLocation; + public Matrix Transform { get; set; } + private int mMaxX; + private int mMaxY; + public Vector2 Location {get {return mLocation.ToVector2();} set { mLocation = value.ToPoint(); } } + public Point ScreenSize => new Point(mGraphicsDeviceManager.GraphicsDevice.Viewport.Width, mGraphicsDeviceManager.GraphicsDevice.Viewport.Height); + public Rectangle ScreenRectangle => new Rectangle(mLocation, ScreenSize); + + public CameraScrolling(GraphicsDeviceManager graphicsDeviceManager) + { + mGraphicsDeviceManager = graphicsDeviceManager; + } + + public void Update(ICreature player) + { + var viewport = mGraphicsDeviceManager.GraphicsDevice.Viewport; + + mMaxX = MapPixelWidth - viewport.Width; + mMaxY = MapPixelHeight / 2 - viewport.Height; + + var mouse = Mouse.GetState(); + if (mouse.X < MinOffset) + { + MoveCameraLeft(); + } + + if (mouse.X > viewport.Width - MinOffset) + { + MoveCameraRight(); + } + + if (mouse.Y < MinOffset) + { + MoveCameraDown(); + } + + if (mouse.Y > viewport.Height - MinOffset) + { + MoveCameraUp(); + } + Transform = Matrix.CreateTranslation(new Vector3(-mLocation.ToVector2(), 0)); + } + + /// + /// Move the Camera to the left. Does not go outside the map. + /// + private void MoveCameraLeft() + { + mLocation.X = MathHelper.Clamp(mLocation.X - CameraSpeed, 0, mMaxX); + } + + /// + /// Move the Camera to the right. Does not go outside the map. + /// + private void MoveCameraRight() + { + mLocation.X = MathHelper.Clamp(mLocation.X + CameraSpeed, 0, mMaxX); + } + + /// + /// Move the Camera up. Does not go outside the map. + /// + private void MoveCameraUp() + { + mLocation.Y = MathHelper.Clamp(mLocation.Y + CameraSpeed, 0, mMaxY); + } + + + /// + /// Move the Camera down. Does not go outside the map. + /// + private void MoveCameraDown() + { + mLocation.Y = MathHelper.Clamp(mLocation.Y - CameraSpeed, 0, mMaxY); + } + } +} \ No newline at end of file -- cgit v1.2.1