RuneHive-Game
Loading...
Searching...
No Matches
PathFinder.java
Go to the documentation of this file.
1package com.runehive.game.world.pathfinding.path;
2
3import com.runehive.game.world.Interactable;
4import com.runehive.game.world.entity.mob.Direction;
5import com.runehive.game.world.entity.mob.Mob;
6import com.runehive.game.world.pathfinding.TraversalMap;
7import com.runehive.game.world.position.Position;
8
9/**
10 * An algorithm used to find a path between two {@link Position}s.
11 *
12 * @author Artem Batutin <artembatutin@gmail.com>
13 */
14public abstract class PathFinder {
15
16 /**
17 * Finds a valid path from the origin {@link Position} to the target one.
18 *
19 *
20 * @param source
21 * @param target The target Position.
22 * @return The path containing the Positions to go through.
23 */
24 public abstract Path find(Mob source, Position target, int targetWidth, int targetLength);
25
26 public final Path find(Mob origin, Interactable target) {
27 return find(origin, target.getPosition(), target.width(), target.length());
28 }
29
30 public final Path find(Mob origin, Position target) {
31 return find(origin, target, 0, 0);
32 }
33
34 /**
35 * Returns whether or not a {@link Position} walking one step in any of the
36 * specified {@link Direction}s would lead to is traversable.
37 *
38 * @param current The current Position.
39 * @param size The entity's size.
40 * @param directions The Directions that should be checked.
41 * @return {@code true} if any of the Directions lead to a traversable tile,
42 * otherwise {@code false}.
43 */
44 protected boolean traversable(Position current, int size, Direction... directions) {
45 for (Direction direction : directions) {
46 if (!TraversalMap.isTraversable(current, direction, size)) {
47 return false;
48 }
49 }
50 return true;
51 }
52
53 /**
54 * Returns whether or not a {@link Position} is traversable to the direction
55 * of another {@link Position}.
56 *
57 * @param current The current Position.
58 * @param going The position to which we are going.
59 * @return {@code true} if any of the Directions lead to a traversable tile,
60 * otherwise {@code false}.
61 */
62 boolean traversable(Position current, Position going, int size) {
63 Direction first = Direction.getDirection(going, current);
64 Direction second = Direction.getDirection(current, going);
65 return TraversalMap.isTraversable(current, second, size) && TraversalMap.isTraversable(going, first, size);
66 }
67
68 /**
69 * Returns whether or not a {@link Position} shooting projectile to another
70 * {@link Position} would lead to is traversable.
71 *
72 * @param current The current Position.
73 * @param going The position to which we are going.
74 * @return {@code true} if any of the Directions lead to a projectile
75 * traversable tile, otherwise {@code false}.
76 */
77 protected boolean projectileCheck(Position current, Position going) {
78 Direction first = Direction.getDirection(going, current);
79 Direction second = Direction.getDirection(current, going);
80 return TraversalMap.isTraversable(current, second, true) && TraversalMap.isTraversable(going, first, true);
81 }
82
83}
Handles the mob class.
Definition Mob.java:66
Contains traversal data for a set of regions.
static boolean isTraversable(Position from, Direction direction, int size)
Tests whether or not a specified position is traversable in the specified direction.
An algorithm used to find a path between two Positions.
boolean traversable(Position current, Position going, int size)
Returns whether or not a Position is traversable to the direction of another Position.
abstract Path find(Mob source, Position target, int targetWidth, int targetLength)
Finds a valid path from the origin Position to the target one.
boolean projectileCheck(Position current, Position going)
Returns whether or not a Position shooting projectile to another Position would lead to is traversabl...
final Path find(Mob origin, Position target)
boolean traversable(Position current, int size, Direction... directions)
Returns whether or not a Position walking one step in any of the specified Directions would lead to i...
final Path find(Mob origin, Interactable target)
Represents a single path in the path finding system.
Definition Path.java:13
Represents a single tile on the game world.
Definition Position.java:14
Represents the enumerated directions an entity can walk or face.
static Direction getDirection(int deltaX, int deltaY)
Gets the direction between two locations.
An object implementing Interactable has uses.