-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworldManager.js
More file actions
128 lines (100 loc) · 3.06 KB
/
worldManager.js
File metadata and controls
128 lines (100 loc) · 3.06 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
class WorldManager {
constructor() {
// scaling variables
this.sfx = 20;
this.sfy = -20;
this.osx = 21;
this.osy = -12;
this.lastAverage = [0, 0]
// options
this.drawGrid = true;
this.drawCoordinates = false;
mouseListeners.push(this);
}
tX(x_worldspace) {
return (x_worldspace + this.osx) * this.sfx;
}
tY(y_worldspace) {
return (y_worldspace + this.osy) * this.sfy;
}
rX(x_screenspace) {
return x_screenspace / this.sfx - this.osx;
}
rY(y_screenspace) {
return y_screenspace / this.sfy - this.osy;
}
scaleW2S(sceenspace){
return sceenspace * this.sfx;
}
scaleS2W(sceenspace){
if(this.sfx == 0)
return 0;
return sceenspace / this.sfx;
}
update() {
if (mouseIsPressed) {
this.osx += (mouseX - pmouseX) / this.sfx;
this.osy += (mouseY - pmouseY) / this.sfy;
}
if(autonomousCars.length > 0){
let average = [0, 0];
for(let c of autonomousCars){
average[0] += c.car.pos.x;
average[1] += c.car.pos.y;
}
average[0] /= autonomousCars.length;
average[1] /= autonomousCars.length;
this.osx -= (average[0] - this.lastAverage[0]);
this.osy -= (average[1] - this.lastAverage[1]);
this.lastAverage = average;
}
}
draw() {
if (!this.drawGrid) return;
const gridStartX = floor(this.rX(0));
const gridStartY = floor(this.rY(ui.graphSplit));
const gridEndX = floor(this.rX(width) + 1);
const gridEndY = floor(this.rY(0) + 1);
strokeWeight(1);
if (this.drawCoordinates) {
textSize(12);
stroke(100);
text("(0, 0)", this.tX(0.2), this.tY(0.2));
}
for (let x = gridStartX + 1; x < gridEndX; x++) {
if (x === 0) {
stroke(0, 250, 0);
} else if (x % 10 === 0) {
stroke(100);
if (this.drawCoordinates) text(`(${x}, 0)`, this.tX(x), this.tY(0.2));
} else {
stroke(220);
}
line(this.tX(x), this.tY(gridStartY), this.tX(x), this.tY(gridEndY));
}
for (let y = gridStartY + 1; y < gridEndY; y++) {
if (y === 0) {
stroke(250, 0, 0);
} else if (y % 10 === 0) {
stroke(100);
if (this.drawCoordinates) text(`(0, ${y})`, this.tX(0.2), this.tY(y));
} else {
stroke(220);
}
line(this.tX(gridStartX), this.tY(y), this.tX(gridEndX), this.tY(y));
}
}
mousePressed() {}
mouseReleased() {}
mouseWheel(event) {
if(mouseY > ui.graphSplit || (mouseX < ui.controlPanelSplit && ui.controlPanel.show))
return;
const zoomSpeed = 0.0005;
const startx = this.rX(mouseX);
const starty = this.rY(mouseY);
this.sfy += event.delta * this.sfx * zoomSpeed;
this.sfx -= event.delta * this.sfx * zoomSpeed;
this.osx -= startx - this.rX(mouseX);
this.osy -= starty - this.rY(mouseY);
}
}