Skip to content

Commit f899469

Browse files
committed
Add rotation functions to Direction
1 parent 84f05ef commit f899469

File tree

1 file changed

+89
-1
lines changed

1 file changed

+89
-1
lines changed

game/src/main/java/org/apollo/game/model/Direction.java

Lines changed: 89 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,95 @@ public static Direction[] diagonalComponents(Direction direction) {
161161
}
162162

163163
/**
164-
* Gets the opposite direction of the this direction.
164+
* Gets the direction that is clockwise of this direction.
165+
*
166+
* @return The clockwise direction.
167+
*/
168+
public Direction clockwise() {
169+
switch (this) {
170+
case NORTH:
171+
return NORTH_EAST;
172+
case SOUTH:
173+
return SOUTH_WEST;
174+
case EAST:
175+
return SOUTH_EAST;
176+
case WEST:
177+
return NORTH_WEST;
178+
case NORTH_WEST:
179+
return NORTH;
180+
case NORTH_EAST:
181+
return EAST;
182+
case SOUTH_EAST:
183+
return SOUTH;
184+
case SOUTH_WEST:
185+
return WEST;
186+
}
187+
188+
return NONE;
189+
}
190+
191+
/**
192+
* Gets a direction that is clockwise of this direction, given the number of times to rotate.
193+
*
194+
* @param count The number of times to apply the rotation.
195+
* @return The rotated direction.
196+
*/
197+
public Direction clockwise(int count) {
198+
Direction direction = this;
199+
200+
for (int index = 0; index < count; index++) {
201+
direction = direction.clockwise();
202+
}
203+
204+
return direction;
205+
}
206+
207+
/**
208+
* Gets the direction that is counter-clockwise of this direction.
209+
*
210+
* @return The counter-clockwise direction.
211+
*/
212+
public Direction counterClockwise() {
213+
switch (this) {
214+
case NORTH:
215+
return NORTH_WEST;
216+
case SOUTH:
217+
return SOUTH_EAST;
218+
case EAST:
219+
return NORTH_EAST;
220+
case WEST:
221+
return SOUTH_WEST;
222+
case NORTH_WEST:
223+
return WEST;
224+
case NORTH_EAST:
225+
return NORTH;
226+
case SOUTH_EAST:
227+
return EAST;
228+
case SOUTH_WEST:
229+
return SOUTH;
230+
}
231+
232+
return NONE;
233+
}
234+
235+
/**
236+
* Gets a direction that is counter-clockwise of this direction, given the number of times to rotate.
237+
*
238+
* @param count The number of times to apply the rotation.
239+
* @return The rotated direction.
240+
*/
241+
public Direction counterClockwise(int count) {
242+
Direction direction = this;
243+
244+
for (int index = 0; index < count; index++) {
245+
direction = direction.counterClockwise();
246+
}
247+
248+
return direction;
249+
}
250+
251+
/**
252+
* Gets the opposite direction of this direction.
165253
*
166254
* @return The opposite direction.
167255
*/

0 commit comments

Comments
 (0)