RuneHive-Game
Loading...
Searching...
No Matches
Direction.java
Go to the documentation of this file.
1package com.runehive.game.world.entity.mob;
2
3import com.google.common.collect.ImmutableList;
4import com.runehive.game.world.position.Position;
5import com.runehive.util.Utility;
6
7/**
8 * Represents the enumerated directions an entity can walk or face.
9 *
10 * @author SeVen
11 */
12public enum Direction {
13 NORTH(0, 1),
15 EAST(1, 0),
16 SOUTH_EAST(1, -1),
17 SOUTH(0, -1),
18 SOUTH_WEST(-1, -1),
19 WEST(-1, 0),
20 NORTH_WEST(-1, 1),
21 NONE(0, 0);
22
23 private final int directionX;
24 private final int directionY;
25 private final Position faceLocation;
27 this.directionX = directionX;
28 this.directionY = directionY;
29 this.faceLocation = new Position(directionX, directionY);
30 }
31
32
33 public static final ImmutableList<Direction> DIRECTIONS = ImmutableList.of(NORTH_WEST, NORTH, NORTH_EAST, WEST, EAST, SOUTH_WEST, SOUTH, SOUTH_EAST);
35
37 return faceLocation;
38 }
39
40 /**
41 * Difference in X coordinates for directions array.
42 */
43 public static final byte[] DELTA_X = new byte[]{-1, 0, 1, -1, 1, -1, 0, 1};
44
45 /**
46 * Difference in Y coordinates for directions array.
47 */
48 public static final byte[] DELTA_Y = new byte[]{1, 1, 1, 0, 0, -1, -1, -1};
49
50 /**
51 * Gets the direction between two locations.
52 *
53 * @return The direction of the other position.
54 */
55 public static Direction getDirection(int deltaX, int deltaY) {
56 if (deltaY >= 1) {
57 if (deltaX >= 1) {
58 return NORTH_EAST;
59 } else if (deltaX == 0) {
60 return NORTH;
61 } else if (deltaX <= -1) {
62 return NORTH_WEST;
63 }
64 } else if (deltaY == 0) {
65 if (deltaX >= 1) {
66 return EAST;
67 } else if (deltaX == 0) {
68 return NONE;
69 } else if (deltaX <= -1) {
70 return WEST;
71 }
72 } else if (deltaY <= -1) {
73 if (deltaX >= 1) {
74 return SOUTH_EAST;
75 } else if (deltaX == 0) {
76 return SOUTH;
77 } else if (deltaX <= -1) {
78 return SOUTH_WEST;
79 }
80 }
81
82 return Direction.NONE;
83
84 }
85
86 /**
87 * Gets the direction between two locations.
88 *
89 * @param position The position that will be the viewpoint.
90 * @param other The other position to get the direction of.
91 * @return The direction of the other position.
92 */
94 final int deltaX = other.getX() - position.getX();
95 final int deltaY = other.getY() - position.getY();
96
97 if (deltaY >= 1) {
98 if (deltaX >= 1) {
99 return NORTH_EAST;
100 } else if (deltaX == 0) {
101 return NORTH;
102 } else if (deltaX <= -1) {
103 return NORTH_WEST;
104 }
105 } else if (deltaY == 0) {
106 if (deltaX >= 1) {
107 return EAST;
108 } else if (deltaX == 0) {
109 return NONE;
110 } else if (deltaX <= -1) {
111 return WEST;
112 }
113 } else if (deltaY <= -1) {
114 if (deltaX >= 1) {
115 return SOUTH_EAST;
116 } else if (deltaX == 0) {
117 return SOUTH;
118 } else if (deltaX <= -1) {
119 return SOUTH_WEST;
120 }
121 }
122
123 return Direction.NONE;
124
125 }
126
128 int random = Utility.random(4);
129 switch (random) {
130 case 1: return NORTH;
131 case 2: return EAST;
132 case 3: return SOUTH;
133 case 4: return WEST;
134 default: return SOUTH;
135 }
136 }
137
138 /**
139 * Gets the direction between two locations, ignoring corners.
140 *
141 * @param position The position that will be the viewpoint.
142 * @param other The other position to get the direction of.
143 * @return The direction of the other position.
144 */
146 final int deltaX = other.getX() - position.getX();
147 final int deltaY = other.getY() - position.getY();
148 if (deltaY >= 1) {
149 if (deltaX == 0) {
150 return NORTH;
151 }
152 } else if (deltaY == 0) {
153 if (deltaX >= 1) {
154 return Direction.EAST;
155 } else if (deltaX == 0) {
156 return Direction.SOUTH;
157 } else if (deltaX <= -1) {
158 return Direction.WEST;
159 }
160 } else if (deltaY <= -1) {
161 if (deltaX == 0) {
162 return SOUTH;
163 }
164 }
165 return Direction.SOUTH;
166
167 }
168
170 switch (direction) {
171 case EAST:
172 return WEST;
173 case NORTH:
174 return SOUTH;
175 case NORTH_EAST:
176 return SOUTH_WEST;
177 case NORTH_WEST:
178 return SOUTH_EAST;
179 case SOUTH:
180 return NORTH;
181 case SOUTH_EAST:
182 return NORTH_WEST;
183 case SOUTH_WEST:
184 return NORTH_EAST;
185 case WEST:
186 return EAST;
187 default:
188 return NONE;
189 }
190 }
191
192 public static Direction getFollowDirection(Position source, Position target) {
193 Direction dir = getDirection(source, target);
194 int deltaX = Math.abs(source.getX() - target.getX());
195 int deltaY = Math.abs(source.getY() - target.getY());
196 boolean vertical = deltaY > deltaX;
197 switch (dir) {
198 case NORTH_EAST:
199 if (vertical) return NORTH;
200 else return EAST;
201 case NORTH_WEST:
202 if (vertical) return NORTH;
203 else return WEST;
204 case SOUTH_EAST:
205 if (vertical) return SOUTH;
206 else return EAST;
207 case SOUTH_WEST:
208 if (vertical) return SOUTH;
209 else return WEST;
210 default: return dir;
211 }
212 }
213
214 /**
215 * Gets the orientation for door.
216 *
217 * @param direction The direction of this object.
218 * @return The orientation.
219 */
221 switch (direction) {
222 case WEST:
223 return 0;
224 case NORTH:
225 return 1;
226 case EAST:
227 return 2;
228 case SOUTH:
229 return 3;
230 default:
231 return 3;
232 }
233 }
234
235 /**
236 * Gets the Manhattan direction of an orientation. Manhattan direction does
237 * not support diagnal directions, so the orientation must be between [0, 3]
238 * inclusive.
239 *
240 * @param orientation
241 * @return
242 */
243 public static Direction ofManhattan(int orientation) {
244 switch (orientation) {
245 case 0:
246 return EAST;
247 case 1:
248 return SOUTH;
249 case 2:
250 return WEST;
251 case 3:
252 return NORTH;
253 }
254 return NONE;
255 }
256
258 switch (this) {
259 case NORTH_WEST:
260 return 0;
261 case NORTH:
262 return 1;
263 case NORTH_EAST:
264 return 2;
265 case WEST:
266 return 3;
267 case EAST:
268 return 4;
269 case SOUTH_WEST:
270 return 5;
271 case SOUTH:
272 return 6;
273 case SOUTH_EAST:
274 return 7;
275 default:
276 return -1;
277 }
278 }
279
280 public static int direction(int dx, int dy) {
281 if (dx < 0) {
282 if (dy < 0) {
283 return 5;
284 } else if (dy > 0) {
285 return 0;
286 } else {
287 return 3;
288 }
289 } else if (dx > 0) {
290 if (dy < 0) {
291 return 7;
292 } else if (dy > 0) {
293 return 2;
294 } else {
295 return 4;
296 }
297 } else {
298 if (dy < 0) {
299 return 6;
300 } else if (dy > 0) {
301 return 1;
302 } else {
303 return -1;
304 }
305 }
306 }
307
308 public int getDirectionX() {
309 return directionX;
310 }
311 public int getDirectionY() {
312 return directionY;
313 }
314
315 public static Direction[] valid() {
316 return VALID;
317 }
318
319}
Represents a single tile on the game world.
Definition Position.java:14
int getY()
Gets the absolute y coordinate.
Definition Position.java:46
int getX()
Gets the absolute x coordinate.
Definition Position.java:41
Handles miscellaneous methods.
Definition Utility.java:27
static int random(int bound)
Definition Utility.java:239
static Direction getDirection(int deltaX, int deltaY)
Gets the direction between two locations.
static Direction ofManhattan(int orientation)
Gets the Manhattan direction of an orientation.
static Direction getDirection(Position position, Position other)
Gets the direction between two locations.
static Direction getManhattanDirection(Position position, Position other)
Gets the direction between two locations, ignoring corners.
static final byte[] DELTA_Y
Difference in Y coordinates for directions array.
static Direction getFollowDirection(Position source, Position target)
Direction(int directionX, int directionY)
static final ImmutableList< Direction > DIRECTIONS
static Direction getOppositeDirection(Direction direction)
static final byte[] DELTA_X
Difference in X coordinates for directions array.
static int getDoorOrientation(Direction direction)
Gets the orientation for door.