Well, what the title says. I have no need for advanced pathfinding - my enemies shall be dumb for now.
I really want to keep my DIRECTION enum used, and not just hardcode xc and yc according to direction.
This is what I’m currently doing (…and I’m ashamed of myself, indeed):
public DIRECTION moveTowards(int x, int y) {
float north =
distanceBetween(
this.getX() + DIRECTION.NORTH.getXC(),
this.getY() + DIRECTION.NORTH.getYC(),
x,
y
);
float south =
distanceBetween(
this.getX() + DIRECTION.SOUTH.getXC(),
this.getY() + DIRECTION.SOUTH.getYC(),
x,
y
);
float east =
distanceBetween(
this.getX() + DIRECTION.EAST.getXC(),
this.getY() + DIRECTION.EAST.getYC(),
x,
y
);
float west =
distanceBetween(
this.getX() + DIRECTION.WEST.getXC(),
this.getY() + DIRECTION.WEST.getYC(),
x,
y
);
if (north < south
&& north < west
&& north < east) {
return DIRECTION.NORTH;
} else if (east < south
&& east < west
&& east < north) {
return DIRECTION.EAST;
} else if (south < east
&& south < west
&& south < north) {
return DIRECTION.SOUTH;
} else if (west < south
&& west < west
&& west < north) {
return DIRECTION.WEST;
} else {
return DIRECTION.NOWHERE;
}
}
public float distanceBetween(int x1, int y1, int x2, int y2) {
return Math.abs(x1 - x2) + Math.abs(y1 - y2);
}
The code should be selfexplanatory in what it aims to do - the process of achieving that is just utterly terrible at the moment. It also doesn’t work as intented. Meh.