]> Dogcows Code - chaz/carfire/blobdiff - CarFire/CarFire/CarFire/MovementManager.cs
New MovementManager properties (IsMoving and Direction) the animation class will...
[chaz/carfire] / CarFire / CarFire / CarFire / MovementManager.cs
index 372be6c23deab452650958f305729077de272b4c..befeb4b62828f6bac4c74ee24df02c35abafaef8 100644 (file)
@@ -6,6 +6,22 @@ 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
+    }\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
@@ -34,6 +50,16 @@ namespace CarFire
         /// </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
@@ -91,16 +117,17 @@ namespace CarFire
             float passedTime = (float)timeSpan.TotalSeconds;\r
 \r
             bool requestMove = (moveLeft ^ moveRight) || (moveUp ^ moveDown);\r
-            if (!mIsMoving && requestMove)\r
+            if (!IsMoving && 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
-            else if (mIsMoving)\r
+            else if (IsMoving)\r
             {\r
                 mTimeAccumulator += passedTime;\r
 \r
@@ -113,6 +140,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
@@ -144,6 +172,33 @@ namespace CarFire
             return point;\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
         #endregion\r
 \r
 \r
@@ -181,7 +236,8 @@ namespace CarFire
         Point mLastCoordinates;         // Last position on the grid.\r
         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
+        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.019646 seconds and 4 git commands to generate.