aboutsummaryrefslogtreecommitdiff
path: root/V3/Camera/CameraScrolling.cs
blob: 72e4911b385cfde454cadbf9d7b9a314bf1e7430 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
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);
        }
    }
}