-
Notifications
You must be signed in to change notification settings - Fork 0
/
Road.pde
152 lines (148 loc) · 3.62 KB
/
Road.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
class Road {
int x, y, drawX, drawY, distance, direction, progress, state, buildDiffX, buildDiffY;
Road(int x, int y, int direction, int distance) {
this.x = x;
this.y = y;
this.drawX = x;
this.drawY = y;
this.direction = direction;
this.distance = distance;
this.state = 0;
this.progress = 0;
this.buildDiffX = 0;
this.buildDiffY = 0;
switch(direction) {
case 0: //UP
this.buildDiffX = roadWidth/2+1;
break;
case 1: //RIGHT
this.buildDiffY = roadWidth/2+1;
break;
case 2: //DOWN
this.buildDiffX = -(roadWidth/2+1);
break;
case 3: //LEFT
this.buildDiffY = -(roadWidth/2+1);
break;
}
}
void update() {
switch(state) {
case 0:
//--- GROW if distance not yet completed
if (progress < distance) {
advance(1);
progress++;
// Are we still on stage?
if (drawX <= 0 || drawX >= width || drawY <= 0 || drawY >= height) {
restart();
} else {
//Is the pixel we want to draw in blank?
if (pixels[drawY*width+drawX] != color(0)) restart();
}
} else {
state++;
}
break;
case 1:
//--- SPAWN
break;
case 2:
//--- BUILD
if (progress < distance) {
advance(roadWidth/2);
progress++;
// Are we still on stage?
if (drawX <= 0 || drawX >= width || drawY <= 0 || drawY >= height) {
restart();
}
} else {
state++;
}
break;
}
}
void display() {
switch(state) {
case 0:
//--- GROW
strokeWeight(1);
stroke(255);
point(drawX, drawY);
break;
case 1:
//--- SPAWN
spawnNewRoads();
// state++;
// drawX = x;
// drawY = y;
// progress = 0;
break;
case 2:
//--- BUILD
//noStroke();
//fill(255,0,0);
strokeWeight(roadWidth);
stroke(255,100);
boolean cantDraw = false;
if ((drawX+buildDiffX) <= 0 || (drawX+buildDiffX) >= width || (drawY+buildDiffY) <= 0 || (drawY+buildDiffY) >= height) {
restart();
} else {
if (pixels[(drawY+buildDiffY)*width+(drawX+buildDiffX)] == color(0)) {
point(drawX+buildDiffX, drawY+buildDiffY);
} else {
cantDraw = true;
}
}
if ((drawX-buildDiffX) <= 0 || (drawX-buildDiffX) >= width || (drawY-buildDiffY) <= 0 || (drawY-buildDiffY) >= height) {
restart();
} else {
if (pixels[(drawY-buildDiffY)*width+(drawX-buildDiffX)] == color(0)) {
point(drawX-buildDiffX, drawY-buildDiffY);
}
else {
cantDraw = true;
}
}
if (cantDraw) state++;
break;
}
}
void spawnNewRoads() {
int oppDirection = direction - 2;
if (oppDirection < 0) oppDirection += 4;
println("== Spawn new roads at" + drawX + "," + drawY + " everyway but " + oppDirection);
for (int i=0; i<4; i++) {
if (i != oppDirection) {
r = new Road(drawX, drawY, i, int(random(minLength, maxLength)));
roads.add(r);
}
}
state++;
drawX = x;
drawY = y;
progress = 0;
}
void advance(int amount) {
switch(direction) {
case 0: //UP
drawY-=amount;
break;
case 1: //RIGHT
drawX+=amount;
break;
case 2: //DOWN
drawY+=amount;
break;
case 3: //LEFT
drawX-=amount;
break;
}
}
void restart() {
state+=2;
drawX = x;
drawY = y;
progress = 0;
}
}