FXGL
FXGL copied to clipboard
Built-in component for top-down movement
Consider pacman, bomberman and battletanks games. They have very similar code wrt movement that can be provided in a component.
Collisions vs the world cells / tiles should be considered.
CellMoveComponent and AStarMoveComponent work nicely together for use cases where entities move using cells. For other "free" type movement, we need TopDownMoveComponent, which also considers collisions.
// private List<Entity> blocks;
//
// private void move(double dx, double dy) {
// if (!getEntity().isActive())
// return;
//
// if (blocks == null) {
// blocks = FXGL.getApp().getGameWorld().getEntitiesByType(PacmanType.BLOCK);
// }
//
// double mag = Math.sqrt(dx * dx + dy * dy);
// long length = Math.round(mag);
//
// double unitX = dx / mag;
// double unitY = dy / mag;
//
// for (int i = 0; i < length; i++) {
// position.translate(unitX, unitY);
//
// boolean collision = false;
//
// for (int j = 0; j < blocks.size(); j++) {
// if (blocks.get(j).getBoundingBoxComponent().isCollidingWith(bbox)) {
// collision = true;
// break;
// }
// }
//
// if (collision) {
// position.translate(-unitX, -unitY);
// break;
// }
// }
// }