I think the title might be misleading, but I couldn’t do better.
I have a tiled map, containing monsters. These monsters will walk wherever they please, based on
A*. When they figure out where they want to go, their “path” field will be set with whatever path they should follow.
However, their move method takes these parameters: Direction direction, Tilemap map.
Directions work like this: (this is a little simplified, so it’s easier to read)
public enum DIRECTION {
NORTH(0, -1),
SOUTH(0, 1),
WEST(-1,0),
EAST(1,0),
;
private DIRECTION(int dx, int dy){
}
public int DX()
{
return this.dx;
}
public int DY()
{
return this.dy;
}
}
Now, when they want to move to the next Node in their Path, I can calculate the differences in coordinates.
This gives me the DX and DY, for the direction it needs to go in, but not the actual DIRECTION enum.
How do I get this?
So far, I’ve used a horrible helper, like this:
public static DIRECTION getDirection(int x, int y) {
if (x == 1) {
if (y != 0) {
return null;
}
return DIRECTION.EAST;
} else if (x == -1) {
if (y != 0) {
return null;
}
return DIRECTION.WEST;
} else if (x == 0) {
if (y == 1) {
return DIRECTION.SOUTH;
} else if (y == -1) {
return DIRECTION.NORTH;
} else { // Both x and y are 0, or invalid arguments passed
return null;
}
} else { // Invalid arguments
return null;
}
}
However, this is very ugly and quite a bit of a cheap hack. I’m thinking there is probably a better way of getting the DIRECTION, but
I just can’t think of one!
This is where the code is needed:
public void update(GameContainer container, int delta) {
updateMoveTimer(delta);
if (getReadyToMove()) {
if (route != null) {
//System.out.println(route.getStep(1).getX() + " " + route.getStep(1).getY());
int desX = route.getStep(stepIndex + 1).getX();
int desY = route.getStep(stepIndex + 1).getY();
int diffX = desX - tileX;
int diffY = desY - tileY;
/*
* I want to use the move method from the superclass,
* but I don't know how to convert the differences to directions
*/
}
}
}
Thanks in advance for all your help