From: Kyle Date: Mon, 19 Apr 2010 06:55:54 +0000 (+0000) Subject: Smooth player movement... still has some issues. X-Git-Url: https://git.brokenzipper.com/gitweb?a=commitdiff_plain;h=f8846aea7e94e617bacb8e497d65fbbab9676717;p=chaz%2Fcarfire Smooth player movement... still has some issues. git-svn-id: https://bd85.net/svn/cs3505_group@110 92bb83a3-7c8f-8a45-bc97-515c4e399668 --- diff --git a/CarFire/CarFire/CarFire/Display.cs b/CarFire/CarFire/CarFire/Display.cs index fc24895..d8f9a6b 100644 --- a/CarFire/CarFire/CarFire/Display.cs +++ b/CarFire/CarFire/CarFire/Display.cs @@ -51,22 +51,6 @@ namespace CarFire mMap = contentManager.Load("Maps/stable"); Map.DefaultTile = contentManager.Load("default"); mMap.CenterCell = new Vector2(currentCenterX,currentCenterY); - //Debugging... Spawn eight projectiles. - //Diagonals - /* - mProjectiles.Add(new Projectile(mMap, everything, new Vector2(5,5), 10, 10, 300, 300)); - mProjectiles.Add(new Projectile(mMap, everything, new Vector2(-5, 5), 10, 10, 300, 300)); - mProjectiles.Add(new Projectile(mMap, everything, new Vector2(5, -5), 10, 10, 300, 300)); - mProjectiles.Add(new Projectile(mMap, everything, new Vector2(-5, -5), 10, 10, 300, 300)); - //Vertical and horizontal - mProjectiles.Add(new Projectile(mMap, everything, new Vector2(0, 5), 10, 10, 300, 300)); - mProjectiles.Add(new Projectile(mMap, everything, new Vector2(-5, 0), 10, 10, 300, 300)); - mProjectiles.Add(new Projectile(mMap, everything, new Vector2(5, 0), 10, 10, 300, 300)); - mProjectiles.Add(new Projectile(mMap, everything, new Vector2(0, -5), 10, 10, 300, 300)); - */ - - - // TODO: use this.Content to load your game content here } /// @@ -160,10 +144,14 @@ namespace CarFire { mCharacters[i].MovePlayer(state.keysDown[i]); - mMap.CenterCell = new Vector2(mCharacters[0].GridX, mCharacters[0].GridY); + } } - //Handle wall collisions of projectiles again... + if (mCharacters[0] != null) + { + mMap.CenterCell = new Vector2(mCharacters[0].PixelX / Map.PixelsToUnitSquares, mCharacters[0].PixelY / Map.PixelsToUnitSquares); + } + //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))) @@ -190,13 +178,26 @@ namespace CarFire foreach(Projectile projectile in mProjectiles) { projectile.Draw(spriteBatch); - } for(int i = 0; i < 4; i++)//IPlayer character in mCharacters) { - if(mCharacters[i] != null) + + if (mCharacters[i] != null) + { mCharacters[i].Draw(spriteBatch); - } + 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)) + { + //Console.WriteLine(Math.Abs(mCharacters[i].GridX - mCharacters[i].PixelX / Map.PixelsToUnitSquares)); + //Console.WriteLine(Math.Abs(mCharacters[i].GridY - mCharacters[i].PixelY / Map.PixelsToUnitSquares)); + Console.WriteLine("Begin"); + Console.WriteLine(mCharacters[i].GridX); + Console.WriteLine(mCharacters[i].GridY); + Console.WriteLine(mCharacters[i].PixelX / Map.PixelsToUnitSquares); + Console.WriteLine(mCharacters[i].PixelY / Map.PixelsToUnitSquares); + } + } + } + } /// /// Add a projectile to the Display. diff --git a/CarFire/CarFire/CarFire/Human.cs b/CarFire/CarFire/CarFire/Human.cs index 9350d86..8c94c29 100644 --- a/CarFire/CarFire/CarFire/Human.cs +++ b/CarFire/CarFire/CarFire/Human.cs @@ -18,7 +18,7 @@ namespace CarFire up, down }; - const int moveCoolDown = 12; + const int moveCoolDown = 15; const int shootCoolDown = 10; //Member Variables State state; @@ -33,14 +33,25 @@ namespace CarFire int damage; int range; int score; + + //Used to smooth animations bool isMoving; - int pixelX; - int pixelY; - bool visible; + float pixelX; + float pixelY; + float nextPixelX; + float nextPixelY; + int movementSteps; int movementCoolDown; + float changeX; + float changeY; + + bool visible; Display theDisplay; + + //Used to draw projectiles int projectileSpeed; int projectileCoolDown; + public Human(Map _theMap, String Name, Texture2D model, Texture2D projectile, Display mDisplay) { @@ -57,6 +68,7 @@ namespace CarFire charModel = model; projectileModel = projectile; projectileSpeed = 30; + movementSteps = moveCoolDown -2; } @@ -80,13 +92,33 @@ namespace CarFire public long Draw(SpriteBatch spriteBatch) { - spriteBatch.Draw(charModel, theMap.GetRectangleFromCoordinates(gridX, gridY), Color.White); + if (isMoving && movementSteps > 0) + { + movementSteps--; + pixelX = pixelX + changeX; + pixelY = pixelY + changeY; + Rectangle position3 = theMap.GetRectangleFromCoordinates(new Vector2(pixelX / Map.PixelsToUnitSquares, pixelY / Map.PixelsToUnitSquares)); + spriteBatch.Draw(charModel, position3, Color.White); + } + else + { + pixelX = gridX * Map.PixelsToUnitSquares; + pixelY = gridY * Map.PixelsToUnitSquares; + changeX = 0; + changeY = 0; + isMoving = false; + movementSteps = moveCoolDown - 2; + spriteBatch.Draw(charModel, theMap.GetRectangleFromCoordinates(gridX, gridY), Color.White); + } return 0; } public int GridX { get { return gridX; } set { gridX = value; } } public int GridY { get { return gridY; } set { gridY = value; } } + public float PixelX { get { return pixelX; } } + public float PixelY { get { return pixelY; } } public int Health { get { return health; } } + public bool IsMoving { get { return isMoving; } } public int Score { get { return score; } } public bool alive { get { return health > 0; } } @@ -112,58 +144,104 @@ namespace CarFire keysPressed.Contains(Keys.Left); if (keysPressed.Contains(Keys.Up) && keysPressed.Contains(Keys.Left) && theMap.IsCellOpen(gridX - 1, gridY - 1)) { + pixelX = (float)(gridX * Map.PixelsToUnitSquares); + pixelY = (float)(gridY * Map.PixelsToUnitSquares); gridX -= 1; gridY -= 1; movementCoolDown = moveCoolDown; + isMoving = true; + nextPixelX = (float)(gridX * Map.PixelsToUnitSquares); + nextPixelY = (float)(gridY * Map.PixelsToUnitSquares); + } // move upright else if (keysPressed.Contains(Keys.Up) && keysPressed.Contains(Keys.Right) && theMap.IsCellOpen(gridX + 1, gridY - 1)) { + pixelX = (float)(gridX * Map.PixelsToUnitSquares); + pixelY = (float)(gridY * Map.PixelsToUnitSquares); gridX += 1; gridY -= 1; movementCoolDown = moveCoolDown; + isMoving = true; + nextPixelX = (float)(gridX * Map.PixelsToUnitSquares); + nextPixelY = (float)(gridY * Map.PixelsToUnitSquares); + } // move downleft else if (keysPressed.Contains(Keys.Down) && keysPressed.Contains(Keys.Left) && theMap.IsCellOpen(gridX - 1, gridY + 1)) { + pixelX = (float)(gridX * Map.PixelsToUnitSquares); + pixelY = (float)(gridY * Map.PixelsToUnitSquares); gridX -= 1; gridY += 1; movementCoolDown = moveCoolDown; + isMoving = true; + nextPixelX = (float)(gridX * Map.PixelsToUnitSquares); + nextPixelY = (float)(gridY * Map.PixelsToUnitSquares); } // move downright else if (keysPressed.Contains(Keys.Down) && keysPressed.Contains(Keys.Right) && theMap.IsCellOpen(gridX + 1, gridY + 1)) { + pixelX = (float)(gridX * Map.PixelsToUnitSquares); + pixelY = (float)(gridY * Map.PixelsToUnitSquares); gridX += 1; gridY += 1; movementCoolDown = moveCoolDown; + isMoving = true; + nextPixelX = (float)(gridX * Map.PixelsToUnitSquares); + nextPixelY = (float)(gridY * Map.PixelsToUnitSquares); } // move up else if (keysPressed.Contains(Keys.Up) && theMap.IsCellOpen(gridX, gridY - 1)) { + pixelX = (float)(gridX * Map.PixelsToUnitSquares); + pixelY = (float)(gridY * Map.PixelsToUnitSquares); state = State.up; gridY -= 1; movementCoolDown = moveCoolDown; + isMoving = true; + nextPixelX = (float)(gridX * Map.PixelsToUnitSquares); + nextPixelY = (float)(gridY * Map.PixelsToUnitSquares); + changeY = (float)(-Map.PixelsToUnitSquares / moveCoolDown); } // move down else if (keysPressed.Contains(Keys.Down) && theMap.IsCellOpen(gridX, gridY + 1)) { + pixelX = (float)(gridX * Map.PixelsToUnitSquares); + pixelY = (float)(gridY * Map.PixelsToUnitSquares); state = State.down; gridY += 1; movementCoolDown = moveCoolDown; + isMoving = true; + nextPixelX = (float)(gridX * Map.PixelsToUnitSquares); + nextPixelY = (float)(gridY * Map.PixelsToUnitSquares); + changeY = (float)(Map.PixelsToUnitSquares / moveCoolDown); } // move left else if (keysPressed.Contains(Keys.Left) && theMap.IsCellOpen(gridX - 1, gridY)) { + pixelX = (float)(gridX * Map.PixelsToUnitSquares); + pixelY = (float)(gridY * Map.PixelsToUnitSquares); state = State.left; gridX -= 1; movementCoolDown = moveCoolDown; + isMoving = true; + nextPixelX = (float)(gridX * Map.PixelsToUnitSquares); + nextPixelY = (float)(gridY * Map.PixelsToUnitSquares); + changeX = (float)(-Map.PixelsToUnitSquares / moveCoolDown); } // move right else if (keysPressed.Contains(Keys.Right) && theMap.IsCellOpen(gridX + 1, gridY)) { + pixelX = (float)(gridX * Map.PixelsToUnitSquares); + pixelY = (float)(gridY * Map.PixelsToUnitSquares); state = State.right; gridX += 1; movementCoolDown = moveCoolDown; + isMoving = true; + nextPixelX = (float)(gridX * Map.PixelsToUnitSquares); + nextPixelY = (float)(gridY * Map.PixelsToUnitSquares); + changeX = (float)(Map.PixelsToUnitSquares / moveCoolDown); } } if (projectileCoolDown > 0) diff --git a/CarFire/CarFire/CarFire/IPlayer.cs b/CarFire/CarFire/CarFire/IPlayer.cs index 3b98301..b60ef1a 100644 --- a/CarFire/CarFire/CarFire/IPlayer.cs +++ b/CarFire/CarFire/CarFire/IPlayer.cs @@ -19,6 +19,9 @@ namespace CarFire void causeDamageTo(int amount); int GridX { get; set; } int GridY { get; set; } + float PixelX { get; } + float PixelY { get; } + bool IsMoving { get; } } public interface IPlayer : ICharacter