X-Git-Url: https://git.dogcows.com/gitweb?p=chaz%2Fcarfire;a=blobdiff_plain;f=CarFire%2FCarFire%2FCarFire%2FMovementManager.cs;h=7d818681993a47e22b09e4566046992e9072bdd3;hp=372be6c23deab452650958f305729077de272b4c;hb=122c062297acac44673e947b666c1d72cd23fb1b;hpb=f31f4ae920ff902f4cd4fb64f5e6ccf0d5e58402 diff --git a/CarFire/CarFire/CarFire/MovementManager.cs b/CarFire/CarFire/CarFire/MovementManager.cs index 372be6c..7d81868 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 @@ -76,6 +103,24 @@ namespace CarFire Update(timeSpan, false, false, false, false); } + /// + /// Update the movement manager with the timeslice and a direction. + /// + /// The timeslice. + /// Direction you want to move. + public void Update(TimeSpan timeSpan, Direction direction) + { + if (direction == Direction.Left) Update(timeSpan, true, false, false, false); + else if (direction == Direction.UpperLeft) Update(timeSpan, true, false, true, false); + else if (direction == Direction.Up) Update(timeSpan, false, false, true, false); + else if (direction == Direction.UpperRight) Update(timeSpan, false, true, true, false); + else if (direction == Direction.Right) Update(timeSpan, false, true, false, false); + else if (direction == Direction.LowerRight) Update(timeSpan, false, true, false, true); + else if (direction == Direction.Down) Update(timeSpan, false, false, false, true); + else if (direction == Direction.LowerLeft) Update(timeSpan, true, false, false, true); + else Update(timeSpan); + } + /// /// Update the movement manager with the timeslice and the directions /// the object is supposed to go. The directions will be ignored if the @@ -93,10 +138,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 +159,7 @@ namespace CarFire alpha = mTimeAccumulator / mInverseSpeed; UpdateCoordinates(moveLeft, moveRight, moveUp, moveDown); + mDirection = GetDirection(moveLeft, moveRight, moveUp, moveDown); } else { @@ -124,10 +171,31 @@ 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); + } + } /// - /// Helper method to get neighbor cells from a point and directions. + /// Helper method to get a neighbor cell from a point and directions. /// /// The point. /// To the left. @@ -135,7 +203,7 @@ namespace CarFire /// Above. /// Below. /// The neighbor cell coordinates. - public static Point GetNeighborCell(Point point, bool left, bool right, bool up, bool down) + public static Point GetNeighbor(Point point, bool left, bool right, bool up, bool down) { if (left) point.X--; if (right) point.X++; @@ -144,6 +212,98 @@ namespace CarFire return point; } + /// + /// Helper method to get a neighbor cell from a point and a direction. + /// + /// The point. + /// The direction. + /// The neighbor cell coordinates. + public static Point GetNeighbor(Point point, Direction direction) + { + switch (direction) + { + case Direction.Left: return new Point(point.X - 1, point.Y); + case Direction.UpperLeft: return new Point(point.X - 1, point.Y - 1); + case Direction.Up: return new Point(point.X, point.Y - 1); + case Direction.UpperRight: return new Point(point.X + 1, point.Y - 1); + case Direction.Right: return new Point(point.X + 1, point.Y); + case Direction.LowerRight: return new Point(point.X + 1, point.Y + 1); + case Direction.Down: return new Point(point.X, point.Y + 1); + case Direction.LowerLeft: return new Point(point.X - 1, point.Y + 1); + } + return point; + } + + /// + /// Helper method to get the two neighbor cells of two nearby cells. + /// + /// A point. + /// Another point. + /// An array of two points representing the neighbor cells. + public static Point[] GetNeighbors(Point a, Point b) + { + Point[] neighbors = new Point[2]; + neighbors[0] = new Point(a.X, b.Y); + neighbors[1] = new Point(b.X, a.Y); + return neighbors; + } + + + /// + /// 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; + } + + /// + /// Helper method to get the general Direction type if you want to move + /// from one cell to another. + /// Starting point. + /// Destination point. + /// The direction toward the cell. + public static Direction GetDirection(Point a, Point b) + { + int dx = b.X - a.X; + int dy = b.Y - a.Y; + + if (dx < 0) + { + if (dy < 0) return Direction.UpperLeft; + else if (dy > 0) return Direction.LowerLeft; + else return Direction.Left; + } + else if (dx > 0) + { + if (dy < 0) return Direction.UpperRight; + else if (dy > 0) return Direction.LowerRight; + else return Direction.Right; + } + else if (dy < 0) return Direction.Up; + else if (dy > 0) return Direction.Down; + else return Direction.None; + } + #endregion @@ -151,7 +311,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); } @@ -159,7 +319,7 @@ namespace CarFire void UpdateCoordinates(bool moveLeft, bool moveRight, bool moveUp, bool moveDown) { mLastCoordinates = mCoordinates; - mCoordinates = GetNeighborCell(mCoordinates, moveLeft, moveRight, moveUp, moveDown); + mCoordinates = GetNeighbor(mCoordinates, moveLeft, moveRight, moveUp, moveDown); if ((moveLeft && moveUp) || (moveUp && moveRight) || (moveRight && moveDown) || (moveDown && moveLeft)) { @@ -182,6 +342,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 }