]> Dogcows Code - chaz/carfire/blobdiff - CarFire/CarFire/CarFire/MovementManager.cs
better player movement (walls are no longer sticky)
[chaz/carfire] / CarFire / CarFire / CarFire / MovementManager.cs
index e7002035b6509caec0f80f5e5cf980c21190f8d5..73caa5775b02eca70e009bff5874bf2946cfad13 100644 (file)
@@ -103,6 +103,24 @@ namespace CarFire
             Update(timeSpan, false, false, false, false);\r
         }\r
 \r
+        /// <summary>\r
+        /// Update the movement manager with the timeslice and a direction.\r
+        /// </summary>\r
+        /// <param name="timeSpan">The timeslice.</param>\r
+        /// <param name="direction">Direction you want to move.</param>\r
+        public void Update(TimeSpan timeSpan, Direction direction)\r
+        {\r
+            if (direction == Direction.Left) Update(timeSpan, true, false, false, false);\r
+            else if (direction == Direction.UpperLeft) Update(timeSpan, true, false, true, false);\r
+            else if (direction == Direction.Up) Update(timeSpan, false, false, true, false);\r
+            else if (direction == Direction.UpperRight) Update(timeSpan, false, true, true, false);\r
+            else if (direction == Direction.Right) Update(timeSpan, false, true, false, false);\r
+            else if (direction == Direction.LowerRight) Update(timeSpan, false, true, false, true);\r
+            else if (direction == Direction.Down) Update(timeSpan, false, false, false, true);\r
+            else if (direction == Direction.LowerLeft) Update(timeSpan, true, false, false, true);\r
+            else Update(timeSpan);\r
+        }\r
+\r
         /// <summary>\r
         /// Update the movement manager with the timeslice and the directions\r
         /// the object is supposed to go.  The directions will be ignored if the\r
@@ -185,7 +203,7 @@ namespace CarFire
         /// <param name="up">Above.</param>\r
         /// <param name="down">Below.</param>\r
         /// <returns>The neighbor cell coordinates.</returns>\r
-        public static Point GetNeighborCell(Point point, bool left, bool right, bool up, bool down)\r
+        public static Point GetNeighbor(Point point, bool left, bool right, bool up, bool down)\r
         {\r
             if (left) point.X--;\r
             if (right) point.X++;\r
@@ -194,6 +212,21 @@ namespace CarFire
             return point;\r
         }\r
 \r
+        /// <summary>\r
+        /// Helper method to get the two neighbor cells of two nearby cells.\r
+        /// </summary>\r
+        /// <param name="a">A point.</param>\r
+        /// <param name="b">Another point.</param>\r
+        /// <returns>An array of two points representing the neighbor cells.</returns>\r
+        public static Point[] GetNeighbors(Point a, Point b)\r
+        {\r
+            Point[] neighbors = new Point[2];\r
+            neighbors[0] = new Point(a.X, b.Y);\r
+            neighbors[1] = new Point(b.X, a.Y);\r
+            return neighbors;\r
+        }\r
+\r
+\r
         /// <summary>\r
         /// Helper method to get a Direction type from directions.\r
         /// </summary>\r
@@ -221,6 +254,34 @@ namespace CarFire
             else return Direction.None;\r
         }\r
 \r
+        /// <summary>\r
+        /// Helper method to get the general Direction type if you want to move\r
+        /// from one cell to another.\r
+        /// <param name="a">Starting point.</param>\r
+        /// <param name="b">Destination point.</param>\r
+        /// <returns>The direction toward the cell.</returns>\r
+        public static Direction GetDirection(Point a, Point b)\r
+        {\r
+            int dx = b.X - a.X;\r
+            int dy = b.Y - a.Y;\r
+\r
+            if (dx < 0)\r
+            {\r
+                if (dy < 0) return Direction.UpperLeft;\r
+                else if (dy > 0) return Direction.LowerLeft;\r
+                else return Direction.Left;\r
+            }\r
+            else if (dx > 0)\r
+            {\r
+                if (dy < 0) return Direction.UpperRight;\r
+                else if (dy > 0) return Direction.LowerRight;\r
+                else return Direction.Right;\r
+            }\r
+            else if (dy < 0) return Direction.Up;\r
+            else if (dy > 0) return Direction.Down;\r
+            else return Direction.None;\r
+        }\r
+\r
         #endregion\r
 \r
 \r
@@ -236,7 +297,7 @@ namespace CarFire
         void UpdateCoordinates(bool moveLeft, bool moveRight, bool moveUp, bool moveDown)\r
         {\r
             mLastCoordinates = mCoordinates;\r
-            mCoordinates = GetNeighborCell(mCoordinates, moveLeft, moveRight, moveUp, moveDown);\r
+            mCoordinates = GetNeighbor(mCoordinates, moveLeft, moveRight, moveUp, moveDown);\r
 \r
             if ((moveLeft && moveUp) || (moveUp && moveRight) || (moveRight && moveDown) || (moveDown && moveLeft))\r
             {\r
This page took 0.020404 seconds and 4 git commands to generate.