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 sandbox.ai.pathfinding;
8
+
9
+
import com.almasb.fxgl.app.GameApplication;
10
+
import com.almasb.fxgl.app.GameSettings;
11
+
import com.almasb.fxgl.core.math.FXGLMath;
12
+
import com.almasb.fxgl.dsl.components.RandomAStarMoveComponent;
13
+
import com.almasb.fxgl.pathfinding.CellMoveComponent;
14
+
import com.almasb.fxgl.pathfinding.astar.AStarCell;
15
+
import com.almasb.fxgl.pathfinding.astar.AStarMoveComponent;
16
+
import com.almasb.fxgl.pathfinding.astar.TraversableGrid;
17
+
import com.almasb.fxgl.pathfinding.dungeon.DungeonGrid;
18
+
import com.almasb.fxgl.pathfinding.maze.MazeGrid;
19
+
import javafx.scene.paint.Color;
20
+
import javafx.scene.shape.Line;
21
+
import javafx.scene.shape.Rectangle;
22
+
import javafx.util.Duration;
23
+
24
+
import static com.almasb.fxgl.dsl.FXGL.addUINode;
25
+
import static com.almasb.fxgl.dsl.FXGL.entityBuilder;
26
+
27
+
/**
28
+
* @author Almas Baim (https://github.com/AlmasB)
29
+
*/
30
+
public class MazeGenSample extends GameApplication {
31
+
@Override
32
+
protected void initSettings(GameSettings settings) {
33
+
settings.setWidth(1280);
34
+
settings.setHeight(720);
35
+
}
36
+
37
+
@Override
38
+
protected void initGame() {
39
+
var dungeon = new MazeGrid(30, 26);
40
+
41
+
var scale = 40;
42
+
43
+
var agent = entityBuilder()
44
+
.viewWithBBox(new Rectangle(scale, scale, Color.BLUE))
45
+
.with(new CellMoveComponent(scale, scale, 150))
46
+
.with(new AStarMoveComponent<>(dungeon))
47
+
.zIndex(1)
48
+
.anchorFromCenter()
49
+
.buildAndAttach();
50
+
51
+
for (int y = 0; y < 26; y++) {
52
+
for (int x = 0; x < 30; x++) {
53
+
var finalX = x;
54
+
var finalY = y;
55
+
56
+
var tile = dungeon.get(x, y);
57
+
58
+
var rect = new Rectangle(scale, scale, Color.WHITE);
59
+
60
+
if (tile.hasLeftWall()) {
61
+
var line = new Line(x*scale, y*scale, x*scale, (y+1)*scale);
62
+
line.setStrokeWidth(2);
63
+
line.setStroke(Color.DARKGRAY);
64
+
65
+
addUINode(line);
66
+
}
67
+
68
+
if (tile.hasTopWall()) {
69
+
var line = new Line(x*scale, y*scale, (x+1) * scale, y*scale);
70
+
line.setStrokeWidth(2);
71
+
line.setStroke(Color.DARKGRAY);
72
+
73
+
addUINode(line);
74
+
}
75
+
76
+
if (!tile.isWalkable()) {
77
+
rect.setFill(Color.GRAY);
78
+
} else {
79
+
rect.setFill(Color.WHITE);
80
+
agent.getComponent(AStarMoveComponent.class).stopMovementAt(finalX, finalY);
81
+
82
+
rect.setOnMouseClicked(e -> {
83
+
agent.getComponent(AStarMoveComponent.class).moveToCell(finalX, finalY);
84
+
});
85
+
86
+
if (FXGLMath.randomBoolean(0.09)) {
87
+
spawnNPC(x, y, dungeon);
88
+
}
89
+
}
90
+
91
+
entityBuilder()
92
+
.at(x*scale, y*scale)
93
+
.view(rect)
94
+
.buildAndAttach();
95
+
}
96
+
}
97
+
}
98
+
99
+
private void spawnNPC(int x, int y, TraversableGrid<?> grid) {
100
+
var view = new Rectangle(40, 40, FXGLMath.randomColor().brighter().brighter());
101
+
view.setStroke(Color.BLACK);
102
+
view.setStrokeWidth(2);
103
+
104
+
var e = entityBuilder()
105
+
.zIndex(2)
106
+
.viewWithBBox(view)
107
+
.anchorFromCenter()
108
+
.with(new CellMoveComponent(40, 40, 150))
109
+
.with(new AStarMoveComponent<>(grid))
110
+
.with(new RandomAStarMoveComponent<AStarCell>(1, 7, Duration.seconds(1), Duration.seconds(3)))
111
+
.buildAndAttach();
112
+
113
+
e.getComponent(AStarMoveComponent.class).stopMovementAt(x, y);
114
+
}
115
+
116
+
public static void main(String[] args) {
117
+
launch(args);
118
+
}
119
+
}
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