\r
namespace CarFire\r
{\r
+ /// <summary>\r
+ /// A type for a direction, including diagonals.\r
+ /// </summary>\r
+ public enum Direction\r
+ {\r
+ Down, // Default direction is down.\r
+ Left,\r
+ UpperLeft,\r
+ Up,\r
+ UpperRight,\r
+ Right,\r
+ LowerRight,\r
+ LowerLeft,\r
+ None\r
+ }\r
+\r
+\r
/// <summary>\r
/// A class to manage the motion of objects on a grid of cells.\r
/// Each update you can pass a direction and the manager will move\r
/// </summary>\r
public float Speed;\r
\r
+ /// <summary>\r
+ /// Get whether or not the object is moving.\r
+ /// </summary>\r
+ public bool IsMoving { get { return mIsMoving; } }\r
+\r
+ /// <summary>\r
+ /// Get the direction the object is facing.\r
+ /// </summary>\r
+ public Direction Direction { get { return mDirection; } }\r
+\r
#endregion\r
\r
\r
Update(timeSpan, false, false, false, false);\r
}\r
\r
+ /// <summary>\r
+ /// Update the movement manager with the timeslice and a direction.\r
+ /// </summary>\r
+ /// <param name="timeSpan">The timeslice.</param>\r
+ /// <param name="direction">Direction you want to move.</param>\r
+ public void Update(TimeSpan timeSpan, Direction direction)\r
+ {\r
+ if (direction == Direction.Left) Update(timeSpan, true, false, false, false);\r
+ else if (direction == Direction.UpperLeft) Update(timeSpan, true, false, true, false);\r
+ else if (direction == Direction.Up) Update(timeSpan, false, false, true, false);\r
+ else if (direction == Direction.UpperRight) Update(timeSpan, false, true, true, false);\r
+ else if (direction == Direction.Right) Update(timeSpan, false, true, false, false);\r
+ else if (direction == Direction.LowerRight) Update(timeSpan, false, true, false, true);\r
+ else if (direction == Direction.Down) Update(timeSpan, false, false, false, true);\r
+ else if (direction == Direction.LowerLeft) Update(timeSpan, true, false, false, true);\r
+ else Update(timeSpan);\r
+ }\r
+\r
/// <summary>\r
/// Update the movement manager with the timeslice and the directions\r
/// the object is supposed to go. The directions will be ignored if the\r
bool requestMove = (moveLeft ^ moveRight) || (moveUp ^ moveDown);\r
if (!mIsMoving && requestMove)\r
{\r
- UpdateCoordinates(moveLeft, moveRight, moveUp, moveDown);\r
-\r
- mIsMoving = true;\r
mTimeAccumulator = passedTime;\r
+ \r
+ mIsMoving = true;\r
+ UpdateCoordinates(moveLeft, moveRight, moveUp, moveDown);\r
+ mDirection = GetDirection(moveLeft, moveRight, moveUp, moveDown);\r
\r
RecalculatePosition(mTimeAccumulator / mInverseSpeed);\r
}\r
alpha = mTimeAccumulator / mInverseSpeed;\r
\r
UpdateCoordinates(moveLeft, moveRight, moveUp, moveDown);\r
+ mDirection = GetDirection(moveLeft, moveRight, moveUp, moveDown);\r
}\r
else\r
{\r
RecalculatePosition(alpha);\r
}\r
}\r
+ public void LockUpdate(TimeSpan timeSpan, bool moveLeft, bool moveRight, bool moveUp, bool moveDown)\r
+ {\r
+ float passedTime = (float)timeSpan.TotalSeconds;\r
+ if (moveLeft == true || moveRight == true || moveUp == true || moveDown == true)\r
+ {\r
+ mDirection = GetDirection(moveLeft, moveRight, moveUp, moveDown);\r
+ }\r
+ if (mIsMoving)\r
+ {\r
+ mTimeAccumulator += passedTime;\r
+\r
+ float alpha = mTimeAccumulator / mInverseSpeed;\r
+ if (alpha >= 1.0f)\r
+ {\r
+ mIsMoving = false;\r
+ alpha = 1.0f;\r
+ }\r
+\r
+ RecalculatePosition(alpha);\r
+ }\r
+ }\r
\r
\r
/// <summary>\r
return point;\r
}\r
\r
+ /// <summary>\r
+ /// Helper method to get a Direction type from directions.\r
+ /// </summary>\r
+ /// <param name="left">Left.</param>\r
+ /// <param name="right">Right.</param>\r
+ /// <param name="up">Up.</param>\r
+ /// <param name="down">Down.</param>\r
+ /// <returns>The direction.</returns>\r
+ public static Direction GetDirection(bool left, bool right, bool up, bool down)\r
+ {\r
+ if (left && !right)\r
+ {\r
+ if (up) return Direction.UpperLeft;\r
+ else if (down) return Direction.LowerLeft;\r
+ else return Direction.Left;\r
+ }\r
+ else if (right && !left)\r
+ {\r
+ if (up) return Direction.UpperRight;\r
+ else if (down) return Direction.LowerRight;\r
+ else return Direction.Right;\r
+ }\r
+ else if (up) return Direction.Up;\r
+ else if (down) return Direction.Down;\r
+ else return Direction.None;\r
+ }\r
+\r
+ /// <summary>\r
+ /// Helper method to get the general Direction type if you want to move\r
+ /// from one cell to another.\r
+ /// <param name="a">Starting point.</param>\r
+ /// <param name="b">Destination point.</param>\r
+ /// <returns>The direction toward the cell.</returns>\r
+ public static Direction GetDirection(Point a, Point b)\r
+ {\r
+ int dx = b.X - a.X;\r
+ int dy = b.Y - a.Y;\r
+\r
+ if (dx < 0)\r
+ {\r
+ if (dy < 0) return Direction.UpperLeft;\r
+ else if (dy > 0) return Direction.LowerLeft;\r
+ else return Direction.Left;\r
+ }\r
+ else if (dx > 0)\r
+ {\r
+ if (dy < 0) return Direction.UpperRight;\r
+ else if (dy > 0) return Direction.LowerRight;\r
+ else return Direction.Right;\r
+ }\r
+ else if (dy < 0) return Direction.Up;\r
+ else if (dy > 0) return Direction.Down;\r
+ else return Direction.None;\r
+ }\r
+\r
#endregion\r
\r
\r
\r
void RecalculatePosition(float alpha)\r
{\r
- Console.WriteLine("last: " + mLastCoordinates + ", now: " + mCoordinates + ", alpha: " + alpha);\r
+ //Console.WriteLine("last: " + mLastCoordinates + ", now: " + mCoordinates + ", alpha: " + alpha);\r
mPosition.X = (float)mLastCoordinates.X + alpha * ((float)mCoordinates.X - (float)mLastCoordinates.X);\r
mPosition.Y = (float)mLastCoordinates.Y + alpha * ((float)mCoordinates.Y - (float)mLastCoordinates.Y);\r
}\r
float mInverseSpeed; // The time it takes to move from one cell to another.\r
float mTimeAccumulator; // Amount of time passed since last move.\r
bool mIsMoving; // Whether or not it is currently in the process of moving.\r
+ Direction mDirection; // The direction the object is facing.\r
\r
#endregion\r
}\r