From: Charles Date: Mon, 19 Apr 2010 21:49:43 +0000 (+0000) Subject: New MovementManager properties (IsMoving and Direction) the animation class will... X-Git-Url: https://git.dogcows.com/gitweb?a=commitdiff_plain;h=3cd7b300c8d161218ed795d0b12a81ac2dc93b7c;p=chaz%2Fcarfire New MovementManager properties (IsMoving and Direction) the animation class will need to use. git-svn-id: https://bd85.net/svn/cs3505_group@116 92bb83a3-7c8f-8a45-bc97-515c4e399668 --- 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 }