X-Git-Url: https://git.dogcows.com/gitweb?p=chaz%2Fcarfire;a=blobdiff_plain;f=CarFire%2FCarFire%2FCarFire%2FHuman.cs;h=8c94c297de9152bcdf999c197f7b7b717ef45751;hp=acb0d3b8cd92cca04d68ca30fe11027aecbc3e44;hb=f8846aea7e94e617bacb8e497d65fbbab9676717;hpb=d0bdd76b2cfd38fe985a7493f42b5d6e0f79ac91 diff --git a/CarFire/CarFire/CarFire/Human.cs b/CarFire/CarFire/CarFire/Human.cs index acb0d3b..8c94c29 100644 --- a/CarFire/CarFire/CarFire/Human.cs +++ b/CarFire/CarFire/CarFire/Human.cs @@ -11,36 +11,72 @@ namespace CarFire { public class Human : IPlayer { + public enum State + { + left, + right, + up, + down + }; + const int moveCoolDown = 15; + const int shootCoolDown = 10; //Member Variables + State state; String CharName; Map theMap; int movementSpeed; int gridX; int gridY; Texture2D charModel; + Texture2D projectileModel; int health; int damage; int range; int score; + + //Used to smooth animations bool isMoving; - int pixelX; - int pixelY; + 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) + public Human(Map _theMap, String Name, Texture2D model, Texture2D projectile, Display mDisplay) { theMap = _theMap; CharName = Name; + theDisplay = mDisplay; - movementSpeed = 20; // randomly set now + movementSpeed = 12; // randomly set now health = 100; score = 0; visible = false; + //default state + state = State.up; + charModel = model; + projectileModel = projectile; + projectileSpeed = 30; + movementSteps = moveCoolDown -2; + } public void LoadContent(ContentManager contentManager) { charModel = contentManager.Load("deselectBox"); //change to charModel when designed + projectileModel = contentManager.Load("emptySelectBox"); //change to a projectile model later + } public void UnloadContent() @@ -56,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; } } @@ -80,50 +136,142 @@ namespace CarFire /// A general list of keys that are pressed. Other keys can be included but only direction keys will be used public void MovePlayer(List keysPressed) { - // move upleft - keysPressed.Contains(Keys.Left); - if (keysPressed.Contains(Keys.Up) && keysPressed.Contains(Keys.Left) && theMap.IsCellOpen(gridX - 1, gridY - 1)) - { - gridX -= 1; - gridY -= 1; - } - // move upright - else if (keysPressed.Contains(Keys.Up) && keysPressed.Contains(Keys.Right) && theMap.IsCellOpen(gridX + 1, gridY - 1)) - { - gridX += 1; - gridY -= 1; - } - // move downleft - else if (keysPressed.Contains(Keys.Down) && keysPressed.Contains(Keys.Left) && theMap.IsCellOpen(gridX - 1, gridY + 1)) - { - gridX -= 1; - gridY += 1; - } - // move downright - else if (keysPressed.Contains(Keys.Down) && keysPressed.Contains(Keys.Right) && theMap.IsCellOpen(gridX + 1, gridY + 1)) - { - gridX += 1; - gridY += 1; - } - // move up - else if (keysPressed.Contains(Keys.Up) && theMap.IsCellOpen(gridX, gridY - 1)) - { - gridY -= 1; - } - // move down - else if (keysPressed.Contains(Keys.Down) && theMap.IsCellOpen(gridX, gridY + 1)) - { - gridY += 1; - } - // move left - else if (keysPressed.Contains(Keys.Left) && theMap.IsCellOpen(gridX - 1, gridY)) + if(movementCoolDown > 0) + movementCoolDown--; + else if (movementCoolDown == 0) { - gridX -= 1; + // move upleft + 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); + } } - // move right - else if (keysPressed.Contains(Keys.Right) && theMap.IsCellOpen(gridX + 1, gridY)) + if (projectileCoolDown > 0) + projectileCoolDown--; + else if (projectileCoolDown == 0) { - gridX += 1; + if (keysPressed.Contains(Keys.Space)) + { + //TODO spawn projectile... needs to be added to display though + if (state == State.up) + { + projectileCoolDown = shootCoolDown; + theDisplay.AddProjectiles(new Projectile(theMap, projectileModel, new Vector2(0, -projectileSpeed), GridX, GridY - 1)); + } + if (state == State.down) + { + projectileCoolDown = shootCoolDown; + theDisplay.AddProjectiles(new Projectile(theMap, projectileModel, new Vector2(0, projectileSpeed), GridX, GridY + 1)); + } + if (state == State.right) + { + projectileCoolDown = shootCoolDown; + theDisplay.AddProjectiles(new Projectile(theMap, projectileModel, new Vector2(projectileSpeed, 0), GridX + 1, GridY)); + } + if (state == State.left) + { + projectileCoolDown = shootCoolDown; + theDisplay.AddProjectiles(new Projectile(theMap, projectileModel, new Vector2(-projectileSpeed, 0), GridX - 1, GridY)); + } + } } }