X-Git-Url: https://git.dogcows.com/gitweb?p=chaz%2Fcarfire;a=blobdiff_plain;f=CarFire%2FCarFire%2FCarFire%2FMovementManager.cs;h=befeb4b62828f6bac4c74ee24df02c35abafaef8;hp=372be6c23deab452650958f305729077de272b4c;hb=3cd7b300c8d161218ed795d0b12a81ac2dc93b7c;hpb=e2cb0e6d7b2e6cc0a2720f9adc856dedbaaa808b
diff --git a/CarFire/CarFire/CarFire/MovementManager.cs b/CarFire/CarFire/CarFire/MovementManager.cs
index 372be6c..befeb4b 100644
--- a/CarFire/CarFire/CarFire/MovementManager.cs
+++ b/CarFire/CarFire/CarFire/MovementManager.cs
@@ -6,6 +6,22 @@ using Microsoft.Xna.Framework;
namespace CarFire
{
+ ///
+ /// A type for a direction, including diagonals.
+ ///
+ public enum Direction
+ {
+ Down, // Default direction is down.
+ Left,
+ UpperLeft,
+ Up,
+ UpperRight,
+ Right,
+ LowerRight,
+ LowerLeft
+ }
+
+
///
/// A class to manage the motion of objects on a grid of cells.
/// Each update you can pass a direction and the manager will move
@@ -34,6 +50,16 @@ namespace CarFire
///
public float Speed;
+ ///
+ /// Get whether or not the object is moving.
+ ///
+ public bool IsMoving { get { return mIsMoving; } }
+
+ ///
+ /// Get the direction the object is facing.
+ ///
+ public Direction Direction { get { return mDirection; } }
+
#endregion
@@ -91,16 +117,17 @@ namespace CarFire
float passedTime = (float)timeSpan.TotalSeconds;
bool requestMove = (moveLeft ^ moveRight) || (moveUp ^ moveDown);
- if (!mIsMoving && requestMove)
+ if (!IsMoving && requestMove)
{
- UpdateCoordinates(moveLeft, moveRight, moveUp, moveDown);
-
- mIsMoving = true;
mTimeAccumulator = passedTime;
+
+ mIsMoving = true;
+ UpdateCoordinates(moveLeft, moveRight, moveUp, moveDown);
+ mDirection = GetDirection(moveLeft, moveRight, moveUp, moveDown);
RecalculatePosition(mTimeAccumulator / mInverseSpeed);
}
- else if (mIsMoving)
+ else if (IsMoving)
{
mTimeAccumulator += passedTime;
@@ -113,6 +140,7 @@ namespace CarFire
alpha = mTimeAccumulator / mInverseSpeed;
UpdateCoordinates(moveLeft, moveRight, moveUp, moveDown);
+ mDirection = GetDirection(moveLeft, moveRight, moveUp, moveDown);
}
else
{
@@ -144,6 +172,33 @@ namespace CarFire
return point;
}
+ ///
+ /// Helper method to get a Direction type from directions.
+ ///
+ /// Left.
+ /// Right.
+ /// Up.
+ /// Down.
+ /// The direction.
+ public static Direction GetDirection(bool left, bool right, bool up, bool down)
+ {
+ if (left && !right)
+ {
+ if (up) return Direction.UpperLeft;
+ else if (down) return Direction.LowerLeft;
+ else return Direction.Left;
+ }
+ else if (right && !left)
+ {
+ if (up) return Direction.UpperRight;
+ else if (down) return Direction.LowerRight;
+ else return Direction.Right;
+ }
+ else if (up) return Direction.Up;
+ else if (down) return Direction.Down;
+ else return Direction.None;
+ }
+
#endregion
@@ -181,7 +236,8 @@ namespace CarFire
Point mLastCoordinates; // Last position on the grid.
float mInverseSpeed; // The time it takes to move from one cell to another.
float mTimeAccumulator; // Amount of time passed since last move.
- bool mIsMoving; // Whether or not it is currently in the process of moving.
+ bool mIsMoving // Whether or not it is currently in the process of moving.
+ Direction mDirection; // The direction the object is facing.
#endregion
}