44public enum Direction {
55 private final int directionX;
56 private final int directionY;
58 Direction(
int directionX,
int directionY) {
59 this.directionX = directionX;
60 this.directionY = directionY;
61 this.faceLocation =
new Position(directionX, directionY);
65 public static final ImmutableList<Direction> DIRECTIONS = ImmutableList.of(NORTH_WEST, NORTH, NORTH_EAST, WEST, EAST, SOUTH_WEST, SOUTH, SOUTH_EAST);
66 private static final Direction[] VALID =
new Direction[]{NORTH, NORTH_EAST, EAST, SOUTH_EAST, SOUTH, SOUTH_WEST, WEST, NORTH_WEST};
75 public static final byte[]
DELTA_X =
new byte[]{-1, 0, 1, -1, 1, -1, 0, 1};
80 public static final byte[]
DELTA_Y =
new byte[]{1, 1, 1, 0, 0, -1, -1, -1};
91 }
else if (deltaX == 0) {
93 }
else if (deltaX <= -1) {
96 }
else if (deltaY == 0) {
99 }
else if (deltaX == 0) {
101 }
else if (deltaX <= -1) {
104 }
else if (deltaY <= -1) {
107 }
else if (deltaX == 0) {
109 }
else if (deltaX <= -1) {
114 return Direction.NONE;
126 final int deltaX = other.
getX() - position.getX();
127 final int deltaY = other.
getY() - position.getY();
132 }
else if (deltaX == 0) {
134 }
else if (deltaX <= -1) {
137 }
else if (deltaY == 0) {
140 }
else if (deltaX == 0) {
142 }
else if (deltaX <= -1) {
145 }
else if (deltaY <= -1) {
148 }
else if (deltaX == 0) {
150 }
else if (deltaX <= -1) {
155 return Direction.NONE;
159 public static Direction getRandomDirection() {
160 int random =
Utility.random(4);
162 case 1:
return NORTH;
164 case 3:
return SOUTH;
166 default:
return SOUTH;
178 final int deltaX = other.
getX() - position.getX();
179 final int deltaY = other.
getY() - position.getY();
184 }
else if (deltaY == 0) {
186 return Direction.EAST;
187 }
else if (deltaX == 0) {
188 return Direction.SOUTH;
189 }
else if (deltaX <= -1) {
190 return Direction.WEST;
192 }
else if (deltaY <= -1) {
197 return Direction.SOUTH;
224 public static Direction getFollowDirection(
Position source,
Position target) {
225 Direction dir = getDirection(source, target);
226 int deltaX = Math.abs(source.
getX() - target.
getX());
227 int deltaY = Math.abs(source.
getY() - target.
getY());
228 boolean vertical = deltaY > deltaX;
231 if (vertical)
return NORTH;
234 if (vertical)
return NORTH;
237 if (vertical)
return SOUTH;
240 if (vertical)
return SOUTH;
276 switch (orientation) {
289 public int getManhattanOrientation() {
312 public static int direction(
int dx,
int dy) {
340 public int getDirectionX() {
343 public int getDirectionY() {