UpperRight,\r
Right,\r
LowerRight,\r
- LowerLeft\r
+ LowerLeft,\r
+ None\r
}\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
float passedTime = (float)timeSpan.TotalSeconds;\r
\r
bool requestMove = (moveLeft ^ moveRight) || (moveUp ^ moveDown);\r
- if (!IsMoving && requestMove)\r
+ if (!mIsMoving && requestMove)\r
{\r
mTimeAccumulator = passedTime;\r
\r
\r
RecalculatePosition(mTimeAccumulator / mInverseSpeed);\r
}\r
- else if (IsMoving)\r
+ else if (mIsMoving)\r
{\r
mTimeAccumulator += passedTime;\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
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
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