2020-04-14 15:54:09 +02:00
|
|
|
package inf112.fiasko.roborally.elementproperties;
|
2020-02-17 21:12:04 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* This enum represents all possible directions for an element on the board
|
|
|
|
*/
|
|
|
|
public enum Direction {
|
2020-04-20 14:19:20 +02:00
|
|
|
/**
|
|
|
|
* The north direction
|
|
|
|
*/
|
2020-04-20 13:10:13 +02:00
|
|
|
NORTH(1),
|
2020-04-20 14:19:20 +02:00
|
|
|
/**
|
|
|
|
* The north-east direction
|
|
|
|
*/
|
2020-04-20 13:10:13 +02:00
|
|
|
NORTH_EAST(2),
|
2020-04-20 14:19:20 +02:00
|
|
|
/**
|
|
|
|
* The east direction
|
|
|
|
*/
|
2020-04-20 13:10:13 +02:00
|
|
|
EAST(3),
|
2020-04-20 14:19:20 +02:00
|
|
|
/**
|
|
|
|
* The south-east direction
|
|
|
|
*/
|
2020-04-20 13:10:13 +02:00
|
|
|
SOUTH_EAST(4),
|
2020-04-20 14:19:20 +02:00
|
|
|
/**
|
|
|
|
* The south direction
|
|
|
|
*/
|
2020-04-20 13:10:13 +02:00
|
|
|
SOUTH(5),
|
2020-04-20 14:19:20 +02:00
|
|
|
/**
|
|
|
|
* The south-west direction
|
|
|
|
*/
|
2020-04-20 13:10:13 +02:00
|
|
|
SOUTH_WEST(6),
|
2020-04-20 14:19:20 +02:00
|
|
|
/**
|
|
|
|
* The west direction
|
|
|
|
*/
|
2020-04-20 13:10:13 +02:00
|
|
|
WEST(7),
|
2020-04-20 14:19:20 +02:00
|
|
|
/**
|
|
|
|
* The north-west direction
|
|
|
|
*/
|
2020-04-20 13:10:13 +02:00
|
|
|
NORTH_WEST(8);
|
2020-02-17 21:12:04 +01:00
|
|
|
|
|
|
|
private final int directionID;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor to let a direction to be represented by a numerical identifier
|
2020-04-20 13:10:13 +02:00
|
|
|
*
|
|
|
|
* @param directionID The numerical identifier assigned to the direction
|
2020-02-17 21:12:04 +01:00
|
|
|
*/
|
|
|
|
Direction(int directionID) {
|
|
|
|
this.directionID = directionID;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets a direction from its numerical id
|
2020-04-20 13:10:13 +02:00
|
|
|
*
|
|
|
|
* @param directionID The numerical representation of a direction
|
|
|
|
* @return The enum value representing the direction, or null if the id is invalid
|
2020-02-17 21:12:04 +01:00
|
|
|
*/
|
|
|
|
public static Direction getDirectionFromID(int directionID) {
|
|
|
|
for (Direction direction : Direction.values()) {
|
|
|
|
if (direction.directionID == directionID) {
|
|
|
|
return direction;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
2020-02-22 22:31:47 +01:00
|
|
|
|
2020-03-23 12:45:50 +01:00
|
|
|
/**
|
|
|
|
* Checks whether two directions are perpendicular
|
2020-04-20 13:10:13 +02:00
|
|
|
*
|
2020-03-23 12:45:50 +01:00
|
|
|
* @param direction1 The first direction
|
|
|
|
* @param direction2 The second direction
|
|
|
|
* @return True if the directions are perpendicular
|
|
|
|
*/
|
|
|
|
public static boolean arePerpendicular(Direction direction1, Direction direction2) {
|
|
|
|
return direction1.equals(getLeftRotatedDirection(direction2)) ||
|
|
|
|
direction1.equals(getRightRotatedDirection(direction2));
|
|
|
|
}
|
|
|
|
|
2020-02-22 22:31:47 +01:00
|
|
|
/**
|
|
|
|
* Gets the reverse of a direction
|
2020-04-20 13:10:13 +02:00
|
|
|
*
|
2020-02-22 22:31:47 +01:00
|
|
|
* @param direction A direction
|
|
|
|
* @return The reverse direction
|
|
|
|
*/
|
|
|
|
public static Direction getReverseDirection(Direction direction) {
|
2020-02-23 19:43:19 +01:00
|
|
|
return getDirectionFromID((((direction.getDirectionID() + 3) % 8) + 1));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the direction if something rotated to the left
|
|
|
|
*
|
2020-04-20 13:10:13 +02:00
|
|
|
* <p>A rotation is assumed to be a ninety degrees rotation, so NORTH would become WEST and so on.</p>
|
2020-02-23 19:43:19 +01:00
|
|
|
*
|
|
|
|
* @param direction A direction
|
|
|
|
* @return The left rotated direction
|
|
|
|
*/
|
|
|
|
public static Direction getLeftRotatedDirection(Direction direction) {
|
|
|
|
return getDirectionFromID(((((direction.getDirectionID() - 3) + 8) % 8) + 1));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the direction if something rotated to the right
|
|
|
|
*
|
2020-04-20 13:10:13 +02:00
|
|
|
* <p>A rotation is assumed to be a ninety degrees rotation, so NORTH would become EAST and so on.</p>
|
2020-02-23 19:43:19 +01:00
|
|
|
*
|
|
|
|
* @param direction A direction
|
|
|
|
* @return The left rotated direction
|
|
|
|
*/
|
|
|
|
public static Direction getRightRotatedDirection(Direction direction) {
|
|
|
|
return getDirectionFromID((((direction.getDirectionID() + 1) % 8) + 1));
|
2020-02-22 22:31:47 +01:00
|
|
|
}
|
2020-04-20 13:10:13 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the numerical identifier used for alternate identification of a direction
|
|
|
|
*
|
|
|
|
* @return The numerical id of the direction
|
|
|
|
*/
|
|
|
|
public int getDirectionID() {
|
|
|
|
return this.directionID;
|
|
|
|
}
|
2020-02-17 21:12:04 +01:00
|
|
|
}
|