diff options
Diffstat (limited to 'V3/Camera')
-rw-r--r-- | V3/Camera/CameraCentered.cs | 57 | ||||
-rw-r--r-- | V3/Camera/CameraManager.cs | 71 | ||||
-rw-r--r-- | V3/Camera/CameraScrolling.cs | 96 | ||||
-rw-r--r-- | V3/Camera/CameraType.cs | 8 | ||||
-rw-r--r-- | V3/Camera/ICamera.cs | 18 |
5 files changed, 250 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 diff --git a/V3/Camera/CameraManager.cs b/V3/Camera/CameraManager.cs new file mode 100644 index 0000000..b4d502e --- /dev/null +++ b/V3/Camera/CameraManager.cs @@ -0,0 +1,71 @@ +using Microsoft.Xna.Framework; +using System.Collections.Generic; +using V3.Data; +using V3.Objects; + +namespace V3.Camera +{ + /// <summary> + /// Stores and provides access to the possible cameras. + /// </summary> + // ReSharper disable once ClassNeverInstantiated.Global + public sealed class CameraManager + { + private readonly IOptionsManager mOptionsManager; + private readonly CameraCentered mCameraCentered; + private readonly CameraScrolling mCameraScrolling; + + /// <summary> + /// Creates a new CameraManager. + /// </summary> + public CameraManager(CameraCentered cameraCentered, + CameraScrolling cameraScrolling, IOptionsManager optionsManager) + { + mCameraCentered = cameraCentered; + mCameraScrolling = cameraScrolling; + mOptionsManager = optionsManager; + } + + /// <summary> + /// Initializes the cameras with the given map data. + /// </summary> + public void Initialize(Point mapPixelSize) + { + // TODO: Warum *2? Irgendwas stimmt bei der Interpretation hier nicht. + var mapPixelHeight = mapPixelSize.Y * 2; + var mapPixelWidth = mapPixelSize.X; + var cameras = new List<ICamera> { mCameraCentered, mCameraScrolling }; + foreach (var camera in cameras) + { + camera.MapPixelHeight = mapPixelHeight; + camera.MapPixelWidth = mapPixelWidth; + } + } + + /// <summary> + /// Updates the cameras. + /// </summary> + public void Update(ICreature creature) + { + GetCamera().Update(creature); + if (mOptionsManager.Options.CameraType != CameraType.Scrolling) + mCameraScrolling.Location = GetCamera().Location; + } + + /// <summary> + /// Returns the currently selected camera. + /// </summary> + public ICamera GetCamera() + { + switch (mOptionsManager.Options.CameraType) + { + case CameraType.Centered: + return mCameraCentered; + case CameraType.Scrolling: + return mCameraScrolling; + default: + return mCameraScrolling; + } + } + } +} 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 +{ + /// <summary> + /// This is the camera class for a map-scrolling camera mode. Does not move over the map. + /// </summary> + // 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)); + } + + /// <summary> + /// Move the Camera to the left. Does not go outside the map. + /// </summary> + private void MoveCameraLeft() + { + mLocation.X = MathHelper.Clamp(mLocation.X - CameraSpeed, 0, mMaxX); + } + + /// <summary> + /// Move the Camera to the right. Does not go outside the map. + /// </summary> + private void MoveCameraRight() + { + mLocation.X = MathHelper.Clamp(mLocation.X + CameraSpeed, 0, mMaxX); + } + + /// <summary> + /// Move the Camera up. Does not go outside the map. + /// </summary> + private void MoveCameraUp() + { + mLocation.Y = MathHelper.Clamp(mLocation.Y + CameraSpeed, 0, mMaxY); + } + + + /// <summary> + /// Move the Camera down. Does not go outside the map. + /// </summary> + private void MoveCameraDown() + { + mLocation.Y = MathHelper.Clamp(mLocation.Y - CameraSpeed, 0, mMaxY); + } + } +}
\ No newline at end of file diff --git a/V3/Camera/CameraType.cs b/V3/Camera/CameraType.cs new file mode 100644 index 0000000..4e373d9 --- /dev/null +++ b/V3/Camera/CameraType.cs @@ -0,0 +1,8 @@ +namespace V3.Camera +{ + public enum CameraType + { + Centered, + Scrolling + } +} diff --git a/V3/Camera/ICamera.cs b/V3/Camera/ICamera.cs new file mode 100644 index 0000000..4a8bbcd --- /dev/null +++ b/V3/Camera/ICamera.cs @@ -0,0 +1,18 @@ +using Microsoft.Xna.Framework; +using V3.Objects; + +namespace V3.Camera +{ + public interface ICamera + { + Matrix Transform { get; set; } + Vector2 Location { get; set; } + Point ScreenSize { get; } + Rectangle ScreenRectangle { get; } + + void Update(ICreature player); + + int MapPixelWidth { get; set; } + int MapPixelHeight { get; set; } + } + } |