]> Dogcows Code - chaz/carfire/blobdiff - CarFire/CarFire/CarFire/Human.cs
Player can now stop and 'aim' by pressing left control.
[chaz/carfire] / CarFire / CarFire / CarFire / Human.cs
index 07b4222f97a6fff7c3a0b36072ea9d27b0a24110..519d9d02f14908767e150112472c01d7638d6bd1 100644 (file)
@@ -18,30 +18,19 @@ namespace CarFire
             up,\r
             down\r
         };\r
-        //The number of frames between each square movements\r
-        const int moveCoolDown = 15;\r
         //The number of frames between each projectile is spawned.\r
         const int shootCoolDown = 10;\r
         State state;\r
         String CharName;\r
         Map theMap;\r
-        int gridX;\r
-        int gridY;\r
         Texture2D charModel;\r
         Texture2D projectileModel;\r
         int health;\r
         int damage;\r
         int range;\r
         int score;\r
-        \r
-        //Used to smooth animations\r
-        bool isMoving;\r
-        float pixelX;\r
-        float pixelY;\r
-        int movementSteps;\r
-        int movementCoolDown;\r
-        float changeX;\r
-        float changeY;\r
+\r
+        MovementManager mMotion;\r
 \r
         bool visible;\r
         Display theDisplay;\r
@@ -51,7 +40,7 @@ namespace CarFire
         int projectileCoolDown;\r
         \r
 \r
-        public Human(Map _theMap, String Name, Texture2D model, Texture2D projectile, Display mDisplay)\r
+        public Human(Map _theMap, String Name, Texture2D model, Texture2D projectile, Display mDisplay, Point position)\r
         {\r
             theMap = _theMap;\r
             CharName = Name;\r
@@ -64,9 +53,9 @@ namespace CarFire
             charModel = model;\r
             projectileModel = projectile;\r
             projectileSpeed = 30;\r
-            //The number of animation steps between each square movement.\r
-            movementSteps = moveCoolDown -2;\r
-            \r
+\r
+            // Speed is the number of grid cells you can move through per second.\r
+            mMotion = new MovementManager(position, 5.0f);\r
         }\r
 \r
         public void LoadContent(ContentManager contentManager)\r
@@ -93,38 +82,18 @@ namespace CarFire
         /// <returns></returns>\r
         public long Draw(SpriteBatch spriteBatch)\r
         {\r
-            //If the sprite is moving there are still movement animations to do.\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
-            // If the sprite is not moving then just draw it.\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
+            Rectangle position = theMap.GetRectangleFromCoordinates(mMotion.Position);\r
+            spriteBatch.Draw(charModel, position, Color.White);\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
+        public Vector2 Position { get { return mMotion.Position; } }\r
+        public Point Coordinates { get { return mMotion.Coordinates; } }\r
+\r
         public void causeDamageTo(int amount)\r
         {\r
             health -= amount;\r
@@ -137,131 +106,102 @@ namespace CarFire
         /// that is being moved to is an open space.\r
         /// </summary>\r
         /// <param name="keysPressed">A general list of keys that are pressed. Other keys can be included but only direction keys will be used</param>\r
-        public void MovePlayer(List<Keys> keysPressed)\r
+        public void MovePlayer(TimeSpan timeSpan, List<Keys> keysPressed)\r
         {\r
-            if(movementCoolDown > 0)\r
-                movementCoolDown--;\r
-            else if (movementCoolDown == 0)\r
+            bool moveLeft = keysPressed.Contains(Keys.Left);\r
+            bool moveRight = keysPressed.Contains(Keys.Right);\r
+            bool moveUp = keysPressed.Contains(Keys.Up);\r
+            bool moveDown = keysPressed.Contains(Keys.Down);\r
+            if (moveLeft)\r
+                state = State.left;\r
+            else if (moveRight)\r
+                state = State.right;\r
+            else if (moveUp)\r
+                state = State.up;\r
+            else if (moveDown)\r
+                state = State.down;\r
+\r
+            Point destination = MovementManager.GetNeighborCell(mMotion.Coordinates, moveLeft, moveRight, moveUp, moveDown);\r
+            if (!keysPressed.Contains(Keys.LeftControl))\r
             {\r
-                // move upleft\r
-                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
-                  \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
-                    \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
-                    \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
-                    \r
-                }\r
-                // move up\r
-                else if (keysPressed.Contains<Keys>(Keys.Up) && theMap.IsCellOpen(gridX, gridY - 1))\r
+                if (theMap.IsCellOpen(destination))\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
-                    changeY = (float)(-Map.PixelsToUnitSquares / moveCoolDown);\r
+                    mMotion.Update(timeSpan, moveLeft, moveRight, moveUp, moveDown);\r
                 }\r
-                // move down\r
-                else if (keysPressed.Contains<Keys>(Keys.Down) && theMap.IsCellOpen(gridX, gridY + 1))\r
+                else\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
-                    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
-                    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
-                    changeX = (float)(Map.PixelsToUnitSquares / moveCoolDown);\r
+                    mMotion.Update(timeSpan);\r
                 }\r
             }\r
+            else\r
+            {\r
+                mMotion.LockUpdate(timeSpan, moveLeft, moveRight, moveUp, moveDown);\r
+            }\r
+\r
+\r
             if (projectileCoolDown > 0)\r
                 projectileCoolDown--;\r
             else if (projectileCoolDown == 0)\r
             {\r
                 if (keysPressed.Contains<Keys>(Keys.Space))\r
                 {\r
-                    //TODO spawn projectile... needs to be added to display though\r
+                    float velocityX = 0;\r
+                    float velocityY = 0;\r
+                    int startX = Coordinates.X;\r
+                    int startY = Coordinates.Y;\r
+                    if (mMotion.Direction == Direction.Down || mMotion.Direction == Direction.LowerLeft || mMotion.Direction == Direction.LowerRight)\r
+                    {\r
+                        velocityY = 1;\r
+                        startY = Coordinates.Y + 1;\r
+                    }\r
+                    else if (mMotion.Direction == Direction.Up || mMotion.Direction == Direction.UpperLeft || mMotion.Direction == Direction.UpperRight)\r
+                    {\r
+                        velocityY = -1;\r
+                        startY = Coordinates.Y - 1;\r
+                    }\r
+                    if (mMotion.Direction == Direction.Right || mMotion.Direction == Direction.LowerRight || mMotion.Direction == Direction.UpperRight)\r
+                    {\r
+                        velocityX = 1;\r
+                        startX = Coordinates.X + 1;\r
+                    }\r
+                    else if (mMotion.Direction == Direction.Left || mMotion.Direction == Direction.LowerLeft || mMotion.Direction == Direction.UpperLeft)\r
+                    {\r
+                        velocityX = -1;\r
+                        startX = Coordinates.X - 1;\r
+                    }\r
+                    Vector2 toShoot = new Vector2(velocityX, velocityY);\r
+                    toShoot.Normalize();\r
+                    toShoot *= projectileSpeed;\r
+                    projectileCoolDown = shootCoolDown;\r
+                    theDisplay.AddProjectiles(new Projectile(theMap, projectileModel,\r
+                        toShoot, new Point(startX, startY)));\r
+\r
+                    /*\r
                     if (state == State.up)\r
                     {\r
                         projectileCoolDown = shootCoolDown;\r
-                        theDisplay.AddProjectiles(new Projectile(theMap, projectileModel, new Vector2(0, -projectileSpeed), GridX, GridY - 1));\r
+                        theDisplay.AddProjectiles(new Projectile(theMap, projectileModel,\r
+                            new Vector2(0, -projectileSpeed), new Point(Coordinates.X, Coordinates.Y - 1)));\r
                     }\r
                     if (state == State.down)\r
                     {\r
                         projectileCoolDown = shootCoolDown;\r
-                        theDisplay.AddProjectiles(new Projectile(theMap, projectileModel, new Vector2(0, projectileSpeed), GridX, GridY + 1));\r
+                        theDisplay.AddProjectiles(new Projectile(theMap, projectileModel,\r
+                            new Vector2(0, projectileSpeed), new Point(Coordinates.X, Coordinates.Y + 1)));\r
                     }\r
                     if (state == State.right)\r
                     {\r
                         projectileCoolDown = shootCoolDown;\r
-                        theDisplay.AddProjectiles(new Projectile(theMap, projectileModel, new Vector2(projectileSpeed, 0), GridX + 1, GridY));\r
+                        theDisplay.AddProjectiles(new Projectile(theMap, projectileModel,\r
+                            new Vector2(projectileSpeed, 0), new Point (Coordinates.X + 1, Coordinates.Y)));\r
                     }\r
                     if (state == State.left)\r
                     {\r
                         projectileCoolDown = shootCoolDown;\r
-                        theDisplay.AddProjectiles(new Projectile(theMap, projectileModel, new Vector2(-projectileSpeed, 0), GridX - 1, GridY));\r
+                        theDisplay.AddProjectiles(new Projectile(theMap, projectileModel,\r
+                            new Vector2(-projectileSpeed, 0), new Point(Coordinates.X - 1, Coordinates.Y)));\r
                     }\r
+                     */\r
                 }\r
             }\r
         }\r
@@ -274,8 +214,8 @@ namespace CarFire
 \r
         public void Spawn(Vector2 spawn)\r
         {\r
-            gridX = (int)spawn.X;\r
-            gridY = (int)spawn.Y;\r
+            //gridX = (int)spawn.X;\r
+            //gridY = (int)spawn.Y;\r
             visible = true;\r
         }\r
 \r
This page took 0.03067 seconds and 4 git commands to generate.