-
Notifications
You must be signed in to change notification settings - Fork 0
/
Grid.pde
71 lines (63 loc) · 1.66 KB
/
Grid.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
public class Grid{
private Tile[][] tiles;
private int res;
private int numX, numY;
Grid(int res){
this.res = res;
this.numX = width/res;
this.numY = height/res;
tiles = new Tile[numX][numY];
for(int y = 0; y<numY; y++){
for(int x = 0; x<numX; x++){
tiles[x][y] = new Tile(x, y);
}
}
for(int y = 0; y<numY; y++){
for(int x = 0; x<numX; x++){
Tile curTile = tiles[x][y];
if(x > 0) curTile.left = tiles[x-1][y];
if(x < numX-1) curTile.right = tiles[x+1][y];
if(y > 0) curTile.up = tiles[x][y-1];
if(y < numY-1) curTile.down = tiles[x][y+1];
}
}
}
public void createPaths(){
for(int y = 0; y<numY; y++){
for(int x = 0; x<numX; x++){
if(!tiles[x][y].hasNode) continue;
tiles[x][y].buildPathRight();
tiles[x][y].buildPathUp();
}
}
}
public void clearPaths(){
for(int y = 0; y<numY; y++){
for(int x = 0; x<numX; x++){
tiles[x][y].clearNodes();
}
}
}
public Tile getTile(int x, int y){
return tiles[x][y];
}
public Tile getTileAtMouse(int mX, int mY){
int x = mX/res;
int y = mY/res;
if(x<0 || x>numX-1 || y<0 || y>numY-1) return null;
return tiles[x][y];
}
public void render(){
for(int y = 0; y<numY; y++){
for(int x = 0; x<numX; x++){
fill(255);
if(tiles[x][y].isOpen()) fill(0);
square(x*res, y*res, res);
if(tiles[x][y].hasNode){
fill(0, 255, 0);
circle(x*res+res/2, y*res+res/2, res/2);
}
}
}
}
}