FXGL icon indicating copy to clipboard operation
FXGL copied to clipboard

Built-in component for top-down movement

Open AlmasB opened this issue 6 years ago • 2 comments

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.

AlmasB avatar Dec 03 '19 13:12 AlmasB

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.

AlmasB avatar Jul 30 '20 17:07 AlmasB

//    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;
//            }
//        }
//    }

AlmasB avatar Jul 30 '20 17:07 AlmasB