From: Kyle Date: Tue, 20 Apr 2010 00:33:22 +0000 (+0000) Subject: Modified Projectiles to work with MovementManager. X-Git-Url: https://git.brokenzipper.com/gitweb?a=commitdiff_plain;h=b32b73746f5bf95771eb5f9db95763ce2a44049f;p=chaz%2Fcarfire Modified Projectiles to work with MovementManager. Projectiles can now be fired in all eight directions. git-svn-id: https://bd85.net/svn/cs3505_group@119 92bb83a3-7c8f-8a45-bc97-515c4e399668 --- diff --git a/CarFire/CarFire/CarFire/Display.cs b/CarFire/CarFire/CarFire/Display.cs index 9e06485..9fecb5b 100644 --- a/CarFire/CarFire/CarFire/Display.cs +++ b/CarFire/CarFire/CarFire/Display.cs @@ -99,7 +99,7 @@ namespace CarFire for (int i = 0; i < mProjectiles.Count; i++ ) { bool removed = false; - if (!mMap.IsCellOpen(new Point(mProjectiles[i].GridX, mProjectiles[i].GridY))) + if (!mMap.IsCellOpen(new Point(mProjectiles[i].Coordinates.X, mProjectiles[i].Coordinates.Y))) { mProjectiles.RemoveAt(i); @@ -117,7 +117,7 @@ namespace CarFire if(mCharacters[j] != null) for (int i = 0; i < mProjectiles.Count; i++) { - if (mProjectiles[i].GridX == mCharacters[j].Coordinates.X && mProjectiles[i].GridY == mCharacters[j].Coordinates.Y) + if (mProjectiles[i].Coordinates.X == mCharacters[j].Coordinates.X && mProjectiles[i].Coordinates.Y == mCharacters[j].Coordinates.Y) { mCharacters[j].causeDamageTo(mProjectiles[i].Damage); Console.WriteLine(mCharacters[j].Health); @@ -152,7 +152,7 @@ namespace CarFire //Handle wall collisions of projectiles again... for (int i = 0; i < mProjectiles.Count; i++) { - if (!mMap.IsCellOpen(new Point(mProjectiles[i].GridX, mProjectiles[i].GridY))) + if (!mMap.IsCellOpen(new Point(mProjectiles[i].Coordinates.X, mProjectiles[i].Coordinates.Y))) { mProjectiles.RemoveAt(i); i--; diff --git a/CarFire/CarFire/CarFire/Human.cs b/CarFire/CarFire/CarFire/Human.cs index 0d68d30..8a94fd3 100644 --- a/CarFire/CarFire/CarFire/Human.cs +++ b/CarFire/CarFire/CarFire/Human.cs @@ -138,31 +138,63 @@ namespace CarFire { if (keysPressed.Contains(Keys.Space)) { - //TODO spawn projectile... needs to be added to display though + float velocityX = 0; + float velocityY = 0; + int startX = Coordinates.X; + int startY = Coordinates.Y; + if (mMotion.Direction == Direction.Down || mMotion.Direction == Direction.LowerLeft || mMotion.Direction == Direction.LowerRight) + { + velocityY = 1; + startY = Coordinates.Y + 1; + } + else if (mMotion.Direction == Direction.Up || mMotion.Direction == Direction.UpperLeft || mMotion.Direction == Direction.UpperRight) + { + velocityY = -1; + startY = Coordinates.Y - 1; + } + if (mMotion.Direction == Direction.Right || mMotion.Direction == Direction.LowerRight || mMotion.Direction == Direction.UpperRight) + { + velocityX = 1; + startX = Coordinates.X + 1; + } + else if (mMotion.Direction == Direction.Left || mMotion.Direction == Direction.LowerLeft || mMotion.Direction == Direction.UpperLeft) + { + velocityX = -1; + startX = Coordinates.X - 1; + } + Vector2 toShoot = new Vector2(velocityX, velocityY); + toShoot.Normalize(); + toShoot *= projectileSpeed; + projectileCoolDown = shootCoolDown; + theDisplay.AddProjectiles(new Projectile(theMap, projectileModel, + toShoot, new Point(startX, startY))); + + /* if (state == State.up) { projectileCoolDown = shootCoolDown; theDisplay.AddProjectiles(new Projectile(theMap, projectileModel, - new Vector2(0, -projectileSpeed), Coordinates.X, Coordinates.Y - 1)); + new Vector2(0, -projectileSpeed), new Point(Coordinates.X, Coordinates.Y - 1))); } if (state == State.down) { projectileCoolDown = shootCoolDown; theDisplay.AddProjectiles(new Projectile(theMap, projectileModel, - new Vector2(0, projectileSpeed), Coordinates.X, Coordinates.Y + 1)); + new Vector2(0, projectileSpeed), new Point(Coordinates.X, Coordinates.Y + 1))); } if (state == State.right) { projectileCoolDown = shootCoolDown; theDisplay.AddProjectiles(new Projectile(theMap, projectileModel, - new Vector2(projectileSpeed, 0), Coordinates.X + 1, Coordinates.Y)); + new Vector2(projectileSpeed, 0), new Point (Coordinates.X + 1, Coordinates.Y))); } if (state == State.left) { projectileCoolDown = shootCoolDown; theDisplay.AddProjectiles(new Projectile(theMap, projectileModel, - new Vector2(-projectileSpeed, 0), Coordinates.X - 1, Coordinates.Y)); + new Vector2(-projectileSpeed, 0), new Point(Coordinates.X - 1, Coordinates.Y))); } + */ } } } diff --git a/CarFire/CarFire/CarFire/MovementManager.cs b/CarFire/CarFire/CarFire/MovementManager.cs index 512c2af..3b8dae1 100644 --- a/CarFire/CarFire/CarFire/MovementManager.cs +++ b/CarFire/CarFire/CarFire/MovementManager.cs @@ -207,7 +207,7 @@ namespace CarFire void RecalculatePosition(float alpha) { - Console.WriteLine("last: " + mLastCoordinates + ", now: " + mCoordinates + ", alpha: " + alpha); + //Console.WriteLine("last: " + mLastCoordinates + ", now: " + mCoordinates + ", alpha: " + alpha); mPosition.X = (float)mLastCoordinates.X + alpha * ((float)mCoordinates.X - (float)mLastCoordinates.X); mPosition.Y = (float)mLastCoordinates.Y + alpha * ((float)mCoordinates.Y - (float)mLastCoordinates.Y); } diff --git a/CarFire/CarFire/CarFire/Projectile.cs b/CarFire/CarFire/CarFire/Projectile.cs index 308e313..3f5b136 100644 --- a/CarFire/CarFire/CarFire/Projectile.cs +++ b/CarFire/CarFire/CarFire/Projectile.cs @@ -25,7 +25,9 @@ namespace CarFire //these will have to be transformed to the coordinate system that the drawable screen is using. int pixelX; int pixelY; - + MovementManager mMotion; + Point mPosition; + /// /// The Constructor for a projectile object. /// @@ -39,58 +41,34 @@ namespace CarFire public Projectile(Map _currentMap, Texture2D _projectileModel, Vector2 _velocity, - int _gridX, - int _gridY) - /*, - int _pixelX, - int _pixelY)*/ + Point _position) + { theMap = _currentMap; projectileModel = _projectileModel; velocity = _velocity; - gridX = _gridX; - gridY = _gridY; - pixelX = _gridX * (int)Map.PixelsToUnitSquares; - pixelY = _gridY * (int)Map.PixelsToUnitSquares; + mPosition = _position; damage = 20; + // Speed is the number of grid cells you can move through per second. + mMotion = new MovementManager(mPosition, velocity.Length()); } - public void Update(TimeSpan timespan) + public void Update(TimeSpan timeSpan) { - /* - //See if something moved onto this projectile. - if(theMap.isOpenSquare(gridX, gridY)) - { - theMap.damageSquare(gridX, gridY, damage); - } - */ - //If the projectile will be moving to a new grid position we need to check to see if it is occupied. - if ((int)((pixelX + velocity.X) / Map.PixelsToUnitSquares) != gridX || (int)((pixelY + velocity.Y) / Map.PixelsToUnitSquares) != gridY) - { - //bool open = theMap.IsCellOpen((int)((pixelX + velocity.X) /Map.PixelsToUnitSquares), (int)((pixelY + velocity.Y) / Map.PixelsToUnitSquares)); - //If open just move this projectile there - //***Map doesn't need to know that this projectile is there because players/monsters are allowed - // to move into the path of projectiles. - //if (open) - { - //Console.WriteLine("pixelX:" + pixelX + " " + " pixelY: "+ pixelY); - pixelX += (int)velocity.X; - pixelY += (int)velocity.Y; - gridX = (int)(pixelX /Map.PixelsToUnitSquares); - gridY = (int)(pixelY / Map.PixelsToUnitSquares); - } - //If the square isn't open then just damage whatever is there - //TODO: A projectile must be deleted after this happens - //else - { - //theMap.damageSquare(gridX, gridY, damage); - } - } - //If it is not moving grid positions just increment pixelX and pixelY - else - { - pixelX += (int)velocity.X; - pixelY += (int)velocity.Y; - } + bool moveRight = false; + bool moveLeft = false; + bool moveUp = false; + bool moveDown = false; + if (velocity.X > 0) + moveRight = true; + else if (velocity.X < 0) + moveLeft = true; + if (velocity.Y > 0) + moveDown = true; + else if (velocity.Y < 0) + moveUp = true; + Point destination = MovementManager.GetNeighborCell(mMotion.Coordinates, moveLeft, moveRight, moveUp, moveDown); + mMotion.Update(timeSpan, moveLeft, moveRight, moveUp, moveDown); + } /// @@ -101,13 +79,9 @@ namespace CarFire /// The map Y pixel position of the topLeft of the display public void Draw(SpriteBatch spriteBatch) { - //Console.WriteLine(gridX + " " + gridY); - //Console.WriteLine(theMap.GetRectangleFromCoordinates(new Vector2(gridX, gridY))); - Rectangle position = theMap.GetRectangleFromCoordinates(new Vector2(gridX, gridY)); - //spriteBatch.Draw(projectileModel, position, Color.White); - Rectangle position2 = new Rectangle((int)(pixelX), (int)(pixelY), (int)Map.PixelsToUnitSquares, (int)Map.PixelsToUnitSquares); - Rectangle position3 = theMap.GetRectangleFromCoordinates(new Vector2(pixelX / Map.PixelsToUnitSquares, pixelY / Map.PixelsToUnitSquares)); - spriteBatch.Draw(projectileModel, position3, Color.White); + Rectangle position = theMap.GetRectangleFromCoordinates(mMotion.Position); + spriteBatch.Draw(projectileModel, position, Color.White); + } /// @@ -119,5 +93,7 @@ namespace CarFire public int PixelY { get { return pixelY; } set { pixelY = value; } } public Map TheMap { get { return theMap; } set { theMap = value; } } public int Damage { get { return damage; } set { damage = value; } } + public Vector2 Position { get { return mMotion.Position; } } + public Point Coordinates { get { return mMotion.Coordinates; } } } }