]> Dogcows Code - chaz/carfire/commitdiff
Modified Projectiles to work with MovementManager.
authorKyle <Kyle@92bb83a3-7c8f-8a45-bc97-515c4e399668>
Tue, 20 Apr 2010 00:33:22 +0000 (00:33 +0000)
committerKyle <Kyle@92bb83a3-7c8f-8a45-bc97-515c4e399668>
Tue, 20 Apr 2010 00:33:22 +0000 (00:33 +0000)
Projectiles can now be fired in all eight directions.

git-svn-id: https://bd85.net/svn/cs3505_group@119 92bb83a3-7c8f-8a45-bc97-515c4e399668

CarFire/CarFire/CarFire/Display.cs
CarFire/CarFire/CarFire/Human.cs
CarFire/CarFire/CarFire/MovementManager.cs
CarFire/CarFire/CarFire/Projectile.cs

index 9e06485af34a242af1bc86187e68eb02637eeba8..9fecb5bcbdcdc849f02b64de42e7275b478e2129 100644 (file)
@@ -99,7 +99,7 @@ namespace CarFire
             for (int i = 0; i < mProjectiles.Count; i++ )\r
             {\r
                 bool removed = false;\r
-                if (!mMap.IsCellOpen(new Point(mProjectiles[i].GridX, mProjectiles[i].GridY)))\r
+                if (!mMap.IsCellOpen(new Point(mProjectiles[i].Coordinates.X, mProjectiles[i].Coordinates.Y)))\r
                 {\r
                \r
                     mProjectiles.RemoveAt(i);\r
@@ -117,7 +117,7 @@ namespace CarFire
                 if(mCharacters[j] != null)\r
                 for (int i = 0; i < mProjectiles.Count; i++)\r
                 {\r
-                    if (mProjectiles[i].GridX == mCharacters[j].Coordinates.X && mProjectiles[i].GridY == mCharacters[j].Coordinates.Y)\r
+                    if (mProjectiles[i].Coordinates.X == mCharacters[j].Coordinates.X && mProjectiles[i].Coordinates.Y == mCharacters[j].Coordinates.Y)\r
                     {\r
                         mCharacters[j].causeDamageTo(mProjectiles[i].Damage);\r
                         Console.WriteLine(mCharacters[j].Health);\r
@@ -152,7 +152,7 @@ namespace CarFire
                 //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
+                if (!mMap.IsCellOpen(new Point(mProjectiles[i].Coordinates.X, mProjectiles[i].Coordinates.Y)))\r
                 {\r
                     mProjectiles.RemoveAt(i);\r
                     i--;\r
index 0d68d302d07c6d4b186a5d1d36d6c08317622ef1..8a94fd3822745dcac308baf3e3243d960b615b2f 100644 (file)
@@ -138,31 +138,63 @@ namespace CarFire
             {\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,\r
-                            new Vector2(0, -projectileSpeed), Coordinates.X, Coordinates.Y - 1));\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,\r
-                            new Vector2(0, projectileSpeed), Coordinates.X, Coordinates.Y + 1));\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,\r
-                            new Vector2(projectileSpeed, 0), Coordinates.X + 1, Coordinates.Y));\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,\r
-                            new Vector2(-projectileSpeed, 0), Coordinates.X - 1, Coordinates.Y));\r
+                            new Vector2(-projectileSpeed, 0), new Point(Coordinates.X - 1, Coordinates.Y)));\r
                     }\r
+                     */\r
                 }\r
             }\r
         }\r
index 512c2af203c5bcafc18adc48026d28961214f22d..3b8dae13a2d0983b3f3b0dfbc4f8f3be39f4da8e 100644 (file)
@@ -207,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
index 308e313d39dbbfae5fa5467d84f30f34b3d7bf54..3f5b1368466a9496318ae47a3b63a86d315ff388 100644 (file)
@@ -25,7 +25,9 @@ namespace CarFire
         //these will have to be transformed to the coordinate system that the drawable screen is using.\r
         int pixelX;\r
         int pixelY;\r
-   \r
+        MovementManager mMotion;\r
+        Point mPosition;\r
+\r
         /// <summary>\r
         /// The Constructor for a projectile object.\r
         /// </summary>\r
@@ -39,58 +41,34 @@ namespace CarFire
         public Projectile(Map _currentMap,\r
                             Texture2D _projectileModel,\r
                             Vector2 _velocity,\r
-                            int _gridX,\r
-                            int _gridY)\r
-                            /*,\r
-                            int _pixelX,\r
-                            int _pixelY)*/\r
+                            Point _position)\r
+                \r
         {\r
             theMap = _currentMap;\r
             projectileModel = _projectileModel;\r
             velocity = _velocity;\r
-            gridX = _gridX;\r
-            gridY = _gridY;\r
-            pixelX = _gridX * (int)Map.PixelsToUnitSquares;\r
-            pixelY = _gridY * (int)Map.PixelsToUnitSquares;\r
+            mPosition = _position;\r
             damage = 20;\r
+            // Speed is the number of grid cells you can move through per second.\r
+            mMotion = new MovementManager(mPosition, velocity.Length());\r
         }\r
-        public void Update(TimeSpan timespan)\r
+        public void Update(TimeSpan timeSpan)\r
         {\r
-            /*\r
-            //See if something moved onto this projectile.\r
-            if(theMap.isOpenSquare(gridX, gridY))\r
-            {\r
-                theMap.damageSquare(gridX, gridY, damage);\r
-            }\r
-             */\r
-            //If the projectile will be moving to a new grid position we need to check to see if it is occupied.\r
-            if ((int)((pixelX + velocity.X) / Map.PixelsToUnitSquares) != gridX || (int)((pixelY + velocity.Y) / Map.PixelsToUnitSquares) != gridY)\r
-            {\r
-                //bool open = theMap.IsCellOpen((int)((pixelX + velocity.X) /Map.PixelsToUnitSquares), (int)((pixelY + velocity.Y) / Map.PixelsToUnitSquares));\r
-                //If open just move this projectile there\r
-                //***Map doesn't need to know that this projectile is there because players/monsters are allowed\r
-                // to move into the path of projectiles.\r
-                //if (open)\r
-                {\r
-                //Console.WriteLine("pixelX:" + pixelX + " " + " pixelY: "+ pixelY);\r
-                    pixelX += (int)velocity.X;\r
-                    pixelY += (int)velocity.Y;\r
-                    gridX = (int)(pixelX /Map.PixelsToUnitSquares);\r
-                    gridY = (int)(pixelY / Map.PixelsToUnitSquares);\r
-                }\r
-                //If the square isn't open then just damage whatever is there\r
-                //TODO: A projectile must be deleted after this happens\r
-                //else\r
-                {\r
-                    //theMap.damageSquare(gridX, gridY, damage);\r
-                }\r
-            }\r
-            //If it is not moving grid positions just increment pixelX and pixelY\r
-            else\r
-            {\r
-                pixelX += (int)velocity.X;\r
-                pixelY += (int)velocity.Y;\r
-            }\r
+            bool moveRight = false;\r
+            bool moveLeft = false;\r
+            bool moveUp = false;\r
+            bool moveDown = false;\r
+            if (velocity.X > 0)\r
+                moveRight = true;\r
+            else if (velocity.X < 0)\r
+                moveLeft = true;\r
+            if (velocity.Y > 0)\r
+                moveDown = true;\r
+            else if (velocity.Y < 0)\r
+                moveUp = true;\r
+            Point destination = MovementManager.GetNeighborCell(mMotion.Coordinates, moveLeft, moveRight, moveUp, moveDown);\r
+            mMotion.Update(timeSpan, moveLeft, moveRight, moveUp, moveDown);\r
+            \r
             \r
         }\r
         /// <summary>\r
@@ -101,13 +79,9 @@ namespace CarFire
         /// <param name="topLeftY">The map Y pixel position of the topLeft of the display</param>\r
         public void Draw(SpriteBatch spriteBatch)\r
         {\r
-            //Console.WriteLine(gridX + " " + gridY);\r
-            //Console.WriteLine(theMap.GetRectangleFromCoordinates(new Vector2(gridX, gridY)));\r
-            Rectangle position = theMap.GetRectangleFromCoordinates(new Vector2(gridX, gridY));\r
-            //spriteBatch.Draw(projectileModel, position, Color.White);\r
-            Rectangle position2 = new Rectangle((int)(pixelX), (int)(pixelY), (int)Map.PixelsToUnitSquares, (int)Map.PixelsToUnitSquares);\r
-            Rectangle position3 = theMap.GetRectangleFromCoordinates(new Vector2(pixelX / Map.PixelsToUnitSquares, pixelY / Map.PixelsToUnitSquares));\r
-            spriteBatch.Draw(projectileModel, position3, Color.White);\r
+            Rectangle position = theMap.GetRectangleFromCoordinates(mMotion.Position);\r
+            spriteBatch.Draw(projectileModel, position, Color.White);\r
+         \r
         }\r
 \r
         /// <summary>\r
@@ -119,5 +93,7 @@ namespace CarFire
         public int PixelY { get { return pixelY; } set { pixelY = value; } }\r
         public Map TheMap { get { return theMap; } set { theMap = value; } }\r
         public int Damage { get { return damage; } set { damage = value; } }\r
+        public Vector2 Position { get { return mMotion.Position; } }\r
+        public Point Coordinates { get { return mMotion.Coordinates; } }\r
     }\r
 }\r
This page took 0.034893 seconds and 4 git commands to generate.