]> Dogcows Code - chaz/carfire/blobdiff - CarFire/CarFire/CarFire/MovementManager.cs
Player can now stop and 'aim' by pressing left control.
[chaz/carfire] / CarFire / CarFire / CarFire / MovementManager.cs
index 372be6c23deab452650958f305729077de272b4c..e7002035b6509caec0f80f5e5cf980c21190f8d5 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
@@ -124,6 +153,27 @@ 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
@@ -144,6 +194,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 +228,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 +259,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.021352 seconds and 4 git commands to generate.