-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPathingMain.java
More file actions
executable file
·247 lines (207 loc) · 6.12 KB
/
PathingMain.java
File metadata and controls
executable file
·247 lines (207 loc) · 6.12 KB
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
import java.util.List;
import java.util.ArrayList;
import java.util.LinkedList;
import processing.core.*;
public class PathingMain extends PApplet
{
private List<PImage> imgs;
private int current_image;
private long next_time;
private PImage background;
private PImage obstacle;
private PImage goal;
private List<Point> path;
private static final int TILE_SIZE = 32;
private static final int ANIMATION_TIME = 100;
private GridValues[][] grid;
private static final int ROWS = 15;
private static final int COLS = 20;
private static enum GridValues { BACKGROUND, OBSTACLE, GOAL, SEARCHED };
private Point wPos;
private boolean drawPath = false;
public void settings() {
size(640,480);
}
/* runs once to set up world */
public void setup()
{
path = new LinkedList<>();
wPos = new Point(2, 2);
imgs = new ArrayList<>();
imgs.add(loadImage("images/wyvern1.bmp"));
imgs.add(loadImage("images/wyvern2.bmp"));
imgs.add(loadImage("images/wyvern3.bmp"));
background = loadImage("images/grass.bmp");
obstacle = loadImage("images/vein.bmp");
goal = loadImage("images/water.bmp");
grid = new GridValues[ROWS][COLS];
initialize_grid(grid);
current_image = 0;
next_time = System.currentTimeMillis() + ANIMATION_TIME;
noLoop();
draw();
}
/* set up a 2D grid to represent the world */
private static void initialize_grid(GridValues[][] grid)
{
for (int row = 0; row < grid.length; row++)
{
for (int col = 0; col < grid[row].length; col++)
{
grid[row][col] = GridValues.BACKGROUND;
}
}
//set up some obstacles
for (int row = 2; row < 8; row++)
{
grid[row][row + 5] = GridValues.OBSTACLE;
}
for (int row = 8; row < 12; row++)
{
grid[row][19 - row] = GridValues.OBSTACLE;
}
for (int col = 1; col < 8; col++)
{
grid[11][col] = GridValues.OBSTACLE;
}
grid[13][14] = GridValues.GOAL;
}
private void next_image()
{
current_image = (current_image + 1) % imgs.size();
}
/* runs over and over */
public void draw()
{
// A simplified action scheduling handler
long time = System.currentTimeMillis();
if (time >= next_time)
{
next_image();
next_time = time + ANIMATION_TIME;
}
draw_grid();
draw_path();
image(imgs.get(current_image), wPos.x * TILE_SIZE, wPos.y * TILE_SIZE);
System.out.println(current_image);
}
private void draw_grid()
{
for (int row = 0; row < grid.length; row++)
{
for (int col = 0; col < grid[row].length; col++)
{
draw_tile(row, col);
}
}
}
private void draw_path()
{
if (drawPath)
{
for (Point p : path)
{
fill(128, 0, 0);
rect(p.x * TILE_SIZE + TILE_SIZE * 3 / 8,
p.y * TILE_SIZE + TILE_SIZE * 3 / 8,
TILE_SIZE / 4, TILE_SIZE / 4);
}
}
}
private void draw_tile(int row, int col)
{
switch (grid[row][col])
{
case BACKGROUND:
image(background, col * TILE_SIZE, row * TILE_SIZE);
break;
case OBSTACLE:
image(obstacle, col * TILE_SIZE, row * TILE_SIZE);
break;
case SEARCHED:
fill(0, 128);
rect(col * TILE_SIZE + TILE_SIZE / 4,
row * TILE_SIZE + TILE_SIZE / 4,
TILE_SIZE / 2, TILE_SIZE / 2);
break;
case GOAL:
image(goal, col * TILE_SIZE, row * TILE_SIZE);
break;
}
}
public static void main(String args[])
{
PApplet.main("PathingMain");
}
public void keyPressed()
{
if (key == ' ')
{
//clear out prior path
path.clear();
//example - replace with dfs
DepthFirstSearch(wPos, grid, path);
}
else if (key == 'p')
{
drawPath ^= true;
redraw();
}
}
/* replace the below with a depth first search
this code provided only as an example of moving in
in one direction for one tile - it mostly is for illustrating
how you might test the occupancy grid and add nodes to path!
*/
private boolean moveOnce(Point pos, GridValues[][] grid, List<Point> path)
{
try {
Thread.sleep(200);
} catch (Exception e) {}
redraw();
Point rightN = new Point(pos.x +1, pos.y );
//test if this is a valid grid cell
if (withinBounds(rightN, grid) &&
grid[rightN.y][rightN.x] != GridValues.OBSTACLE && //
grid[rightN.y][rightN.x] != GridValues.SEARCHED)
{
//check if my right neighbor is the goal
if (grid[rightN.y][rightN.x] == GridValues.GOAL) {
path.add(0, rightN);
return true;
//set this value as
}
//set this value as searched
grid[rightN.y][rightN.x] = GridValues.SEARCHED;
}
return false;
}
private boolean DepthFirstSearch(Point pos, GridValues[][] grid, List<Point> path){
if (withinBounds(pos, grid) &&
grid[pos.y][pos.x] != GridValues.OBSTACLE &&
grid[pos.y][pos.x] != GridValues.SEARCHED)
{
//add current point to path!
if (grid[pos.y][pos.x] == GridValues.GOAL) {
path.add(0, pos);
return true;
}else{
grid[pos.y][pos.x] = GridValues.SEARCHED;
Point rightN = new Point(pos.x + 1, pos.y );
Point downN = new Point(pos.x, pos.y + 1);
Point leftN = new Point(pos.x - 1, pos.y );
Point upN = new Point(pos.x, pos.y - 1 );
if (DepthFirstSearch(downN, grid, path) || DepthFirstSearch(leftN, grid, path) || DepthFirstSearch(rightN, grid, path) || DepthFirstSearch(upN, grid, path)) {
path.add(pos);
return true;
}
}
}
return false;
}
private static boolean withinBounds(Point p, GridValues[][] grid)
{
return p.y >= 0 && p.y < grid.length &&
p.x >= 0 && p.x < grid[0].length;
}
}