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)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| 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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| 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 { return null; } } else { 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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| public void update(GameContainer container, int delta) { updateMoveTimer(delta); if (getReadyToMove()) { if (route != null) { int desX = route.getStep(stepIndex + 1).getX(); int desY = route.getStep(stepIndex + 1).getY(); int diffX = desX - tileX; int diffY = desY - tileY; } } } |
Thanks in advance for all your help
