\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
+ }\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
float passedTime = (float)timeSpan.TotalSeconds;\r
\r
bool requestMove = (moveLeft ^ moveRight) || (moveUp ^ moveDown);\r
- if (!mIsMoving && requestMove)\r
+ if (!IsMoving && 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
- else if (mIsMoving)\r
+ else if (IsMoving)\r
{\r
mTimeAccumulator += passedTime;\r
\r
alpha = mTimeAccumulator / mInverseSpeed;\r
\r
UpdateCoordinates(moveLeft, moveRight, moveUp, moveDown);\r
+ mDirection = GetDirection(moveLeft, moveRight, moveUp, moveDown);\r
}\r
else\r
{\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
#endregion\r
\r
\r
Point mLastCoordinates; // Last position on the grid.\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
+ 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