X-Git-Url: https://git.dogcows.com/gitweb?a=blobdiff_plain;f=CarFire%2FCarFire%2FCarFire%2FMovementManager.cs;h=3b8dae13a2d0983b3f3b0dfbc4f8f3be39f4da8e;hb=b32b73746f5bf95771eb5f9db95763ce2a44049f;hp=372be6c23deab452650958f305729077de272b4c;hpb=f31f4ae920ff902f4cd4fb64f5e6ccf0d5e58402;p=chaz%2Fcarfire
diff --git a/CarFire/CarFire/CarFire/MovementManager.cs b/CarFire/CarFire/CarFire/MovementManager.cs
index 372be6c..3b8dae1 100644
--- a/CarFire/CarFire/CarFire/MovementManager.cs
+++ b/CarFire/CarFire/CarFire/MovementManager.cs
@@ -6,6 +6,23 @@ 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,
+ None
+ }
+
+
///
/// 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 +51,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
@@ -93,10 +120,11 @@ namespace CarFire
bool requestMove = (moveLeft ^ moveRight) || (moveUp ^ moveDown);
if (!mIsMoving && 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);
}
@@ -113,6 +141,7 @@ namespace CarFire
alpha = mTimeAccumulator / mInverseSpeed;
UpdateCoordinates(moveLeft, moveRight, moveUp, moveDown);
+ mDirection = GetDirection(moveLeft, moveRight, moveUp, moveDown);
}
else
{
@@ -144,6 +173,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
@@ -151,7 +207,7 @@ namespace CarFire
void RecalculatePosition(float alpha)
{
- Console.WriteLine("last: " + mLastCoordinates + ", now: " + mCoordinates + ", alpha: " + alpha);
+ //Console.WriteLine("last: " + mLastCoordinates + ", now: " + mCoordinates + ", alpha: " + alpha);
mPosition.X = (float)mLastCoordinates.X + alpha * ((float)mCoordinates.X - (float)mLastCoordinates.X);
mPosition.Y = (float)mLastCoordinates.Y + alpha * ((float)mCoordinates.Y - (float)mLastCoordinates.Y);
}
@@ -182,6 +238,7 @@ namespace CarFire
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.
+ Direction mDirection; // The direction the object is facing.
#endregion
}