-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStations.pde
153 lines (128 loc) · 3.26 KB
/
Stations.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
import processing.opengl.*;
boolean mouseOver = false;
ArrayList<Station> stations;
ArrayList<Road> roads;
ArrayList<Vehicle> vehicles;
ArrayList<Asteroid> asteroids;
ArrayList<Particle> particles;
boolean drawingRoad = false;
Road currentRoad = null;
PVector lastPoint = null;
float drawDistance = 50.0f;
void setup() {
size(800, 600, P3D);
stations = new ArrayList<Station>();
roads = new ArrayList<Road>();
vehicles = new ArrayList<Vehicle>();
asteroids = new ArrayList<Asteroid>();
particles = new ArrayList<Particle>();
stations.add(new Station("X-1", 0));
stations.add(new Station("Pheonix", 1));
stations.add(new Station("Missa 12", 2));
for (int i = 0; i < 10; ++i) {
Vehicle v = new Vehicle(new PVector(random(width - 50) + 50, random(height - 50) + 50, 0));
// v.setTarget(stations.get(parseInt(random(stations.size()))));
v.findNewAsteroid();
vehicles.add(v);
}
for (int i = 0; i < 40; ++i) {
Asteroid a = new Asteroid(new PVector(random(width - 50) + 50, random(height - 50) + 50, 0));
asteroids.add(a);
}
}
void draw() {
background(20);
// lights();
// ortho();
for (Station station : stations) {
station.render();
}
for (Road road : roads) {
road.render();
}
for(int i = asteroids.size()-1; i >= 0; --i) {
Asteroid a = asteroids.get(i);
a.update();
if( a.empty ) {
asteroids.remove(a);
} else {
a.render();
}
}
for(int i = particles.size()-1; i >= 0; --i) {
Particle p = particles.get(i);
p.update();
if( p.dead ) {
particles.remove(p);
} else {
p.render();
}
}
for (Vehicle v : vehicles) {
v.update();
v.render();
}
// strokeWeight(1);
if (drawingRoad) {
stroke(255);
noFill();
beginShape(LINES);
vertex(lastPoint.x, lastPoint.y, 0);
vertex(mouseX, mouseY, 0);
endShape();
ellipse(lastPoint.x, lastPoint.y, drawDistance*2, drawDistance*2);
}
PVector nearestPoint = findNearestPoint(new PVector(mouseX, mouseY, 0));
if(nearestPoint != null) {
beginShape(LINES);
vertex(mouseX, mouseY, 0);
vertex(nearestPoint.x, nearestPoint.y, nearestPoint.z);
endShape();
}
}
PVector findNearestPoint(PVector to) {
PVector best = null;
float bestDist = MAX_FLOAT;
for(Road r : roads) {
PVector p = r.findNearestPoint(to);
float distance = dist(p.x, p.y, p.z, to.x, to.y, to.z);
if(distance < bestDist) {
bestDist = distance;
best = p;
}
}
return best;
}
void mouseOver() {
mouseOver = true;
}
void mouseOut() {
mouseOver = false;
}
void mousePressed() {
if (mouseButton == LEFT) {
drawingRoad = true;
currentRoad = new Road();
roads.add(currentRoad);
lastPoint = new PVector(mouseX, mouseY, 0);
currentRoad.addPoint(lastPoint);
} else {
vehicles.add( new Vehicle( new PVector(mouseX, mouseY, 0)));
}
}
void mouseDragged() {
if (drawingRoad) {
if (dist(lastPoint.x, lastPoint.y, mouseX, mouseY) > drawDistance) {
lastPoint = new PVector(mouseX, mouseY, 0);
currentRoad.addPoint(lastPoint);
}
}
}
void mouseReleased() {
if( mouseButton == LEFT) {
drawingRoad = false;
lastPoint = null;
currentRoad.addPoint(new PVector(mouseX, mouseY, 0));
currentRoad = null;
}
}