2 using System.Collections.Generic;
5 using Microsoft.Xna.Framework;
10 /// A type for a direction, including diagonals.
14 Down, // Default direction is down.
27 /// A class to manage the motion of objects on a grid of cells.
28 /// Each update you can pass a direction and the manager will move
29 /// the position to that point while also enforcing a speed limit.
30 /// This class does not detect collisions, so care must be taken
31 /// to only pass directions to a walkable cell during an update.
33 public class MovementManager
35 #region Public Properties
38 /// Get the current position in map coordinates. This is the
39 /// smooth, interpolated set of coordinates.
41 public Vector2 Position { get { return mPosition; } }
44 /// Get the grid coordinates where the object is at or
47 public Point Coordinates { get { return mCoordinates; } }
50 /// Get and set the speed of movement in grid cells / second.
55 /// Get whether or not the object is moving.
57 public bool IsMoving { get { return mIsMoving; } }
60 /// Get the direction the object is facing.
62 public Direction Direction { get { return mDirection; } }
67 #region Public Methods
70 /// Construct a movement manager with the initial position of
71 /// the thing you want to track.
73 /// <param name="position">Grid coordinates.</param>
74 public MovementManager(Point position)
76 mPosition = new Vector2((float)position.X, (float)position.Y);
77 mCoordinates = position;
78 mLastCoordinates = position;
83 /// Construct a movement manager with the initial position of
84 /// the thing you want to track and its speed.
86 /// <param name="position">Grid coordinates.</param>
87 /// <param name="speed">Speed: Grid cells per second.</param>
88 public MovementManager(Point position, float speed)
90 mPosition = new Vector2((float)position.X, (float)position.Y);
91 mCoordinates = position;
92 mLastCoordinates = position;
98 /// Update the movement manager with the timeslice and no directions.
100 /// <param name="timeSpan">The timeslice.</param>
101 public void Update(TimeSpan timeSpan)
103 Update(timeSpan, false, false, false, false);
107 /// Update the movement manager with the timeslice and a direction.
109 /// <param name="timeSpan">The timeslice.</param>
110 /// <param name="direction">Direction you want to move.</param>
111 public void Update(TimeSpan timeSpan, Direction direction)
113 if (direction == Direction.Left) Update(timeSpan, true, false, false, false);
114 else if (direction == Direction.UpperLeft) Update(timeSpan, true, false, true, false);
115 else if (direction == Direction.Up) Update(timeSpan, false, false, true, false);
116 else if (direction == Direction.UpperRight) Update(timeSpan, false, true, true, false);
117 else if (direction == Direction.Right) Update(timeSpan, false, true, false, false);
118 else if (direction == Direction.LowerRight) Update(timeSpan, false, true, false, true);
119 else if (direction == Direction.Down) Update(timeSpan, false, false, false, true);
120 else if (direction == Direction.LowerLeft) Update(timeSpan, true, false, false, true);
121 else Update(timeSpan);
125 /// Update the movement manager with the timeslice and the directions
126 /// the object is supposed to go. The directions will be ignored if the
127 /// object is currently in transit from one cell to another.
129 /// <param name="timeSpan">The timeslice.</param>
130 /// <param name="moveLeft">Want to move left.</param>
131 /// <param name="moveRight">Want to move right.</param>
132 /// <param name="moveUp">Want to move up.</param>
133 /// <param name="moveDown">Want to move down.</param>
134 public void Update(TimeSpan timeSpan, bool moveLeft, bool moveRight, bool moveUp, bool moveDown)
136 float passedTime = (float)timeSpan.TotalSeconds;
138 bool requestMove = (moveLeft ^ moveRight) || (moveUp ^ moveDown);
139 if (!mIsMoving && requestMove)
141 mTimeAccumulator = passedTime;
144 UpdateCoordinates(moveLeft, moveRight, moveUp, moveDown);
145 mDirection = GetDirection(moveLeft, moveRight, moveUp, moveDown);
147 RecalculatePosition(mTimeAccumulator / mInverseSpeed);
151 mTimeAccumulator += passedTime;
153 float alpha = mTimeAccumulator / mInverseSpeed;
158 mTimeAccumulator = mTimeAccumulator - mInverseSpeed;
159 alpha = mTimeAccumulator / mInverseSpeed;
161 UpdateCoordinates(moveLeft, moveRight, moveUp, moveDown);
162 mDirection = GetDirection(moveLeft, moveRight, moveUp, moveDown);
171 RecalculatePosition(alpha);
174 public void LockUpdate(TimeSpan timeSpan, bool moveLeft, bool moveRight, bool moveUp, bool moveDown)
176 float passedTime = (float)timeSpan.TotalSeconds;
177 if (moveLeft == true || moveRight == true || moveUp == true || moveDown == true)
179 mDirection = GetDirection(moveLeft, moveRight, moveUp, moveDown);
183 mTimeAccumulator += passedTime;
185 float alpha = mTimeAccumulator / mInverseSpeed;
192 RecalculatePosition(alpha);
198 /// Helper method to get neighbor cells from a point and directions.
200 /// <param name="point">The point.</param>
201 /// <param name="left">To the left.</param>
202 /// <param name="right">To the right.</param>
203 /// <param name="up">Above.</param>
204 /// <param name="down">Below.</param>
205 /// <returns>The neighbor cell coordinates.</returns>
206 public static Point GetNeighborCell(Point point, bool left, bool right, bool up, bool down)
209 if (right) point.X++;
216 /// Helper method to get a Direction type from directions.
218 /// <param name="left">Left.</param>
219 /// <param name="right">Right.</param>
220 /// <param name="up">Up.</param>
221 /// <param name="down">Down.</param>
222 /// <returns>The direction.</returns>
223 public static Direction GetDirection(bool left, bool right, bool up, bool down)
227 if (up) return Direction.UpperLeft;
228 else if (down) return Direction.LowerLeft;
229 else return Direction.Left;
231 else if (right && !left)
233 if (up) return Direction.UpperRight;
234 else if (down) return Direction.LowerRight;
235 else return Direction.Right;
237 else if (up) return Direction.Up;
238 else if (down) return Direction.Down;
239 else return Direction.None;
243 /// Helper method to get the general Direction type if you want to move
244 /// from one cell to another.
245 /// <param name="a">Starting point.</param>
246 /// <param name="b">Destination point.</param>
247 /// <returns>The direction toward the cell.</returns>
248 public static Direction GetDirection(Point a, Point b)
255 if (dy < 0) return Direction.UpperLeft;
256 else if (dy > 0) return Direction.LowerLeft;
257 else return Direction.Left;
261 if (dy < 0) return Direction.UpperRight;
262 else if (dy > 0) return Direction.LowerRight;
263 else return Direction.Right;
265 else if (dy < 0) return Direction.Up;
266 else if (dy > 0) return Direction.Down;
267 else return Direction.None;
273 #region Private Methods
275 void RecalculatePosition(float alpha)
277 //Console.WriteLine("last: " + mLastCoordinates + ", now: " + mCoordinates + ", alpha: " + alpha);
278 mPosition.X = (float)mLastCoordinates.X + alpha * ((float)mCoordinates.X - (float)mLastCoordinates.X);
279 mPosition.Y = (float)mLastCoordinates.Y + alpha * ((float)mCoordinates.Y - (float)mLastCoordinates.Y);
282 void UpdateCoordinates(bool moveLeft, bool moveRight, bool moveUp, bool moveDown)
284 mLastCoordinates = mCoordinates;
285 mCoordinates = GetNeighborCell(mCoordinates, moveLeft, moveRight, moveUp, moveDown);
287 if ((moveLeft && moveUp) || (moveUp && moveRight) || (moveRight && moveDown) || (moveDown && moveLeft))
289 mInverseSpeed = 1.4f / Speed;
293 mInverseSpeed = 1.0f / Speed;
300 #region Private Variables
302 Vector2 mPosition; // Position on the viewable map.
303 Point mCoordinates; // Position on the grid.
304 Point mLastCoordinates; // Last position on the grid.
305 float mInverseSpeed; // The time it takes to move from one cell to another.
306 float mTimeAccumulator; // Amount of time passed since last move.
307 bool mIsMoving; // Whether or not it is currently in the process of moving.
308 Direction mDirection; // The direction the object is facing.