]> Dogcows Code - chaz/carfire/blobdiff - CarFire/CarFire/CarFire/Human.cs
Smooth player movement... still has some issues.
[chaz/carfire] / CarFire / CarFire / CarFire / Human.cs
index 9350d8605e099e92c6c4afecf1e4e7cd27292468..8c94c297de9152bcdf999c197f7b7b717ef45751 100644 (file)
@@ -18,7 +18,7 @@ namespace CarFire
             up,\r
             down\r
         };\r
-        const int moveCoolDown = 12;\r
+        const int moveCoolDown = 15;\r
         const int shootCoolDown = 10;\r
         //Member Variables\r
         State state;\r
@@ -33,14 +33,25 @@ namespace CarFire
         int damage;\r
         int range;\r
         int score;\r
+        \r
+        //Used to smooth animations\r
         bool isMoving;\r
-        int pixelX;\r
-        int pixelY;\r
-        bool visible;\r
+        float pixelX;\r
+        float pixelY;\r
+        float nextPixelX;\r
+        float nextPixelY;\r
+        int movementSteps;\r
         int movementCoolDown;\r
+        float changeX;\r
+        float changeY;\r
+\r
+        bool visible;\r
         Display theDisplay;\r
+\r
+        //Used to draw projectiles\r
         int projectileSpeed;\r
         int projectileCoolDown;\r
+        \r
 \r
         public Human(Map _theMap, String Name, Texture2D model, Texture2D projectile, Display mDisplay)\r
         {\r
@@ -57,6 +68,7 @@ namespace CarFire
             charModel = model;\r
             projectileModel = projectile;\r
             projectileSpeed = 30;\r
+            movementSteps = moveCoolDown -2;\r
             \r
         }\r
 \r
@@ -80,13 +92,33 @@ namespace CarFire
 \r
         public long Draw(SpriteBatch spriteBatch)\r
         {\r
-            spriteBatch.Draw(charModel, theMap.GetRectangleFromCoordinates(gridX, gridY), Color.White);\r
+            if (isMoving && movementSteps > 0)\r
+            {\r
+                movementSteps--;\r
+                pixelX = pixelX + changeX;\r
+                pixelY = pixelY + changeY;\r
+                Rectangle position3 = theMap.GetRectangleFromCoordinates(new Vector2(pixelX / Map.PixelsToUnitSquares, pixelY / Map.PixelsToUnitSquares));\r
+                spriteBatch.Draw(charModel, position3, Color.White);\r
+            }\r
+            else\r
+            {\r
+                pixelX = gridX * Map.PixelsToUnitSquares;\r
+                pixelY = gridY * Map.PixelsToUnitSquares;\r
+                changeX = 0;\r
+                changeY = 0;\r
+                isMoving = false;\r
+                movementSteps = moveCoolDown - 2;\r
+                spriteBatch.Draw(charModel, theMap.GetRectangleFromCoordinates(gridX, gridY), Color.White);\r
+            }\r
             return 0;\r
         }\r
 \r
         public int GridX { get { return gridX; } set { gridX = value; } }\r
         public int GridY { get { return gridY; } set { gridY = value; } }\r
+        public float PixelX { get { return pixelX; } }\r
+        public float PixelY { get { return pixelY; } }\r
         public int Health { get { return health; } }\r
+        public bool IsMoving { get { return isMoving; } }\r
         public int Score { get { return score; } }\r
         public bool alive { get { return health > 0; } }\r
 \r
@@ -112,58 +144,104 @@ namespace CarFire
                 keysPressed.Contains<Keys>(Keys.Left);\r
                 if (keysPressed.Contains<Keys>(Keys.Up) && keysPressed.Contains<Keys>(Keys.Left) && theMap.IsCellOpen(gridX - 1, gridY - 1))\r
                 {\r
+                    pixelX = (float)(gridX * Map.PixelsToUnitSquares);\r
+                    pixelY = (float)(gridY * Map.PixelsToUnitSquares);\r
                     gridX -= 1;\r
                     gridY -= 1;\r
                     movementCoolDown = moveCoolDown;\r
+                    isMoving = true;\r
+                    nextPixelX = (float)(gridX * Map.PixelsToUnitSquares);\r
+                    nextPixelY = (float)(gridY * Map.PixelsToUnitSquares);\r
+                    \r
                 }\r
                 // move upright\r
                 else if (keysPressed.Contains<Keys>(Keys.Up) && keysPressed.Contains<Keys>(Keys.Right) && theMap.IsCellOpen(gridX + 1, gridY - 1))\r
                 {\r
+                    pixelX = (float)(gridX * Map.PixelsToUnitSquares);\r
+                    pixelY = (float)(gridY * Map.PixelsToUnitSquares);\r
                     gridX += 1;\r
                     gridY -= 1;\r
                     movementCoolDown = moveCoolDown;\r
+                    isMoving = true;\r
+                    nextPixelX = (float)(gridX * Map.PixelsToUnitSquares);\r
+                    nextPixelY = (float)(gridY * Map.PixelsToUnitSquares);\r
+                    \r
                 }\r
                 // move downleft\r
                 else if (keysPressed.Contains<Keys>(Keys.Down) && keysPressed.Contains<Keys>(Keys.Left) && theMap.IsCellOpen(gridX - 1, gridY + 1))\r
                 {\r
+                    pixelX = (float)(gridX * Map.PixelsToUnitSquares);\r
+                    pixelY = (float)(gridY * Map.PixelsToUnitSquares);\r
                     gridX -= 1;\r
                     gridY += 1;\r
                     movementCoolDown = moveCoolDown;\r
+                    isMoving = true;\r
+                    nextPixelX = (float)(gridX * Map.PixelsToUnitSquares);\r
+                    nextPixelY = (float)(gridY * Map.PixelsToUnitSquares);\r
                 }\r
                 // move downright\r
                 else if (keysPressed.Contains<Keys>(Keys.Down) && keysPressed.Contains<Keys>(Keys.Right) && theMap.IsCellOpen(gridX + 1, gridY + 1))\r
                 {\r
+                    pixelX = (float)(gridX * Map.PixelsToUnitSquares);\r
+                    pixelY = (float)(gridY * Map.PixelsToUnitSquares);\r
                     gridX += 1;\r
                     gridY += 1;\r
                     movementCoolDown = moveCoolDown;\r
+                    isMoving = true;\r
+                    nextPixelX = (float)(gridX * Map.PixelsToUnitSquares);\r
+                    nextPixelY = (float)(gridY * Map.PixelsToUnitSquares);\r
                 }\r
                 // move up\r
                 else if (keysPressed.Contains<Keys>(Keys.Up) && theMap.IsCellOpen(gridX, gridY - 1))\r
                 {\r
+                    pixelX = (float)(gridX * Map.PixelsToUnitSquares);\r
+                    pixelY = (float)(gridY * Map.PixelsToUnitSquares);\r
                     state = State.up;\r
                     gridY -= 1;\r
                     movementCoolDown = moveCoolDown;\r
+                    isMoving = true;\r
+                    nextPixelX = (float)(gridX * Map.PixelsToUnitSquares);\r
+                    nextPixelY = (float)(gridY * Map.PixelsToUnitSquares);\r
+                    changeY = (float)(-Map.PixelsToUnitSquares / moveCoolDown);\r
                 }\r
                 // move down\r
                 else if (keysPressed.Contains<Keys>(Keys.Down) && theMap.IsCellOpen(gridX, gridY + 1))\r
                 {\r
+                    pixelX = (float)(gridX * Map.PixelsToUnitSquares);\r
+                    pixelY = (float)(gridY * Map.PixelsToUnitSquares);\r
                     state = State.down;\r
                     gridY += 1;\r
                     movementCoolDown = moveCoolDown;\r
+                    isMoving = true;\r
+                    nextPixelX = (float)(gridX * Map.PixelsToUnitSquares);\r
+                    nextPixelY = (float)(gridY * Map.PixelsToUnitSquares);\r
+                    changeY = (float)(Map.PixelsToUnitSquares / moveCoolDown);\r
                 }\r
                 // move left\r
                 else if (keysPressed.Contains<Keys>(Keys.Left) && theMap.IsCellOpen(gridX - 1, gridY))\r
                 {\r
+                    pixelX = (float)(gridX * Map.PixelsToUnitSquares);\r
+                    pixelY = (float)(gridY * Map.PixelsToUnitSquares);\r
                     state = State.left;\r
                     gridX -= 1;\r
                     movementCoolDown = moveCoolDown;\r
+                    isMoving = true;\r
+                    nextPixelX = (float)(gridX * Map.PixelsToUnitSquares);\r
+                    nextPixelY = (float)(gridY * Map.PixelsToUnitSquares);\r
+                    changeX = (float)(-Map.PixelsToUnitSquares / moveCoolDown);\r
                 }\r
                 // move right\r
                 else if (keysPressed.Contains<Keys>(Keys.Right) && theMap.IsCellOpen(gridX + 1, gridY))\r
                 {\r
+                    pixelX = (float)(gridX * Map.PixelsToUnitSquares);\r
+                    pixelY = (float)(gridY * Map.PixelsToUnitSquares);\r
                     state = State.right;\r
                     gridX += 1;\r
                     movementCoolDown = moveCoolDown;\r
+                    isMoving = true;\r
+                    nextPixelX = (float)(gridX * Map.PixelsToUnitSquares);\r
+                    nextPixelY = (float)(gridY * Map.PixelsToUnitSquares);\r
+                    changeX = (float)(Map.PixelsToUnitSquares / moveCoolDown);\r
                 }\r
             }\r
             if (projectileCoolDown > 0)\r
This page took 0.022425 seconds and 4 git commands to generate.