-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
90 lines (89 loc) · 1.78 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
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
class Layer {
constructor(game, width, height, speedModifier, image, x, y) {
this.game = game;
this.width = width;
this.height = height;
this.speedModifier = speedModifier;
this.image = image;
this.x = x || 0;
this.y = 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 = layer1;
this.layer1 = new Layer(
this.game,
this.width,
this.height,
0,
this.layer1Image
);
this.layer2Image = layer2;
this.layer2 = new Layer(
this.game,
this.width,
this.height,
0.2,
this.layer2Image
);
this.layer3Image = layer3;
this.layer3 = new Layer(
this.game,
this.width,
this.height,
0.4,
this.layer3Image
);
this.layer4Image = layer4;
this.layer4 = new Layer(
this.game,
this.width,
this.height,
0.8,
this.layer4Image
);
this.layer5Image = layer5;
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);
});
}
}