public bool willObeyFloorTile(Crew crewMember, Vector2 newPosition) { bool willObey = false; // check for a floor tile under crew member // we'll do a tile based check if the crew member is completely in a tile // get crew member's bounding values float crewXMin = newPosition.x - (crewMember.width / 2); float crewXMax = newPosition.x + (crewMember.width / 2); float crewYMin = newPosition.y - (crewMember.height / 2); float crewYMax = newPosition.y + (crewMember.height / 2); // get crew members column number tile's x bounding values int colNumber = (int)newPosition.x / 64; int xTileMin = colNumber * 64; int xTileMax = xTileMin + 64; // get crew members row number tile's y bounding values int rowNumber = (int)newPosition.y / 64; int yTileMin = rowNumber * 64; int yTileMax = yTileMin + 64; MoveTile moveTile = getMoveTile(rowNumber, colNumber); if (moveTile != null && moveTile != crewMember.lastMoveTileUsed) { // crew member is on a move tile and is not currently following it if (crewMember.direction != moveTile.direction) { // we only care if the tile will change the crew members direction if (crewMember.direction == VectorDirection.Up || crewMember.direction == VectorDirection.Down) { // crew member is moving up or down, wait until crew member is contained between tile's min/max y before obeying it if (crewYMin >= yTileMin && crewYMax <= yTileMax) { crewMember.ChangeDirection(moveTile.direction); crewMember.lastMoveTileUsed = moveTile; willObey = true; } } else { // crew member is moving left or right, wait until crew member is contained between tile's min/max x before obeying it if (crewXMin >= xTileMin && crewXMax <= xTileMax) { crewMember.ChangeDirection(moveTile.direction); crewMember.lastMoveTileUsed = moveTile; willObey = true; } } } } else if (moveTile == null) { // crew member is not on a move tile crewMember.lastMoveTileUsed = null; } return(willObey); }
// this method is fairly similar to rewMemberIsPassingThruDoor(Crew crewMember) // there may be a way to combine similar functionality that has been copy/pasted. // but thats a challenge for another time. public bool willDirectCrewToDoor(Crew crewMember) { bool willDirectToDoor = false; // check for a door immediately next to crew member // we'll do a tile based check if the crew member is completely in a tile // get crew member's bounding values float crewXMin = crewMember.x - (crewMember.width / 2); float crewXMax = crewMember.x + (crewMember.width / 2); float crewYMin = crewMember.y - (crewMember.height / 2); float crewYMax = crewMember.y + (crewMember.height / 2); // get crew members column number tile's x bounding values int colNumber = (int)crewMember.x / 64; int xTileMin = colNumber * 64; int xTileMax = xTileMin + 64; // get crew members row number tile's y bounding values int rowNumber = (int)crewMember.y / 64; int yTileMin = rowNumber * 64; int yTileMax = yTileMin + 64; // see if there is a door next to the crew member Door checkDoor = getOpenDoor(rowNumber, colNumber); if (checkDoor != null && crewMember.lastMoveTileUsed == null) // if lastMoveTileUsed is not null, then crew member is still obeying a move tile { if (checkDoor.doorIsHorizontal) { // door is above or below the crew member, make sure they are within the tile's x boundary in order to access if (crewXMin >= xTileMin && crewXMax <= xTileMax) { if (rowNumber == checkDoor.startRow) { // crew member is below if (crewMember.direction != VectorDirection.Up && crewMember.direction != VectorDirection.Down) { // update direction so long as crew member is not moving directly away from the door, in which case they probably just passed through it willDirectToDoor = true; crewMember.ChangeDirection(VectorDirection.Up); } } else { // crew member is above if (crewMember.direction != VectorDirection.Down && crewMember.direction != VectorDirection.Up) { // update direction so long as crew member is not moving directly away from the door, in which case they probably just passed through it willDirectToDoor = true; crewMember.ChangeDirection(VectorDirection.Down); } } } } else { // door is to the left or right of the crew member, make sure they are within the tile's y boundary in order to access if (crewYMin >= yTileMin && crewYMax <= yTileMax) { // check for a door to the left or right that the crew member could access if (colNumber == checkDoor.startCol) { // crew member is to the left if (crewMember.direction != VectorDirection.Right && crewMember.direction != VectorDirection.Left) { // update direction so long as crew member is not moving directly away from the door, in which case they probably just passed through it willDirectToDoor = true; crewMember.ChangeDirection(VectorDirection.Right); } } else { // crew member is to the right if (crewMember.direction != VectorDirection.Left && crewMember.direction != VectorDirection.Right) { // update direction so long as crew member is not moving directly away from the door, in which case they probably just passed through it willDirectToDoor = true; crewMember.ChangeDirection(VectorDirection.Left); } } } } } return(willDirectToDoor); }
// check if the crew member is going to run into a wall public bool checkCrewHeadingOk(Crew crewMember, Vector2 newPosition) { bool headingOk = true; if (crewMember.direction == VectorDirection.Up) { // check top walls and horizontal walls int maxY; // max height the crew member can travel in this spot if (crewMember.x >= 16 * 64) { maxY = (8 * 64) - 32; } else { maxY = 9 * 64; } if (newPosition.y >= maxY || isCrewGoingToIntersectWall(crewMember, newPosition)) { headingOk = false; if (RXRandom.Range(0, 2) == 1) { crewMember.ChangeDirection(VectorDirection.Right); } else { crewMember.ChangeDirection(VectorDirection.Left); } } } else if (crewMember.direction == VectorDirection.Down) { // check bottom walls and horizontal walls int minY; // min y the crew member can travel in this spot if (crewMember.x >= 16 * 64) { minY = (2 * 64) + 30; } else { minY = 64; } if (newPosition.y <= minY || isCrewGoingToIntersectWall(crewMember, newPosition)) { headingOk = false; if (RXRandom.Range(0, 2) == 1) { crewMember.ChangeDirection(VectorDirection.Right); } else { crewMember.ChangeDirection(VectorDirection.Left); } } } else if (crewMember.direction == VectorDirection.Left) { // check bottom walls and horizontal walls int minX; // min x the crew member can travel in this spot minX = (3 * 64) + 32; if (newPosition.x <= minX || isCrewGoingToIntersectWall(crewMember, newPosition)) { headingOk = false; if (RXRandom.Range(0, 2) == 1) { crewMember.ChangeDirection(VectorDirection.Up); } else { crewMember.ChangeDirection(VectorDirection.Down); } } } else if (crewMember.direction == VectorDirection.Right) { // check bottom walls and horizontal walls int maxX; // max x the crew member can travel in this spot if (crewMember.y >= (2 * 64) + 32 && crewMember.y <= (8 * 64) - 32) { maxX = (19 * 64) - 32; } else { maxX = 16 * 64 - 32; } if (newPosition.x >= maxX || isCrewGoingToIntersectWall(crewMember, newPosition)) { headingOk = false; if (RXRandom.Range(0, 2) == 1) { crewMember.ChangeDirection(VectorDirection.Up); } else { crewMember.ChangeDirection(VectorDirection.Down); } } } return(headingOk); }
public bool willObeyFloorTile(Crew crewMember, Vector2 newPosition) { bool willObey = false; // check for a floor tile under crew member // we'll do a tile based check if the crew member is completely in a tile // get crew member's bounding values float crewXMin = newPosition.x - (crewMember.width / 2); float crewXMax = newPosition.x + (crewMember.width / 2); float crewYMin = newPosition.y - (crewMember.height / 2); float crewYMax = newPosition.y + (crewMember.height / 2); // get crew members column number tile's x bounding values int colNumber = (int)newPosition.x / 64; int xTileMin = colNumber * 64; int xTileMax = xTileMin + 64; // get crew members row number tile's y bounding values int rowNumber = (int)newPosition.y / 64; int yTileMin = rowNumber * 64; int yTileMax = yTileMin + 64; MoveTile moveTile = getMoveTile(rowNumber, colNumber); if (moveTile != null && moveTile != crewMember.lastMoveTileUsed){ // crew member is on a move tile and is not currently following it if (crewMember.direction != moveTile.direction){ // we only care if the tile will change the crew members direction if (crewMember.direction == VectorDirection.Up || crewMember.direction == VectorDirection.Down){ // crew member is moving up or down, wait until crew member is contained between tile's min/max y before obeying it if (crewYMin >= yTileMin && crewYMax <= yTileMax){ crewMember.ChangeDirection(moveTile.direction); crewMember.lastMoveTileUsed = moveTile; willObey = true; } } else { // crew member is moving left or right, wait until crew member is contained between tile's min/max x before obeying it if (crewXMin >= xTileMin && crewXMax <= xTileMax){ crewMember.ChangeDirection(moveTile.direction); crewMember.lastMoveTileUsed = moveTile; willObey = true; } } } } else if (moveTile == null){ // crew member is not on a move tile crewMember.lastMoveTileUsed = null; } return willObey; }
// this method is fairly similar to rewMemberIsPassingThruDoor(Crew crewMember) // there may be a way to combine similar functionality that has been copy/pasted. // but thats a challenge for another time. public bool willDirectCrewToDoor(Crew crewMember) { bool willDirectToDoor = false; // check for a door immediately next to crew member // we'll do a tile based check if the crew member is completely in a tile // get crew member's bounding values float crewXMin = crewMember.x - (crewMember.width / 2); float crewXMax = crewMember.x + (crewMember.width / 2); float crewYMin = crewMember.y - (crewMember.height / 2); float crewYMax = crewMember.y + (crewMember.height / 2); // get crew members column number tile's x bounding values int colNumber = (int)crewMember.x / 64; int xTileMin = colNumber * 64; int xTileMax = xTileMin + 64; // get crew members row number tile's y bounding values int rowNumber = (int)crewMember.y / 64; int yTileMin = rowNumber * 64; int yTileMax = yTileMin + 64; // see if there is a door next to the crew member Door checkDoor = getOpenDoor(rowNumber, colNumber); if (checkDoor != null && crewMember.lastMoveTileUsed == null){ // if lastMoveTileUsed is not null, then crew member is still obeying a move tile if (checkDoor.doorIsHorizontal){ // door is above or below the crew member, make sure they are within the tile's x boundary in order to access if (crewXMin >= xTileMin && crewXMax <= xTileMax){ if (rowNumber == checkDoor.startRow){ // crew member is below if (crewMember.direction != VectorDirection.Up && crewMember.direction != VectorDirection.Down){ // update direction so long as crew member is not moving directly away from the door, in which case they probably just passed through it willDirectToDoor = true; crewMember.ChangeDirection(VectorDirection.Up); } } else { // crew member is above if (crewMember.direction != VectorDirection.Down && crewMember.direction != VectorDirection.Up){ // update direction so long as crew member is not moving directly away from the door, in which case they probably just passed through it willDirectToDoor = true; crewMember.ChangeDirection(VectorDirection.Down); } } } } else { // door is to the left or right of the crew member, make sure they are within the tile's y boundary in order to access if (crewYMin >= yTileMin && crewYMax <= yTileMax){ // check for a door to the left or right that the crew member could access if (colNumber == checkDoor.startCol){ // crew member is to the left if (crewMember.direction != VectorDirection.Right && crewMember.direction != VectorDirection.Left){ // update direction so long as crew member is not moving directly away from the door, in which case they probably just passed through it willDirectToDoor = true; crewMember.ChangeDirection(VectorDirection.Right); } } else { // crew member is to the right if (crewMember.direction != VectorDirection.Left && crewMember.direction != VectorDirection.Right){ // update direction so long as crew member is not moving directly away from the door, in which case they probably just passed through it willDirectToDoor = true; crewMember.ChangeDirection(VectorDirection.Left); } } } } } return willDirectToDoor; }
// check if the crew member is going to run into a wall public bool checkCrewHeadingOk(Crew crewMember, Vector2 newPosition) { bool headingOk = true; if (crewMember.direction == VectorDirection.Up){ // check top walls and horizontal walls int maxY; // max height the crew member can travel in this spot if (crewMember.x >= 16*64){ maxY = (8 * 64) - 32; } else { maxY = 9 * 64; } if (newPosition.y >= maxY || isCrewGoingToIntersectWall(crewMember, newPosition)){ headingOk = false; if (RXRandom.Range(0,2) == 1){ crewMember.ChangeDirection(VectorDirection.Right); } else { crewMember.ChangeDirection(VectorDirection.Left); } } } else if (crewMember.direction == VectorDirection.Down){ // check bottom walls and horizontal walls int minY; // min y the crew member can travel in this spot if (crewMember.x >= 16*64){ minY = (2 * 64) + 30; } else { minY = 64 ; } if (newPosition.y <= minY || isCrewGoingToIntersectWall(crewMember, newPosition)){ headingOk = false; if (RXRandom.Range(0,2) == 1){ crewMember.ChangeDirection(VectorDirection.Right); } else { crewMember.ChangeDirection(VectorDirection.Left); } } } else if (crewMember.direction == VectorDirection.Left){ // check bottom walls and horizontal walls int minX; // min x the crew member can travel in this spot minX = (3 * 64) + 32; if (newPosition.x <= minX || isCrewGoingToIntersectWall(crewMember, newPosition)){ headingOk = false; if (RXRandom.Range(0,2) == 1){ crewMember.ChangeDirection(VectorDirection.Up); } else { crewMember.ChangeDirection(VectorDirection.Down); } } } else if (crewMember.direction == VectorDirection.Right){ // check bottom walls and horizontal walls int maxX; // max x the crew member can travel in this spot if (crewMember.y >= (2*64) + 32 && crewMember.y <= (8*64) - 32){ maxX = (19 * 64) - 32; } else { maxX = 16 * 64 - 32; } if (newPosition.x >= maxX || isCrewGoingToIntersectWall(crewMember, newPosition)){ headingOk = false; if (RXRandom.Range(0,2) == 1){ crewMember.ChangeDirection(VectorDirection.Up); } else { crewMember.ChangeDirection(VectorDirection.Down); } } } return headingOk; }