]> Dogcows Code - chaz/carfire/blobdiff - CarFire/CarFire/CarFire/MovementManager.cs
Modified Projectiles to work with MovementManager.
[chaz/carfire] / CarFire / CarFire / CarFire / MovementManager.cs
index 372be6c23deab452650958f305729077de272b4c..3b8dae13a2d0983b3f3b0dfbc4f8f3be39f4da8e 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
@@ -34,6 +51,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
@@ -93,10 +120,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 +141,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 +173,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
@@ -151,7 +207,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
@@ -182,6 +238,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.025226 seconds and 4 git commands to generate.