Originally Posted by
Typical Name
You're correct. The code you shared is responsible for determining the movement direction when an NPC is following a target in the MovementQueue.java file. The code prioritizes west/east movement over north/south movement when the target is moving.
To address the issue you described, you can modify this code block to prioritize diagonal movement when the NPC is following a moving target. This change will allow the NPC to move diagonally instead of mirroring the player's horizontal movement without progressing towards them.
Here's an updated version of the code that incorporates the changes:
switch (direction) {
case NORTH_WEST:
if (RegionManager.canMove(current, Direction.NORTH_WEST, size, character.getPrivateArea())) {
direction = Direction.NORTH_WEST;
} else if (RegionManager.canMove(current, Direction.WEST, size, character.getPrivateArea())) {
direction = Direction.WEST;
} else if (RegionManager.canMove(current, Direction.NORTH, size, character.getPrivateArea())) {
direction = Direction.NORTH;
} else {
direction = Direction.NONE;
}
break;
case NORTH_EAST:
if (RegionManager.canMove(current, Direction.NORTH_EAST, size, character.getPrivateArea())) {
direction = Direction.NORTH_EAST;
} else if (RegionManager.canMove(current, Direction.NORTH, size, character.getPrivateArea())) {
direction = Direction.NORTH;
} else if (RegionManager.canMove(current, Direction.EAST, size, character.getPrivateArea())) {
direction = Direction.EAST;
} else {
direction = Direction.NONE;
}
break;
case SOUTH_WEST:
if (RegionManager.canMove(current, Direction.SOUTH_WEST, size, character.getPrivateArea())) {
direction = Direction.SOUTH_WEST;
} else if (RegionManager.canMove(current, Direction.WEST, size, character.getPrivateArea())) {
direction = Direction.WEST;
} else if (RegionManager.canMove(current, Direction.SOUTH, size, character.getPrivateArea())) {
direction = Direction.SOUTH;
} else {
direction = Direction.NONE;
}
break;
case SOUTH_EAST:
if (RegionManager.canMove(current, Direction.SOUTH_EAST, size, character.getPrivateArea())) {
direction = Direction.SOUTH_EAST;
} else if (RegionManager.canMove(current, Direction.EAST, size, character.getPrivateArea())) {
direction = Direction.EAST;
} else if (RegionManager.canMove(current, Direction.SOUTH, size, character.getPrivateArea())) {
direction = Direction.SOUTH;
} else {
direction = Direction.NONE;
}
break;
default:
break;
}
With this modification, the NPC will first attempt to move diagonally towards the target if the diagonal direction is valid. If diagonal movement is not possible, it will fall back to horizontal or vertical movement.
Please note that this code snippet assumes the presence of the Direction.NORTH_WEST, Direction.NORTH_EAST, Direction.SOUTH_WEST, and Direction.SOUTH_EAST directions in the codebase. If these directions are not available, you may need to define them accordingly or adapt the code to match the existing direction constants in your project.