A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://github.com/AlmasB/FXGL/commit/fac790bdf below:

added atDestinationProperty() to AStarMoveComponent, closes #945 · AlmasB/FXGL@fac790b · GitHub

1 +

/*

2 +

* FXGL - JavaFX Game Library. The MIT License (MIT).

3 +

* Copyright (c) AlmasB (almaslvl@gmail.com).

4 +

* See LICENSE for details.

5 +

*/

6 + 7 +

package intermediate.ai.pathfinding;

8 + 9 +

import com.almasb.fxgl.app.GameApplication;

10 +

import com.almasb.fxgl.app.GameSettings;

11 +

import com.almasb.fxgl.entity.Entity;

12 +

import com.almasb.fxgl.pathfinding.CellMoveComponent;

13 +

import com.almasb.fxgl.pathfinding.CellState;

14 +

import com.almasb.fxgl.pathfinding.astar.AStarGrid;

15 +

import com.almasb.fxgl.pathfinding.astar.AStarMoveComponent;

16 +

import javafx.scene.input.MouseButton;

17 +

import javafx.scene.paint.Color;

18 +

import javafx.scene.shape.Rectangle;

19 +

import javafx.scene.shape.StrokeType;

20 + 21 +

import static com.almasb.fxgl.dsl.FXGL.debug;

22 +

import static com.almasb.fxgl.dsl.FXGL.entityBuilder;

23 + 24 +

/**

25 +

* Demo that uses A* search to find a path between 2 cells in a grid.

26 +

* Right click to place a wall, left click to move.

27 +

*

28 +

* @author Almas Baimagambetov (AlmasB) (almaslvl@gmail.com)

29 +

*/

30 +

public class AStarPathfindingSample extends GameApplication {

31 + 32 +

private Entity agent;

33 + 34 +

@Override

35 +

protected void initSettings(GameSettings settings) {

36 +

settings.setWidth(1280);

37 +

settings.setHeight(720);

38 +

settings.setDeveloperMenuEnabled(true);

39 +

}

40 + 41 +

@Override

42 +

protected void initGame() {

43 +

var grid = new AStarGrid(1280 / 40, 720 / 40);

44 + 45 +

agent = entityBuilder()

46 +

.viewWithBBox(new Rectangle(40, 40, Color.BLUE))

47 +

.with(new CellMoveComponent(40, 40, 150))

48 +

.with(new AStarMoveComponent(grid))

49 +

.zIndex(1)

50 +

.anchorFromCenter()

51 +

.buildAndAttach();

52 + 53 +

agent.getComponent(CellMoveComponent.class).atDestinationProperty().addListener((o, old, isAtDestination) -> {

54 +

if (isAtDestination) {

55 +

debug("CellMoveComponent: reached destination");

56 +

}

57 +

});

58 + 59 +

agent.getComponent(AStarMoveComponent.class).atDestinationProperty().addListener((o, old, isAtDestination) -> {

60 +

if (isAtDestination) {

61 +

debug("AStarMoveComponent: reached destination");

62 +

}

63 +

});

64 + 65 +

for (int y = 0; y < 720 / 40; y++) {

66 +

for (int x = 0; x < 1280 / 40; x++) {

67 +

final var finalX = x;

68 +

final var finalY = y;

69 + 70 +

var view = new Rectangle(40, 40, Color.WHITE);

71 +

view.setStroke(Color.color(0, 0, 0, 0.25));

72 +

view.setStrokeType(StrokeType.INSIDE);

73 + 74 +

var e = entityBuilder()

75 +

.at(x * 40, y * 40)

76 +

.view(view)

77 +

.buildAndAttach();

78 + 79 +

e.getViewComponent().addOnClickHandler(event -> {

80 +

if (event.getButton() == MouseButton.PRIMARY) {

81 +

agent.getComponent(AStarMoveComponent.class).moveToCell(finalX, finalY);

82 + 83 +

} else if (event.getButton() == MouseButton.SECONDARY) {

84 +

grid.get(finalX, finalY).setState(CellState.NOT_WALKABLE);

85 +

view.setFill(Color.RED);

86 +

}

87 +

});

88 +

}

89 +

}

90 +

}

91 + 92 +

public static void main(String[] args) {

93 +

launch(args);

94 +

}

95 +

}


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4