-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.js
50 lines (47 loc) · 1.65 KB
/
background.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
class Layer {
constructor(game, width, height, speedModifier, image) {
this.game = game;
this.width = width;
this.height = height;
this.speedModifier = speedModifier;
this.image = image;
this.x = 0;
this.y = 0;
}
update() {
if (this.x < -this.width) this.x = 0;
else this.x -= this.game.speed * this.speedModifier;
}
draw(context) {
context.drawImage(this.image, this.x, this.y, this.width, this.height);
context.drawImage(this.image, this.x + this.width, this.y, this.width, this.height);
}
}
export class Background {
constructor(game) {
this.game = game;
this.width = 1667;
this.height = 500;
this.layer1image = document.getElementById('layer1');
this.layer2image = document.getElementById('layer2');
this.layer3image = document.getElementById('layer3');
this.layer4image = document.getElementById('layer4');
this.layer5image = document.getElementById('layer5');
this.layer1 = new Layer(this.game, this.width, this.height, 0, this.layer1image);
this.layer2 = new Layer(this.game, this.width, this.height, 0.2, this.layer2image);
this.layer3 = new Layer(this.game, this.width, this.height, 0.4, this.layer3image);
this.layer4 = new Layer(this.game, this.width, this.height, 0.8, this.layer4image);
this.layer5 = new Layer(this.game, this.width, this.height, 1, this.layer5image);
this.backgroundLayers = [this.layer1, this.layer2, this.layer3, this.layer4, this.layer5];
}
update() {
this.backgroundLayers.forEach((layer) => {
layer.update();
});
}
draw(context) {
this.backgroundLayers.forEach((layer) => {
layer.draw(context);
});
}
}