Smooth player movement... still has some issues.
authorKyle <Kyle@92bb83a3-7c8f-8a45-bc97-515c4e399668>
Mon, 19 Apr 2010 06:55:54 +0000 (06:55 +0000)
committerKyle <Kyle@92bb83a3-7c8f-8a45-bc97-515c4e399668>
Mon, 19 Apr 2010 06:55:54 +0000 (06:55 +0000)
git-svn-id: https://bd85.net/svn/cs3505_group@110 92bb83a3-7c8f-8a45-bc97-515c4e399668

CarFire/CarFire/CarFire/Display.cs
CarFire/CarFire/CarFire/Human.cs
CarFire/CarFire/CarFire/IPlayer.cs

index fc2489530e8f53caea9de9b88f5f8ff99be9fb78..d8f9a6bd9a9f78c24d1f42fe053d611218db4bb2 100644 (file)
@@ -51,22 +51,6 @@ namespace CarFire
             mMap = contentManager.Load<Map>("Maps/stable");\r
             Map.DefaultTile = contentManager.Load<Texture2D>("default");\r
             mMap.CenterCell = new Vector2(currentCenterX,currentCenterY);\r
-            //Debugging... Spawn eight projectiles.\r
-            //Diagonals\r
-            /*\r
-            mProjectiles.Add(new Projectile(mMap, everything, new Vector2(5,5), 10, 10, 300, 300));\r
-            mProjectiles.Add(new Projectile(mMap, everything, new Vector2(-5, 5), 10, 10, 300, 300));\r
-            mProjectiles.Add(new Projectile(mMap, everything, new Vector2(5, -5), 10, 10, 300, 300));\r
-            mProjectiles.Add(new Projectile(mMap, everything, new Vector2(-5, -5), 10, 10, 300, 300));\r
-            //Vertical and horizontal\r
-            mProjectiles.Add(new Projectile(mMap, everything, new Vector2(0, 5), 10, 10, 300, 300));\r
-            mProjectiles.Add(new Projectile(mMap, everything, new Vector2(-5, 0), 10, 10, 300, 300));\r
-            mProjectiles.Add(new Projectile(mMap, everything, new Vector2(5, 0), 10, 10, 300, 300));\r
-            mProjectiles.Add(new Projectile(mMap, everything, new Vector2(0, -5), 10, 10, 300, 300));\r
-            */\r
-\r
-            \r
-            // TODO: use this.Content to load your game content here\r
         }\r
 \r
         /// <summary>\r
@@ -160,10 +144,14 @@ namespace CarFire
                 {\r
 \r
                     mCharacters[i].MovePlayer(state.keysDown[i]);\r
-                    mMap.CenterCell = new Vector2(mCharacters[0].GridX, mCharacters[0].GridY);\r
+                   \r
                 }\r
             }\r
-            //Handle wall collisions of projectiles again...\r
+            if (mCharacters[0] != null)\r
+            {\r
+                mMap.CenterCell = new Vector2(mCharacters[0].PixelX / Map.PixelsToUnitSquares, mCharacters[0].PixelY / Map.PixelsToUnitSquares);\r
+            }\r
+                //Handle wall collisions of projectiles again...\r
             for (int i = 0; i < mProjectiles.Count; i++)\r
             {\r
                 if (!mMap.IsCellOpen(new Point(mProjectiles[i].GridX, mProjectiles[i].GridY)))\r
@@ -190,13 +178,26 @@ namespace CarFire
             foreach(Projectile projectile in mProjectiles)\r
             {\r
                 projectile.Draw(spriteBatch);\r
-                \r
             }\r
             for(int i = 0; i < 4; i++)//IPlayer character in mCharacters)\r
             {\r
-                if(mCharacters[i] != null)\r
+\r
+                if (mCharacters[i] != null)\r
+                {\r
                     mCharacters[i].Draw(spriteBatch);\r
-            } \r
+                    if (Math.Abs(mCharacters[i].GridX - mCharacters[i].PixelX / Map.PixelsToUnitSquares) > 0.7f || 0.7f < Math.Abs(mCharacters[i].GridY - mCharacters[i].PixelY / Map.PixelsToUnitSquares))\r
+                    {\r
+                        //Console.WriteLine(Math.Abs(mCharacters[i].GridX - mCharacters[i].PixelX / Map.PixelsToUnitSquares));\r
+                        //Console.WriteLine(Math.Abs(mCharacters[i].GridY - mCharacters[i].PixelY / Map.PixelsToUnitSquares));\r
+                        Console.WriteLine("Begin");\r
+                        Console.WriteLine(mCharacters[i].GridX);\r
+                        Console.WriteLine(mCharacters[i].GridY);\r
+                        Console.WriteLine(mCharacters[i].PixelX / Map.PixelsToUnitSquares);\r
+                        Console.WriteLine(mCharacters[i].PixelY / Map.PixelsToUnitSquares);\r
+                    }\r
+                }\r
+            }\r
+            \r
         }\r
         /// <summary>\r
         /// Add a projectile to the Display.\r
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
index 3b9830148e0f4ec13d7de0e81909194120b68233..b60ef1a23aa3d784dd3b7c4a7b45c102e976a353 100644 (file)
@@ -19,6 +19,9 @@ namespace CarFire
         void causeDamageTo(int amount);\r
         int GridX { get; set; }\r
         int GridY { get; set; }\r
+        float PixelX { get; }\r
+        float PixelY { get; }\r
+        bool IsMoving { get; }\r
     }\r
 \r
     public interface IPlayer : ICharacter\r
This page took 0.037375 seconds and 4 git commands to generate.