mirror of
https://github.com/inf112-v20/Fiasko.git
synced 2025-01-31 23:29:36 +01:00
Rydder opp i og kommenterer respawning av roboter
This commit is contained in:
parent
160ca89712
commit
bf42047cde
@ -448,7 +448,7 @@ public class Board {
|
|||||||
public void respawnRobots() {
|
public void respawnRobots() {
|
||||||
for (Robot robot : deadRobots) {
|
for (Robot robot : deadRobots) {
|
||||||
if (robot.getAmountOfLives() > 0) {
|
if (robot.getAmountOfLives() > 0) {
|
||||||
tryCircle(robot);
|
respawnRobot(robot);
|
||||||
robot.setDamageTaken(2);
|
robot.setDamageTaken(2);
|
||||||
robots.put(robot.getRobotId(), robot);
|
robots.put(robot.getRobotId(), robot);
|
||||||
} else {
|
} else {
|
||||||
@ -457,101 +457,119 @@ public class Board {
|
|||||||
}
|
}
|
||||||
deadRobots = new ArrayList<>();
|
deadRobots = new ArrayList<>();
|
||||||
}
|
}
|
||||||
public void tryCircle(Robot robot){
|
|
||||||
Position respawn = robot.getBackupPosition();
|
/**
|
||||||
int startX=respawn.getXCoordinate();
|
* Re-spawns a robot at the first available position relative to its backup position
|
||||||
int startY=respawn.getYCoordinate();
|
*
|
||||||
if(!hasRobotOnPosition(respawn)){
|
* @param robot The robot to re-spawn
|
||||||
robot.setPosition(respawn);
|
*/
|
||||||
|
public void respawnRobot(Robot robot) {
|
||||||
|
Position backupPosition = robot.getBackupPosition();
|
||||||
|
int startX = backupPosition.getXCoordinate();
|
||||||
|
int startY = backupPosition.getYCoordinate();
|
||||||
|
if (!hasRobotOnPosition(backupPosition)) {
|
||||||
|
robot.setPosition(backupPosition);
|
||||||
|
//Tries to set the robot's facing direction to the true up direction
|
||||||
|
List<BoardElementContainer<Tile>> robotSpawn = getPositionsOfTilesOnBoard(TileType.ROBOT_SPAWN_1);
|
||||||
|
if (robotSpawn.size() == 1) {
|
||||||
|
robot.setFacingDirection(robotSpawn.get(0).getElement().getDirection());
|
||||||
|
} else {
|
||||||
|
robot.setFacingDirection(Direction.NORTH);
|
||||||
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
for(int i=1; i<3;i++){
|
int circleSize = 1;
|
||||||
if(tryRespawn(robot,i,startX,startY,Direction.NORTH)){
|
boolean hasRespawned = false;
|
||||||
break;
|
while (!hasRespawned) {
|
||||||
}
|
hasRespawned = tryRobotRespawn(robot, circleSize, startX, startY, Direction.NORTH) ||
|
||||||
if(tryRespawn(robot,i,startX,startY,Direction.SOUTH)){
|
tryRobotRespawn(robot, circleSize, startX, startY, Direction.EAST) ||
|
||||||
break;
|
tryRobotRespawn(robot, circleSize, startX, startY, Direction.SOUTH) ||
|
||||||
}
|
tryRobotRespawn(robot, circleSize, startX, startY, Direction.WEST);
|
||||||
if(tryRespawn(robot,i,startX,startY,Direction.EAST)){
|
circleSize++;
|
||||||
break;
|
|
||||||
}
|
|
||||||
if(tryRespawn(robot,i,startX,startY,Direction.WEST)){
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
public Boolean tryRespawn(Robot robot,int size,int startX,int startY,Direction direction){
|
/**
|
||||||
|
* Tries to re-spawn a robot on one of the positions described
|
||||||
|
*
|
||||||
|
* @param robot The robot to re-spawn
|
||||||
|
* @param size The size of the square relative to the robot's spawn to try
|
||||||
|
* @param startX The x coordinate of the robot's backup position
|
||||||
|
* @param startY The y coordinate of the robot's backup position
|
||||||
|
* @param direction The direction of the face of the square to check
|
||||||
|
* @return Whether the robot was re-spawned
|
||||||
|
*/
|
||||||
|
public boolean tryRobotRespawn(Robot robot, int size, int startX, int startY, Direction direction) {
|
||||||
int axis;
|
int axis;
|
||||||
for (int i = 1; i <= size; i++) {
|
for (int i = 1; i <= size; i++) {
|
||||||
if (direction == Direction.NORTH || direction == Direction.SOUTH) {
|
if (direction == Direction.NORTH || direction == Direction.SOUTH) {
|
||||||
axis = startX;
|
axis = startX;
|
||||||
}
|
} else {
|
||||||
else{
|
|
||||||
axis = startY;
|
axis = startY;
|
||||||
}
|
}
|
||||||
for (int j = axis - i; j < axis + i; j++) {
|
for (int j = axis - i; j < axis + i; j++) {
|
||||||
int[] coordinates = switchForRespawn(startX,startY,direction,j,size);
|
Position potentialSpawn = calculatePotentialRobotSpawn(startX, startY, direction, j, size);
|
||||||
int x=coordinates[0];
|
if (isValidPosition(potentialSpawn) && !hasHole(potentialSpawn) && !hasRobotOnPosition(potentialSpawn)) {
|
||||||
int y=coordinates[1];
|
robot.setFacingDirection(direction);
|
||||||
Position tryRespawn = new Position(x, y);
|
robot.setPosition(potentialSpawn);
|
||||||
System.out.println(tryRespawn);
|
|
||||||
if (!isValidPosition(tryRespawn)) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
TileType tile = getTileOnPosition(tryRespawn).getType();
|
|
||||||
if(checkIfRespawnHasPit(tile, direction, robot, tryRespawn)){
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
private int[] switchForRespawn(int startX,int startY,Direction direction,int j,int size){
|
|
||||||
int x;
|
|
||||||
int y;
|
|
||||||
switch (direction){
|
|
||||||
case NORTH:
|
|
||||||
x = j;
|
|
||||||
y = startY-size;
|
|
||||||
break;
|
|
||||||
case SOUTH:
|
|
||||||
x = j;
|
|
||||||
y = startY+size;
|
|
||||||
break;
|
|
||||||
case WEST:
|
|
||||||
x = startX-size;
|
|
||||||
y = j;
|
|
||||||
break;
|
|
||||||
case EAST:
|
|
||||||
x = startX+size;
|
|
||||||
y = j;
|
|
||||||
break;
|
|
||||||
default: throw new IllegalArgumentException("invalid direction for tile");
|
|
||||||
}
|
|
||||||
return new int[]{x,y};
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean checkIfRespawnHasPit(TileType tile,Direction direction,Robot robot,Position tryRespawn){
|
/**
|
||||||
if ( tile!=TileType.PIT_CORNER && tile!=TileType.PIT_EMPTY &&tile!=TileType.PIT_FULL &&
|
* Calculates a potential robot spawn from the input
|
||||||
tile!=TileType.PIT_NORMAL &&tile!=TileType.PIT_U && tile!=TileType.HOLE){
|
*
|
||||||
if(!hasRobotOnPosition(tryRespawn)){
|
* @param startX The start position's x coordinate
|
||||||
robot.setFacingDirection(direction);
|
* @param startY The start position's y coordinate
|
||||||
robot.setPosition(tryRespawn);
|
* @param direction The direction which is checked
|
||||||
return true;
|
* @param j The offset from the start position
|
||||||
|
* @param size The size of the square around the start position
|
||||||
|
* @return The position described by the input
|
||||||
|
*/
|
||||||
|
private Position calculatePotentialRobotSpawn(int startX, int startY, Direction direction, int j, int size) {
|
||||||
|
switch (direction) {
|
||||||
|
case NORTH:
|
||||||
|
return new Position(j, startY - size);
|
||||||
|
case SOUTH:
|
||||||
|
return new Position(j, startY + size);
|
||||||
|
case WEST:
|
||||||
|
return new Position(startX - size, j);
|
||||||
|
case EAST:
|
||||||
|
return new Position(startX + size, j);
|
||||||
|
default:
|
||||||
|
throw new IllegalArgumentException("invalid direction for tile");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return false;
|
|
||||||
|
/**
|
||||||
|
* Checks whether a position contains a hole
|
||||||
|
*
|
||||||
|
* @param position The position to check
|
||||||
|
* @return True if the position has a hole
|
||||||
|
*/
|
||||||
|
private boolean hasHole(Position position) {
|
||||||
|
TileType type = getTileOnPosition(position).getType();
|
||||||
|
return type == TileType.HOLE || type == TileType.PIT_CORNER || type == TileType.PIT_EMPTY
|
||||||
|
|| type == TileType.PIT_FULL || type == TileType.PIT_NORMAL || type == TileType.PIT_U;
|
||||||
}
|
}
|
||||||
public void updateRobotRespawn(){
|
|
||||||
List<BoardElementContainer<Tile>> repaireTiles = getPositionsOfTilesOnBoard(TileType.WRENCH,
|
/**
|
||||||
|
* Updates backup position of all robots on a repair tile
|
||||||
|
*/
|
||||||
|
public void updateRobotBackups() {
|
||||||
|
List<BoardElementContainer<Tile>> repairTiles = getPositionsOfTilesOnBoard(TileType.WRENCH,
|
||||||
TileType.WRENCH_AND_HAMMER, TileType.FLAG_1, TileType.FLAG_2, TileType.FLAG_3, TileType.FLAG_4);
|
TileType.WRENCH_AND_HAMMER, TileType.FLAG_1, TileType.FLAG_2, TileType.FLAG_3, TileType.FLAG_4);
|
||||||
for (BoardElementContainer<Tile> repaireTile:repaireTiles) {
|
for (BoardElementContainer<Tile> repairTile : repairTiles) {
|
||||||
Position pos = repaireTile.getPosition();
|
Position position = repairTile.getPosition();
|
||||||
if(hasRobotOnPosition(pos)){
|
if (hasRobotOnPosition(position)) {
|
||||||
getRobot(getRobotOnPosition(pos)).setBackupPosition(pos);
|
getRobot(getRobotOnPosition(position)).setBackupPosition(position);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns a robot id for a robot on a specific position if such a robot exists
|
* Returns a robot id for a robot on a specific position if such a robot exists
|
||||||
*
|
*
|
||||||
|
@ -66,7 +66,7 @@ public class Phase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void updateRobotRespawn() {
|
public void updateRobotRespawn() {
|
||||||
gameBoard.updateRobotRespawn();
|
gameBoard.updateRobotBackups();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -118,7 +118,7 @@ public final class BoardLoaderUtil {
|
|||||||
*/
|
*/
|
||||||
private static void adjustRobotRotationToBoardRotation(Grid<Tile> tileGrid, List<Robot> robotList) {
|
private static void adjustRobotRotationToBoardRotation(Grid<Tile> tileGrid, List<Robot> robotList) {
|
||||||
//The flags are always in the up direction
|
//The flags are always in the up direction
|
||||||
List<BoardElementContainer<Tile>> flags = GridUtil.getMatchingElements(TileType.FLAG_1, tileGrid);
|
List<BoardElementContainer<Tile>> flags = GridUtil.getMatchingElements(TileType.ROBOT_SPAWN_1, tileGrid);
|
||||||
Direction boardDirection;
|
Direction boardDirection;
|
||||||
if (flags.size() == 0) {
|
if (flags.size() == 0) {
|
||||||
boardDirection = Direction.NORTH;
|
boardDirection = Direction.NORTH;
|
||||||
|
@ -1,11 +1,11 @@
|
|||||||
28 12
|
28 12
|
||||||
21;01 01;03 01;03 01;03 01;03 01;03 01;03 01;03 01;03 01;03 01;03 01;03 01;1 01;1 01;1 01;1 01;1 01;1 01;1 01;1 01;1 01;1 01;1 01;1 01;07 01;07 30;07 01;07
|
21;01 01;03 01;03 01;03 01;03 01;03 01;03 01;03 01;03 01;03 01;03 01;03 01;1 01;1 01;1 01;1 01;1 01;1 01;1 01;1 01;1 01;1 01;1 01;1 01;07 01;07 30;07 01;07
|
||||||
01;03 34;07 35;03 01;03 01;03 01;01 01;03 01;03 01;03 35;07 34;01 01;03 01;1 12;3 11;3 11;3 12;5 01;1 01;1 12;3 11;3 11;3 12;5 01;1 01;07 01;07 28;07 01;07
|
01;03 34;07 35;03 01;03 01;03 01;01 01;03 01;03 01;03 35;07 34;01 01;03 01;1 12;3 11;3 11;3 12;5 01;1 01;1 12;3 11;3 11;3 12;5 01;1 01;07 01;07 28;07 01;07
|
||||||
01;03 35;05 03;01 05;03 05;03 05;03 05;03 05;03 05;03 05;03 35;05 01;03 17;7 11;1 03;1 01;1 11;5 04;1 01;1 11;1 03;1 01;1 11;5 01;1 01;07 01;07 01;07 01;07
|
01;03 35;05 03;01 05;03 05;03 05;03 05;03 05;03 05;03 05;03 35;05 01;03 17;1 11;1 03;1 01;1 11;5 04;1 01;1 11;1 03;1 01;1 11;5 01;1 01;07 01;07 01;07 01;07
|
||||||
01;03 01;03 05;01 04;01 05;07 05;07 09;07 05;07 04;01 05;05 01;03 01;03 01;1 11;1 21;1 03;1 11;5 01;1 01;1 11;1 22;1 03;1 11;5 01;1 01;07 01;07 26;07 01;07
|
01;03 01;03 05;01 04;01 05;07 05;07 09;07 05;07 04;01 05;05 01;03 01;03 01;1 11;1 21;1 03;1 11;5 01;1 01;1 11;1 22;1 03;1 11;5 01;1 01;07 01;07 26;07 01;07
|
||||||
01;03 01;03 05;01 05;05 34;07 35;03 05;01 01;03 05;01 05;05 01;03 01;03 01;1 12;1 11;7 11;7 12;7 01;1 04;1 12;1 11;7 11;7 12;7 01;1 01;07 01;07 01;07 01;07
|
01;03 01;03 05;01 05;05 34;07 35;03 05;01 01;03 05;01 05;05 01;03 01;03 01;1 12;1 11;7 11;7 12;7 01;1 04;1 12;1 11;7 11;7 12;7 01;1 01;07 01;07 01;07 01;07
|
||||||
01;03 18;07 05;01 05;05 35;05 22;01 05;01 01;03 05;01 05;05 01;03 01;03 01;1 01;1 01;1 01;1 04;1 01;1 01;1 01;1 01;1 04;1 01;1 01;1 01;07 01;07 24;07 01;07
|
01;03 18;01 05;01 05;05 35;05 22;01 05;01 01;03 05;01 05;05 01;03 01;03 01;1 01;1 01;1 01;1 04;1 01;1 01;1 01;1 01;1 04;1 01;1 01;1 01;07 01;07 24;07 01;07
|
||||||
01;03 01;03 05;01 05;05 01;03 05;05 01;03 35;01 05;01 05;05 01;01 01;03 01;1 01;1 04;1 01;1 01;1 01;1 01;1 04;1 01;1 01;1 19;7 01;1 01;07 01;07 23;07 01;07
|
01;03 01;03 05;01 05;05 01;03 05;05 01;03 35;01 05;01 05;05 01;01 01;03 01;1 01;1 04;1 01;1 01;1 01;1 01;1 04;1 01;1 01;1 19;1 01;1 01;07 01;07 23;07 01;07
|
||||||
01;03 01;03 05;01 05;05 01;03 05;05 35;07 34;03 05;01 05;05 01;03 01;03 01;1 12;3 11;3 11;3 12;5 04;1 01;1 12;3 11;3 11;3 12;5 01;1 01;07 01;07 01;07 01;07
|
01;03 01;03 05;01 05;05 01;03 05;05 35;07 34;03 05;01 05;05 01;03 01;03 01;1 12;3 11;3 11;3 12;5 04;1 01;1 12;3 11;3 11;3 12;5 01;1 01;07 01;07 01;07 01;07
|
||||||
01;03 01;03 05;01 04;01 05;03 09;03 05;03 05;03 04;01 05;05 01;03 01;03 01;1 11;1 03;1 22;1 11;5 01;1 01;1 11;1 03;1 21;1 11;5 01;1 01;07 01;07 25;07 01;07
|
01;03 01;03 05;01 04;01 05;03 09;03 05;03 05;03 04;01 05;05 01;03 01;03 01;1 11;1 03;1 22;1 11;5 01;1 01;1 11;1 03;1 21;1 11;5 01;1 01;07 01;07 25;07 01;07
|
||||||
01;03 35;01 03;01 05;07 05;07 05;07 05;07 05;07 05;07 05;05 35;01 01;03 01;1 11;1 01;1 03;1 11;5 01;1 04;1 11;1 01;1 03;1 11;5 01;1 01;07 01;07 01;07 01;07
|
01;03 35;01 03;01 05;07 05;07 05;07 05;07 05;07 05;07 05;05 35;01 01;03 01;1 11;1 01;1 03;1 11;5 01;1 04;1 11;1 01;1 03;1 11;5 01;1 01;07 01;07 01;07 01;07
|
||||||
|
Loading…
x
Reference in New Issue
Block a user