aboutsummaryrefslogtreecommitdiff
path: root/V3/Objects/Movement/PlayerMovement.cs
blob: 326503ff39b51d4b5d93396935a8230f6908f66d (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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using V3.Data;
using V3.Map;

namespace V3.Objects.Movement
{
    // TODO: Rename the class. Current name is unfitting. (But what is fitting?)
    /// <summary>
    /// Movement scheme for moving an object with the pathfinder.
    /// </summary>
    public class PlayerMovement : IMovable
    {
        private const int CellHeight = Constants.CellHeight;
        private const int CellWidth = Constants.CellWidth;
        private const float SpeedModifier = 0.25f;

        private List<Vector2> mPath;
        private int mStep;
        private Vector2 mLastMovement;

        public bool IsMoving { get; private set; }

        /// <summary>
        /// Calculates a path without collisions to desired destination.
        /// </summary>
        /// <param name="pathfinder">Pathfinder to use.</param>
        /// <param name="position">Current position in pixel.</param>
        /// <param name="destination">Destination in pixel.</param>
        public void FindPath(Pathfinder pathfinder, Vector2 position, Vector2 destination)
        {
            mStep = 0;
            mPath = pathfinder.FindPath(new Vector2((int)(position.X / CellWidth), (int)(position.Y / CellHeight)),
                new Vector2((int)(destination.X / CellWidth), (int)(destination.Y / CellHeight)));
            IsMoving = mPath.Count > 0;
        }

        /// <summary>
        /// Uses pathfinder to for steady movement to new transition.
        /// </summary>
        /// <param name="currentPosition">Current position in pixel.</param>
        /// <param name="speed">Movement speed of the creature.</param>
        /// <returns>Normalized vector * speed which represents a small step in the direction of desired destination.(</returns>
        public virtual Vector2 GiveNewPosition(Vector2 currentPosition, int speed)
        {
            Vector2 nextPosition = mPath[mStep];
            Vector2 newPosition = nextPosition - currentPosition;
            newPosition.Normalize();
            float distanceToDestination = Vector2.Distance(nextPosition, currentPosition);
            if (distanceToDestination < SpeedModifier * speed)
            {
                if (mStep == mPath.Count - 1)
                {
                    IsMoving = false;
                }
                else
                {
                    mStep++;
                }
            }
            mLastMovement = newPosition;
            return newPosition * SpeedModifier * speed;
        }

        /// <summary>
        /// Calculates the direction the creature is looking when moving.
        /// </summary>
        public MovementDirection GiveMovementDirection()
        {
            //   |\
            //   |α\        α == 22.5°
            //  b|  \ 1     β == 67.5°
            //   |  β\
            //   ──────
            //     a
            const float b = 0.92f;  // b == sin β
            const float a = 0.38f;  // a == sin α
            Vector2 direction = mLastMovement;
            MovementDirection movementDirection;
            if (direction.X < -b)
            {
                movementDirection = MovementDirection.W;
            }
            else if (direction.X > b)
            {
                movementDirection = MovementDirection.O;
            }
            else if (direction.Y > 0)
            {
                if (direction.X < -a)
                {
                    movementDirection = MovementDirection.SW;
                }
                else if (direction.X > a)
                {
                    movementDirection = MovementDirection.SO;
                }
                else
                {
                    movementDirection = MovementDirection.S;
                }
            }
            else
            {
                if (direction.X < -a)
                {
                    movementDirection = MovementDirection.NW;
                }
                else if (direction.X > a)
                {
                    movementDirection = MovementDirection.NO;
                }
                else
                {
                    movementDirection = MovementDirection.N;
                }
            }
            return movementDirection;
        }
        /// <summary>
        /// Save the movement data to a MovementData object.
        /// </summary>
        /// <returns>the MovementData object with the current status</returns>
        public MovementData SaveData()
        {
            var data = new MovementData();
            data.IsMoving = IsMoving;
            if (IsMoving)
            {
                data.Path = mPath;
                data.Step = mStep;
                data.LastMovement = mLastMovement;
            }
            return data;
        }

        /// <summary>
        /// Restore the movement state from the given data.
        /// </summary>
        /// <param name="movementData">the state of the movement to restore</param>
        public void LoadData(MovementData movementData)
        {
            if (movementData == null)
                return;

            IsMoving = movementData.IsMoving;
            if (IsMoving)
            {
                mPath = movementData.Path;
                mStep = movementData.Step;
                mLastMovement = movementData.LastMovement;
            }
        }
    }
}