Search in sources :

Example 1 with CellMoveComponent

use of com.almasb.fxgl.pathfinding.CellMoveComponent in project FXGL by AlmasB.

the class SenseAISample method spawnNPC.

private void spawnNPC(int x, int y) {
    var rect = new Rectangle(20, 20, FXGLMath.randomColor().brighter());
    rect.setStrokeType(StrokeType.INSIDE);
    rect.setStroke(Color.BLACK);
    var sense = new HearingSenseComponent(150);
    var text = getUIFactoryService().newText("", Color.WHITE, 16.0);
    text.textProperty().bind(sense.stateProperty().asString());
    var hearingRadiusCircle = new Circle(150);
    hearingRadiusCircle.setFill(null);
    hearingRadiusCircle.setStroke(Color.GREEN);
    var e = entityBuilder().type(Type.NPC).viewWithBBox(rect).view(text).view(hearingRadiusCircle).anchorFromCenter().with(new CellMoveComponent(CELL_WIDTH, CELL_HEIGHT, 150)).with(new AStarMoveComponent(grid)).with(sense).with(new CustomAIComponent()).buildAndAttach();
    e.getComponent(AStarMoveComponent.class).stopMovementAt(x, y);
}
Also used : Circle(javafx.scene.shape.Circle) CellMoveComponent(com.almasb.fxgl.pathfinding.CellMoveComponent) Rectangle(javafx.scene.shape.Rectangle) HearingSenseComponent(com.almasb.fxgl.ai.senseai.HearingSenseComponent) AStarMoveComponent(com.almasb.fxgl.pathfinding.astar.AStarMoveComponent)

Example 2 with CellMoveComponent

use of com.almasb.fxgl.pathfinding.CellMoveComponent in project FXGL by AlmasB.

the class PathfindingSample method initGame.

@Override
protected void initGame() {
    // 2. init grid width x height
    grid = new AStarGrid(GRID_WIDTH, GRID_HEIGHT);
    pathfinder = new AStarPathfinder(grid);
    grid.forEach(c -> {
        var x = c.getX();
        var y = c.getY();
        Rectangle rect = new Rectangle(CELL_WIDTH - 2, CELL_HEIGHT - 2);
        rect.setFill(Color.WHITE);
        rect.setStroke(Color.BLACK);
        Entity tile = entityBuilder().at(x * CELL_WIDTH, y * CELL_HEIGHT).view(rect).buildAndAttach();
        rect.setOnMouseClicked(e -> {
            // if left click do search, else place a red obstacle
            if (e.getButton() == MouseButton.PRIMARY) {
                var cells = pathfinder.findPath(0, 0, x, y);
                cells.forEach(cell -> {
                    getGameWorld().getEntitiesAt(new Point2D(cell.getX() * CELL_WIDTH, cell.getY() * CELL_HEIGHT)).forEach(Entity::removeFromWorld);
                });
            } else {
                grid.get(x, y).setState(CellState.NOT_WALKABLE);
                rect.setFill(Color.RED);
            }
        });
    });
    var e = entityBuilder().viewWithBBox(new Rectangle(20, 20)).with(new CellMoveComponent(CELL_WIDTH, CELL_HEIGHT, 100)).buildAndAttach();
    e.getComponent(CellMoveComponent.class).setPositionToCell(1, 1);
}
Also used : AStarPathfinder(com.almasb.fxgl.pathfinding.astar.AStarPathfinder) Entity(com.almasb.fxgl.entity.Entity) CellMoveComponent(com.almasb.fxgl.pathfinding.CellMoveComponent) Point2D(javafx.geometry.Point2D) Rectangle(javafx.scene.shape.Rectangle) AStarGrid(com.almasb.fxgl.pathfinding.astar.AStarGrid)

Example 3 with CellMoveComponent

use of com.almasb.fxgl.pathfinding.CellMoveComponent in project FXGL by AlmasB.

the class MassUnitsRTSSample method initGame.

@Override
protected void initGame() {
    // 2. init grid width x height
    grid = new AStarGrid(GRID_WIDTH, GRID_HEIGHT);
    pathfinder = new AStarPathfinder(grid);
    grid.forEach(c -> {
        var x = c.getX();
        var y = c.getY();
        Rectangle rect = new Rectangle(CELL_WIDTH - 2, CELL_HEIGHT - 2);
        rect.setFill(Color.WHITE);
        // rect.setStroke(Color.BLACK);
        Entity tile = entityBuilder().at(x * CELL_WIDTH, y * CELL_HEIGHT).view(rect).with(new IDComponent("" + x + "," + y, 0)).buildAndAttach();
        rect.setOnMouseClicked(event -> {
            // if left click do search, else place a red obstacle
            if (event.getButton() == MouseButton.PRIMARY) {
                var units = getGameWorld().getEntitiesFiltered(e -> e.getPropertyOptional("type").isPresent());
                var cells = grid.getWalkableCells().stream().sorted(Comparator.comparingInt(cell -> cell.distance(grid.get(x, y)))).limit(units.size()).collect(Collectors.toList());
                Collections.shuffle(cells);
                for (int i = 0; i < units.size(); i++) {
                    var unit = units.get(i);
                    var cell = cells.get(i);
                    unit.getComponent(AStarMoveComponent.class).moveToCell(cell);
                }
            }
        });
    });
    for (int i = 0; i < 485; i++) {
        var color = FXGLMath.randomColor().darker().darker();
        var e = entityBuilder().viewWithBBox(new Circle(CELL_WIDTH / 4, CELL_WIDTH / 4, CELL_WIDTH / 4, color)).with(new CellMoveComponent(CELL_WIDTH, CELL_HEIGHT, 300)).with(new AStarMoveComponent(grid)).with("type", "unit").buildAndAttach();
    }
}
Also used : Color(javafx.scene.paint.Color) AStarPathfinder(com.almasb.fxgl.pathfinding.astar.AStarPathfinder) MouseButton(javafx.scene.input.MouseButton) AStarMoveComponent(com.almasb.fxgl.pathfinding.astar.AStarMoveComponent) FXGLMath(com.almasb.fxgl.core.math.FXGLMath) Rectangle(javafx.scene.shape.Rectangle) CellMoveComponent(com.almasb.fxgl.pathfinding.CellMoveComponent) Collectors(java.util.stream.Collectors) AStarGrid(com.almasb.fxgl.pathfinding.astar.AStarGrid) IDComponent(com.almasb.fxgl.entity.components.IDComponent) GameSettings(com.almasb.fxgl.app.GameSettings) CellState(com.almasb.fxgl.pathfinding.CellState) Circle(javafx.scene.shape.Circle) Comparator(java.util.Comparator) GameApplication(com.almasb.fxgl.app.GameApplication) Collections(java.util.Collections) Entity(com.almasb.fxgl.entity.Entity) FXGL(com.almasb.fxgl.dsl.FXGL) AStarPathfinder(com.almasb.fxgl.pathfinding.astar.AStarPathfinder) Entity(com.almasb.fxgl.entity.Entity) Circle(javafx.scene.shape.Circle) CellMoveComponent(com.almasb.fxgl.pathfinding.CellMoveComponent) Rectangle(javafx.scene.shape.Rectangle) IDComponent(com.almasb.fxgl.entity.components.IDComponent) AStarMoveComponent(com.almasb.fxgl.pathfinding.astar.AStarMoveComponent) AStarGrid(com.almasb.fxgl.pathfinding.astar.AStarGrid)

Example 4 with CellMoveComponent

use of com.almasb.fxgl.pathfinding.CellMoveComponent in project FXGL by AlmasB.

the class RandomPathfindingSample method spawnNPC.

private void spawnNPC(int x, int y) {
    var e = entityBuilder().viewWithBBox(new Rectangle(20, 20, FXGLMath.randomColor())).anchorFromCenter().with(new CellMoveComponent(CELL_WIDTH, CELL_HEIGHT, 150)).with(new AStarMoveComponent(grid)).with(new RandomAStarMoveComponent(1, 7, Duration.seconds(1), Duration.seconds(3))).buildAndAttach();
    e.getComponent(AStarMoveComponent.class).stopMovementAt(x, y);
}
Also used : CellMoveComponent(com.almasb.fxgl.pathfinding.CellMoveComponent) RandomAStarMoveComponent(com.almasb.fxgl.dsl.components.RandomAStarMoveComponent) Rectangle(javafx.scene.shape.Rectangle) AStarMoveComponent(com.almasb.fxgl.pathfinding.astar.AStarMoveComponent) RandomAStarMoveComponent(com.almasb.fxgl.dsl.components.RandomAStarMoveComponent)

Aggregations

CellMoveComponent (com.almasb.fxgl.pathfinding.CellMoveComponent)4 Rectangle (javafx.scene.shape.Rectangle)4 AStarMoveComponent (com.almasb.fxgl.pathfinding.astar.AStarMoveComponent)3 Entity (com.almasb.fxgl.entity.Entity)2 AStarGrid (com.almasb.fxgl.pathfinding.astar.AStarGrid)2 AStarPathfinder (com.almasb.fxgl.pathfinding.astar.AStarPathfinder)2 Circle (javafx.scene.shape.Circle)2 HearingSenseComponent (com.almasb.fxgl.ai.senseai.HearingSenseComponent)1 GameApplication (com.almasb.fxgl.app.GameApplication)1 GameSettings (com.almasb.fxgl.app.GameSettings)1 FXGLMath (com.almasb.fxgl.core.math.FXGLMath)1 FXGL (com.almasb.fxgl.dsl.FXGL)1 RandomAStarMoveComponent (com.almasb.fxgl.dsl.components.RandomAStarMoveComponent)1 IDComponent (com.almasb.fxgl.entity.components.IDComponent)1 CellState (com.almasb.fxgl.pathfinding.CellState)1 Collections (java.util.Collections)1 Comparator (java.util.Comparator)1 Collectors (java.util.stream.Collectors)1 Point2D (javafx.geometry.Point2D)1 MouseButton (javafx.scene.input.MouseButton)1