-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
269 lines (234 loc) · 7.06 KB
/
index.js
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
// js setup
TrackAPI.index();
Segment.buildSegmentCanvasses();
renderSidePanel("home");
welcomeText();
addJsEventListeners();
// konami feature
const pressed = [];
function checkArrayEquality(a, b) {
if (a.length !== b.length) return false;
for (let i = 0; i < a.length; i++) {
if (a[i] != b[i]) return false;
}
return true;
}
// side panel helper
function renderSidePanel(panelName) {
const homePanel = document.getElementById("home");
const viewingPanel = document.getElementById("viewing");
const creatingPanel = document.getElementById("creating");
const panels = [homePanel, creatingPanel, viewingPanel]
for (const panel of panels) {
if (panel.id === panelName ) {
panel.style.display = "";
} else {
panel.style.display = "none";
}
}
}
function welcomeText() {
ctx.font = '48px serif';
ctx.fillText('Welcome to', 160, 225);
ctx.fillText('Track Builder', 140, 275);
}
// driving set up
let accellerating = false;
let rightSteering = 0;
let leftSteering = 0;
let speed = 0;
let timeBefore;
let time;
let animation;
// could be substituded for an ip address fetch
let ip = Math.random();
let colours = [
"red",
"blue",
"yellow",
"orange",
"purple"
]
let myCar = {
position: [255, 269],
angle: 0,
colour: colours[Math.floor(Math.random() * colours.length)],
ip: ip,
active: true
}
let cars = {};
cars[ip] = myCar;
setInterval(() => {
cars = {};
cars[ip] = myCar;
}, 15000);
let ready;
const drawCars = () => {
if (creating || !currentTrack) return
// canvas set up
var ctx = canvas.getContext('2d');
ctx.lineWidth = 2;
ctx.strokeStyle = "black";
clearCanvas();
currentTrack.drawTrack();
ctx.lineWidth = 8;
// interval set up for smooth animation
timeBefore = time;
time = new Date;
let interval = time - timeBefore;
if (isNaN(interval)) interval = 15;
// updating this car location based on user input
if (accellerating) {
speed += 60*interval/10000;
if (speed > 25/interval) speed = 25/interval;
} else {
speed -= 60*interval/10000;
if (speed < 0) speed = 0;
}
myCar.position[0] += speed*Math.cos(myCar.angle);
myCar.position[1] += speed*Math.sin(myCar.angle);
if (myCar.position[0] < 5) myCar.position[0] = 5;
if (myCar.position[1] < 5) myCar.position[1] = 5;
if (myCar.position[0] > 535) myCar.position[0] = 535;
if (myCar.position[1] > 535) myCar.position[1] = 535;
myCar.angle += (2.5*(rightSteering - leftSteering)*Math.PI/10)/interval;
// drawing all cars to the canvas
for (const carId in cars) {
if (cars[carId].active === true) {
let position = cars[carId].position;
let angle = cars[carId].angle;
let colour = cars[carId].colour;
ctx.strokeStyle = colour;
ctx.beginPath();
ctx.moveTo(position[0]-5*Math.cos(angle), position[1]-5*Math.sin(angle));
ctx.lineTo(position[0]+5*Math.cos(angle), position[1]+5*Math.sin(angle));
ctx.stroke();
}
}
// resetting the canvas
ctx.lineWidth = 2;
ctx.strokeStyle = "black";
// sending to the server the users car location
if (ready && channel === currentTrack.id) updateCarLocation();
// requesting next animation and storing key to stop animation
animation = (window.requestAnimationFrame(drawCars));
}
// socket set up
const socket = new WebSocket(webSocket);
socket.onmessage = function(e) {
const data = JSON.parse(e.data);
if (data.type === 'ping' || creating || !data.message) return
const carData = data.message.content;
if (ip !== carData.ip && ready) cars[carData.ip] = carData;
if (carData.active === false) {
setTimeout(() => {
cars = {};
cars[ip] = myCar;
}, 30);
cars = {};
cars[ip] = myCar;
}
}
socket.onclose = () => {
removeCar(channel);
unsubscribeFrom(channel);
}
let channel;
let readyTimeout;
function requestSubscribe() {
ready = false;
if (readyTimeout) clearTimeout(readyTimeout);
readyTimeout = setTimeout(()=>ready=true, 100);
if (channel) {
removeCar(channel);
unsubscribeFrom(channel);
}
const message = {
command: "subscribe",
identifier: JSON.stringify({channel: "TrackChannel", id: currentTrack.id})
};
socket.send(JSON.stringify(message));
channel = currentTrack.id;
cars = {};
cars[ip] = myCar;
}
function unsubscribeFrom(channelNumber) {
const message = {
command: "unsubscribe",
identifier: JSON.stringify({channel: "TrackChannel", id: channelNumber})
};
socket.send(JSON.stringify(message));
}
function removeCar(channelNumber) {
const removeCar = {...myCar};
removeCar.active = false;
let message = {
command: "message",
identifier: JSON.stringify({channel: "TrackChannel", id: channelNumber}),
data: JSON.stringify(removeCar)
};
socket.send(JSON.stringify(message));
}
function updateCarLocation() {
const message = {
command: "message",
identifier: JSON.stringify({channel: "TrackChannel", id: currentTrack.id}),
data: JSON.stringify(myCar)
};
socket.send(JSON.stringify(message));
}
// Event listeners
function addJsEventListeners() {
const home = document.getElementById("home-link");
home.addEventListener('click',e=> {
creating = false;
currentTrack = null;
renderSidePanel('home');
clearCanvas();
welcomeText();
})
window.addEventListener('keyup', (e) => {
const konami = ["ArrowUp", "ArrowUp", "ArrowDown", "ArrowDown", "ArrowLeft", "ArrowRight", "ArrowLeft", "ArrowRight", "b", "a"];
pressed.push(e.key);
pressed.splice(-konami.length - 1, pressed.length - konami.length);
if (checkArrayEquality(pressed, konami)) {
const editTrackButton = document.getElementById('edit-track');
const deleteTrackButton = document.getElementById('delete-track');
editTrackButton.style.display = "";
deleteTrackButton.style.display = "";
}
switch(e.key) {
case "ArrowUp":
accellerating = false;
break;
case "ArrowLeft":
leftSteering = 0;
break;
case "ArrowRight":
rightSteering = 0;
break;
default:
return
}
});
window.addEventListener('keydown', e => {
switch(e.key) {
case "ArrowUp":
accellerating = true;
break;
case "ArrowLeft":
leftSteering = 1;
break;
case "ArrowRight":
rightSteering = 1;
break;
default:
return
}
});
window.addEventListener('beforeunload', (event) => {
event.preventDefault();
removeCar(channel);
unsubscribeFrom(channel);
});
}