X-Git-Url: https://git.dogcows.com/gitweb?p=chaz%2Fcarfire;a=blobdiff_plain;f=CarFire%2FCarFire%2FCarFire%2FMovementManager.cs;h=e7002035b6509caec0f80f5e5cf980c21190f8d5;hp=372be6c23deab452650958f305729077de272b4c;hb=851a2f1efb5e981fad8f517170809b61d630e8b7;hpb=f31f4ae920ff902f4cd4fb64f5e6ccf0d5e58402 diff --git a/CarFire/CarFire/CarFire/MovementManager.cs b/CarFire/CarFire/CarFire/MovementManager.cs index 372be6c..e700203 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 { @@ -124,6 +153,27 @@ namespace CarFire RecalculatePosition(alpha); } } + public void LockUpdate(TimeSpan timeSpan, bool moveLeft, bool moveRight, bool moveUp, bool moveDown) + { + float passedTime = (float)timeSpan.TotalSeconds; + if (moveLeft == true || moveRight == true || moveUp == true || moveDown == true) + { + mDirection = GetDirection(moveLeft, moveRight, moveUp, moveDown); + } + if (mIsMoving) + { + mTimeAccumulator += passedTime; + + float alpha = mTimeAccumulator / mInverseSpeed; + if (alpha >= 1.0f) + { + mIsMoving = false; + alpha = 1.0f; + } + + RecalculatePosition(alpha); + } + } /// @@ -144,6 +194,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 +228,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 +259,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 }