-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCars.js
39 lines (32 loc) · 1022 Bytes
/
Cars.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
import Car from "./Car.js";
export default class Cars {
constructor(xPosition) {
this.cars = [];
this.start(xPosition);
}
start(xPosition) {
// random smer, random speed, random razlika med avti
let directions = [-1, 1];
this.direction = directions[Math.floor(Math.random() * 2)];
this.speed = Math.floor(Math.random() * 7 + 4);
this.waitTime = Math.floor(Math.random() * 2800 + 1400);
// takoj enga das v sceno
let car = new Car(this.direction, this.speed, xPosition);
this.cars.push(car);
// nato pa se ponavlja na waitTime
this.interval = setInterval(() => {
let car = new Car(this.direction, this.speed, xPosition);
this.cars.push(car);
this.deleteCars();
// console.log(this.cars);
}, this.waitTime);
}
deleteCars() {
// pogleda ce je avto izven zaslona
this.cars = this.cars.filter(car => car.yPosition < 15 && car.yPosition > -15);
}
deleteRow() {
clearInterval(this.interval);
this.cars = [];
}
}