]> Dogcows Code - chaz/carfire/blobdiff - CarFire/CarFire/CarFire/MovementManager.cs
Reseting the map seems to be working now.
[chaz/carfire] / CarFire / CarFire / CarFire / MovementManager.cs
index 372be6c23deab452650958f305729077de272b4c..ad26e478a317f8921f9b14e512f97f9df2690e42 100644 (file)
@@ -6,6 +6,23 @@ using Microsoft.Xna.Framework;
 \r
 namespace CarFire\r
 {\r
+    /// <summary>\r
+    /// A type for a direction, including diagonals.\r
+    /// </summary>\r
+    public enum Direction\r
+    {\r
+        Down,           // Default direction is down.\r
+        Left,\r
+        UpperLeft,\r
+        Up,\r
+        UpperRight,\r
+        Right,\r
+        LowerRight,\r
+        LowerLeft,\r
+        None\r
+    }\r
+\r
+\r
     /// <summary>\r
     /// A class to manage the motion of objects on a grid of cells.\r
     /// Each update you can pass a direction and the manager will move\r
@@ -27,13 +44,23 @@ namespace CarFire
         /// Get the grid coordinates where the object is at or\r
         /// is moving to.\r
         /// </summary>\r
-        public Point Coordinates { get { return mCoordinates; } }\r
+        public Point Coordinates { get { return mCoordinates; } set { mCoordinates = value; } }\r
 \r
         /// <summary>\r
         /// Get and set the speed of movement in grid cells / second.\r
         /// </summary>\r
         public float Speed;\r
 \r
+        /// <summary>\r
+        /// Get whether or not the object is moving.\r
+        /// </summary>\r
+        public bool IsMoving { get { return mIsMoving; } }\r
+\r
+        /// <summary>\r
+        /// Get the direction the object is facing.\r
+        /// </summary>\r
+        public Direction Direction { get { return mDirection; } }\r
+\r
         #endregion\r
 \r
 \r
@@ -76,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
@@ -93,10 +138,11 @@ namespace CarFire
             bool requestMove = (moveLeft ^ moveRight) || (moveUp ^ moveDown);\r
             if (!mIsMoving && requestMove)\r
             {\r
-                UpdateCoordinates(moveLeft, moveRight, moveUp, moveDown);\r
-\r
-                mIsMoving = true;\r
                 mTimeAccumulator = passedTime;\r
+                \r
+                mIsMoving = true;\r
+                UpdateCoordinates(moveLeft, moveRight, moveUp, moveDown);\r
+                mDirection = GetDirection(moveLeft, moveRight, moveUp, moveDown);\r
 \r
                 RecalculatePosition(mTimeAccumulator / mInverseSpeed);\r
             }\r
@@ -113,6 +159,7 @@ namespace CarFire
                         alpha = mTimeAccumulator / mInverseSpeed;\r
 \r
                         UpdateCoordinates(moveLeft, moveRight, moveUp, moveDown);\r
+                        mDirection = GetDirection(moveLeft, moveRight, moveUp, moveDown);\r
                     }\r
                     else\r
                     {\r
@@ -124,10 +171,31 @@ namespace CarFire
                 RecalculatePosition(alpha);\r
             }\r
         }\r
+        public void LockUpdate(TimeSpan timeSpan, bool moveLeft, bool moveRight, bool moveUp, bool moveDown)\r
+        {\r
+            float passedTime = (float)timeSpan.TotalSeconds;\r
+            if (moveLeft == true || moveRight == true || moveUp == true || moveDown == true)\r
+            {\r
+                mDirection = GetDirection(moveLeft, moveRight, moveUp, moveDown);\r
+            }\r
+            if (mIsMoving)\r
+            {\r
+                mTimeAccumulator += passedTime;\r
+\r
+                float alpha = mTimeAccumulator / mInverseSpeed;\r
+                if (alpha >= 1.0f)\r
+                {\r
+                    mIsMoving = false;\r
+                    alpha = 1.0f;\r
+                }\r
+\r
+                RecalculatePosition(alpha);\r
+            }\r
+        }\r
 \r
 \r
         /// <summary>\r
-        /// Helper method to get neighbor cells from a point and directions.\r
+        /// Helper method to get a neighbor cell from a point and directions.\r
         /// </summary>\r
         /// <param name="point">The point.</param>\r
         /// <param name="left">To the left.</param>\r
@@ -135,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
@@ -144,6 +212,98 @@ namespace CarFire
             return point;\r
         }\r
 \r
+        /// <summary>\r
+        /// Helper method to get a neighbor cell from a point and a direction.\r
+        /// </summary>\r
+        /// <param name="point">The point.</param>\r
+        /// <param name="direction">The direction.</param>\r
+        /// <returns>The neighbor cell coordinates.</returns>\r
+        public static Point GetNeighbor(Point point, Direction direction)\r
+        {\r
+            switch (direction)\r
+            {\r
+                case Direction.Left: return new Point(point.X - 1, point.Y);\r
+                case Direction.UpperLeft: return new Point(point.X - 1, point.Y - 1);\r
+                case Direction.Up: return new Point(point.X, point.Y - 1);\r
+                case Direction.UpperRight: return new Point(point.X + 1, point.Y - 1);\r
+                case Direction.Right: return new Point(point.X + 1, point.Y);\r
+                case Direction.LowerRight: return new Point(point.X + 1, point.Y + 1);\r
+                case Direction.Down: return new Point(point.X, point.Y + 1);\r
+                case Direction.LowerLeft: return new Point(point.X - 1, point.Y + 1);\r
+            }\r
+            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
+        /// <param name="left">Left.</param>\r
+        /// <param name="right">Right.</param>\r
+        /// <param name="up">Up.</param>\r
+        /// <param name="down">Down.</param>\r
+        /// <returns>The direction.</returns>\r
+        public static Direction GetDirection(bool left, bool right, bool up, bool down)\r
+        {\r
+            if (left && !right)\r
+            {\r
+                if (up) return Direction.UpperLeft;\r
+                else if (down) return Direction.LowerLeft;\r
+                else return Direction.Left;\r
+            }\r
+            else if (right && !left)\r
+            {\r
+                if (up) return Direction.UpperRight;\r
+                else if (down) return Direction.LowerRight;\r
+                else return Direction.Right;\r
+            }\r
+            else if (up) return Direction.Up;\r
+            else if (down) return Direction.Down;\r
+            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
@@ -151,7 +311,7 @@ namespace CarFire
 \r
         void RecalculatePosition(float alpha)\r
         {\r
-            Console.WriteLine("last: " + mLastCoordinates + ", now: " + mCoordinates + ", alpha: " + alpha);\r
+            //Console.WriteLine("last: " + mLastCoordinates + ", now: " + mCoordinates + ", alpha: " + alpha);\r
             mPosition.X = (float)mLastCoordinates.X + alpha * ((float)mCoordinates.X - (float)mLastCoordinates.X);\r
             mPosition.Y = (float)mLastCoordinates.Y + alpha * ((float)mCoordinates.Y - (float)mLastCoordinates.Y);\r
         }\r
@@ -159,7 +319,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
@@ -182,6 +342,7 @@ namespace CarFire
         float mInverseSpeed;            // The time it takes to move from one cell to another.\r
         float mTimeAccumulator;         // Amount of time passed since last move.\r
         bool mIsMoving;                 // Whether or not it is currently in the process of moving.\r
+        Direction mDirection;           // The direction the object is facing.\r
 \r
         #endregion\r
     }\r
This page took 0.023814 seconds and 4 git commands to generate.