-
Notifications
You must be signed in to change notification settings - Fork 0
/
Map.pde
295 lines (264 loc) · 8.39 KB
/
Map.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
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
/// You do not have to change this file, but you can, if you want to add a more sophisticated generator.
class Wall
{
PVector start;
PVector end;
PVector normal;
PVector direction;
float len;
int ID; //ID will be used to distinguish split walls from the rest of the walls
Wall(PVector start, PVector end)
{
this.start = start;
this.end = end;
direction = PVector.sub(this.end, this.start);
len = direction.mag();
direction.normalize();
normal = new PVector(-direction.y, direction.x);
this.ID = 0;
}
void setID(int ID){
this.ID = ID;
}
int getID(){
return ID;
}
boolean crosses(PVector from, PVector to)
{
// Vector pointing from `this.start` to `from`
PVector d1 = PVector.sub(from, this.start);
// Vector pointing from `this.start` to `to`
PVector d2 = PVector.sub(to, this.start);
// If both vectors are on the same side of the wall
// their dot products with the normal will have the same sign
// If they are both positive, or both negative, their product will
// be positive.
float dist1 = normal.dot(d1);
float dist2 = normal.dot(d2);
if (dist1 * dist2 > 0) return false;
// if the start and end are on different sides, we need to determine
// how far the intersection point is along the wall
// first we determine how far the projections of from and to are
// along the wall
float ldist1 = direction.dot(d1);
float ldist2 = direction.dot(d2);
// the distance of the intersection point from the start
// is proportional to the normal distance of `from` in
// along the total movement
float t = dist1/(dist1 - dist2);
// calculate the intersection as this proportion
float intersection = ldist1 + t*(ldist2 - ldist1);
if (intersection < 0 || intersection > len) return false;
return true;
}
// Return the mid-point of this wall
PVector center()
{
return PVector.mult(PVector.add(start, end), 0.5);
}
void draw()
{
strokeWeight(3);
line(start.x, start.y, end.x, end.y);
if (SHOW_WALL_DIRECTION)
{
PVector marker = PVector.add(PVector.mult(start, 0.2), PVector.mult(end, 0.8));
circle(marker.x, marker.y, 5);
}
}
}
void AddPolygon(ArrayList<Wall> walls, PVector[] nodes)
{
for (int i = 0; i < nodes.length; ++ i)
{
int next = (i+1)%nodes.length;
walls.add(new Wall(nodes[i], nodes[next]));
}
}
void AddPolygonScaled(ArrayList<Wall> walls, PVector[] nodes)
{
for (int i = 0; i < nodes.length; ++ i)
{
int next = (i+1)%nodes.length;
walls.add(new Wall(new PVector(nodes[i].x*width, nodes[i].y*height), new PVector(nodes[next].x*width, nodes[next].y*height)));
}
}
class Obstacle
{
ArrayList<Wall> walls;
Obstacle()
{
walls = new ArrayList<Wall>();
PVector origin = new PVector(width*0.1 + random(width*0.65), height*0.12 + random(height*0.65));
if (origin.x < 100 && origin.y > 500) origin.add(new PVector(150,0));
PVector[] nodes = new PVector[] {};
float angle = random(100)/100.0;
for (int i = 0; i < 3 + random(2) && angle < TAU; ++i)
{
float distance = height*0.05 + random(height*0.15);
nodes = (PVector[])append(nodes, PVector.add(origin, new PVector(cos(-angle)*distance, sin(-angle)*distance)));
angle += 1 + random(25)/50;
}
AddPolygon(walls, nodes);
}
}
// Given a (closed!) polygon surrounded by walls, tests if the
// given point is inside that polygon.
// Note that this only works for polygons that are inside the
// visible screen (or not too far outside)
boolean isPointInPolygon(PVector point, ArrayList<Wall> walls)
{
// we create a test point "far away" horizontally
PVector testpoint = PVector.add(point, new PVector(width*2, 0));
// Then we count how often the line from the given point
// to our test point intersects the polygon outline
int count = 0;
for (Wall w: walls)
{
if (w.crosses(point, testpoint))
count += 1;
}
// If we cross an odd number of times, we started inside
// otherwise we started outside the polygon
// Intersections alternate between enter and exit,
// so if we "know" that the testpoint is outside
// and odd number means we exited one more time
// than we entered.
return (count%2) == 1;
}
class Map
{
ArrayList<Wall> walls;
ArrayList<Obstacle> obstacles;
ArrayList<Wall> outline;
ArrayList<PVector> pts;
Map()
{
walls = new ArrayList<Wall>();
outline = new ArrayList<Wall>();
obstacles = new ArrayList<Obstacle>();
}
boolean collides(PVector from, PVector to)
{
for (Wall w : walls)
{
if (w.crosses(from, to)) return true;
}
return false;
}
void doSplit(boolean xdir, float from, float to, float other, float otherend, ArrayList<PVector> points, int level)
{
float range = abs(to-from);
float sign = -1;
if (to > from) sign = 1;
if (range < 70) return;
if (level > 1 && random(0,1) < 0.05*level) return;
float split = from + sign*random(range*0.35, range*0.45);
float splitend = split + sign*random(20, range*0.35-10);
if (xdir)
points.add(new PVector(split, other));
else
points.add(new PVector(other, split));
float othersign = 1;
if (otherend < other) othersign = -1;
float otherrange = abs(other-otherend);
float spikeend = other + othersign*random(otherrange*0.4, otherrange*(0.9 - 0.1*level));
doSplit(!xdir, other, spikeend, split, from, points, level+1);
if (xdir)
{
points.add(new PVector(split, spikeend));
points.add(new PVector(splitend, spikeend));
}
else
{
points.add(new PVector(spikeend, split));
points.add(new PVector(spikeend, splitend));
}
doSplit(!xdir, spikeend, other, splitend, to, points, level + 1);
if (xdir)
points.add(new PVector(splitend, other));
else
points.add(new PVector(other, splitend));
}
void randomMap()
{
ArrayList<PVector> points = new ArrayList<PVector>();
points.add(new PVector(0, height));
doSplit(true, 50, width, height, 0, points, 0);
points.add(new PVector(width, height));
points.add(new PVector(width, 0));
points.add(new PVector(200, 0));
points.add(new PVector(200, 80));
points.add(new PVector(0, 80));
//pts = points;
AddPolygon(outline, points.toArray(new PVector[]{}));
}
void generate(int which)
{
outline.clear();
obstacles.clear();
walls.clear();
if (which < 0)
{
randomMap();
}
else if (which == 0)
{
AddPolygon(outline, new PVector[] {new PVector(-100, height+100), new PVector(width+100, height+100), new PVector(width+100, -100), new PVector(-100,-100)});
}
else
{
AddPolygonScaled(outline, customMap(which));
}
walls.addAll(outline);
for (int i = 0; i < random(MAX_OBSTACLES); ++i)
{
Obstacle obst = new Obstacle();
boolean ok = true;
// only obstacle if it doesn't intersect with any existing one (or the exterior)
for (Wall w : obst.walls)
{
if (collides(w.start, w.end)) ok = false;
if (!isReachable(w.start)) ok = false;
}
if (ok)
{
obstacles.add(obst);
walls.addAll(obst.walls);
}
}
}
void update(float dt)
{
draw();
}
void draw()
{
stroke(255);
strokeWeight(3);
for (Wall w : walls)
{
w.draw();
}
if (pts != null)
{
PVector current = new PVector(width/2, height/2);
for (PVector p : pts)
{
fill(255,0,0);
circle(p.x, p.y, 4);
line(current.x, current.y, p.x, p.y);
current = p;
}
}
}
boolean isReachable(PVector point)
{
if (!isPointInPolygon(point, outline)) return false;
for (Obstacle o: obstacles)
{
if (isPointInPolygon(point, o.walls)) return false;
}
return true;
}
}