X-Git-Url: https://git.dogcows.com/gitweb?p=chaz%2Fcarfire;a=blobdiff_plain;f=CarFire%2FCarFire%2FCarFire%2FMovementManager.cs;h=ad26e478a317f8921f9b14e512f97f9df2690e42;hp=e7002035b6509caec0f80f5e5cf980c21190f8d5;hb=e8ee0aa62a7e8b5dffa9e02c00c3e353a9e93b4c;hpb=851a2f1efb5e981fad8f517170809b61d630e8b7 diff --git a/CarFire/CarFire/CarFire/MovementManager.cs b/CarFire/CarFire/CarFire/MovementManager.cs index e700203..ad26e47 100644 --- a/CarFire/CarFire/CarFire/MovementManager.cs +++ b/CarFire/CarFire/CarFire/MovementManager.cs @@ -44,7 +44,7 @@ namespace CarFire /// Get the grid coordinates where the object is at or /// is moving to. /// - public Point Coordinates { get { return mCoordinates; } } + public Point Coordinates { get { return mCoordinates; } set { mCoordinates = value; } } /// /// Get and set the speed of movement in grid cells / second. @@ -103,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 @@ -177,7 +195,7 @@ namespace CarFire /// - /// 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. @@ -185,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++; @@ -194,6 +212,43 @@ 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. /// @@ -221,6 +276,34 @@ namespace CarFire 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 @@ -236,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)) {