-
Notifications
You must be signed in to change notification settings - Fork 0
/
NodeBasedAStar.pde
53 lines (47 loc) · 1.1 KB
/
NodeBasedAStar.pde
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
Grid grid;
AStar aStar;
ArrayList<Node> path;
void setup(){
size(800, 800);
grid = new Grid(10);
aStar = new AStar(0,0, false);
}
void draw(){
background(0);
grid.render();
drawPath();
}
void mousePressed(){
setTile();
}
void mouseDragged(){
setTile();
}
void keyReleased(){
if(key == ' '){
grid.clearPaths();
grid.createPaths();
path = aStar.findPath(0, 0, grid.tiles.length-1, grid.tiles[0].length-1);
}
}
public void setTile(){
if(mouseButton == LEFT){
Tile curTile = grid.getTileAtMouse(mouseX,mouseY);
if(curTile!=null) curTile.setOpen(true);
}
if(mouseButton == RIGHT){
Tile curTile = grid.getTileAtMouse(mouseX,mouseY);
if(curTile!=null) curTile.setOpen(false);
}
}
public void drawPath(){
if(path!=null){
for(Node n : path){
strokeWeight(grid.res/4);
stroke(255, 0, 0);
if(n.parent!=null)line(n.x*grid.res+grid.res/2, n.y*grid.res+grid.res/2, n.parent.x*grid.res+grid.res/2, n.parent.y*grid.res+grid.res/2);
strokeWeight(1);
stroke(0);
}
}
}