From 198cb6056e93fecd69e65351ca8d0b34a077523f Mon Sep 17 00:00:00 2001 From: Charles Date: Mon, 26 Apr 2010 23:28:26 +0000 Subject: [PATCH] better player movement (walls are no longer sticky) git-svn-id: https://bd85.net/svn/cs3505_group@155 92bb83a3-7c8f-8a45-bc97-515c4e399668 --- CarFire/CarFire/CarFire/MovementManager.cs | 19 ++++++++++++++++-- CarFire/CarFire/CarFire/Player.cs | 23 +++++++++++----------- CarFire/CarFire/CarFire/Projectile.cs | 2 +- 3 files changed, 30 insertions(+), 14 deletions(-) diff --git a/CarFire/CarFire/CarFire/MovementManager.cs b/CarFire/CarFire/CarFire/MovementManager.cs index 3511e42..73caa57 100644 --- a/CarFire/CarFire/CarFire/MovementManager.cs +++ b/CarFire/CarFire/CarFire/MovementManager.cs @@ -203,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++; @@ -212,6 +212,21 @@ namespace CarFire 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. /// @@ -282,7 +297,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)) { diff --git a/CarFire/CarFire/CarFire/Player.cs b/CarFire/CarFire/CarFire/Player.cs index b84b048..124851c 100644 --- a/CarFire/CarFire/CarFire/Player.cs +++ b/CarFire/CarFire/CarFire/Player.cs @@ -88,22 +88,23 @@ namespace CarFire bool moveRight = keysPressed.Contains(Keys.Right); bool moveUp = keysPressed.Contains(Keys.Up); bool moveDown = keysPressed.Contains(Keys.Down); - Point destination = MovementManager.GetNeighborCell(mMotion.Coordinates, moveLeft, moveRight, moveUp, moveDown); - if (!keysPressed.Contains(Keys.LeftControl)) + + List possibleDestinations = new List(); + possibleDestinations.Add(MovementManager.GetNeighbor(mMotion.Coordinates, moveLeft, moveRight, moveUp, moveDown)); + possibleDestinations.AddRange(MovementManager.GetNeighbors(mMotion.Coordinates, possibleDestinations[0])); + + Direction direction = Direction.None; + foreach (Point destination in possibleDestinations) { if (game.IsCellOpen(destination)) { - mMotion.Update(timeSpan, moveLeft, moveRight, moveUp, moveDown); - } - else - { - mMotion.Update(timeSpan); + direction = MovementManager.GetDirection(mMotion.Coordinates, destination); + break; } } - else - { - mMotion.LockUpdate(timeSpan, moveLeft, moveRight, moveUp, moveDown); - } + + if (direction != Direction.None && !keysPressed.Contains(Keys.LeftControl)) mMotion.Update(timeSpan, direction); + else mMotion.LockUpdate(timeSpan, moveLeft, moveRight, moveUp, moveDown); } public void powerUp(int amount) diff --git a/CarFire/CarFire/CarFire/Projectile.cs b/CarFire/CarFire/CarFire/Projectile.cs index 611cc38..a1d7f27 100644 --- a/CarFire/CarFire/CarFire/Projectile.cs +++ b/CarFire/CarFire/CarFire/Projectile.cs @@ -70,7 +70,7 @@ namespace CarFire moveDown = true; else if (velocity.Y < 0) moveUp = true; - Point destination = MovementManager.GetNeighborCell(mMotion.Coordinates, moveLeft, moveRight, moveUp, moveDown); + Point destination = MovementManager.GetNeighbor(mMotion.Coordinates, moveLeft, moveRight, moveUp, moveDown); mMotion.Update(timeSpan, moveLeft, moveRight, moveUp, moveDown); -- 2.43.0