-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbackground.js
More file actions
84 lines (76 loc) · 2.34 KB
/
background.js
File metadata and controls
84 lines (76 loc) · 2.34 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
class ParallaxLayer {
constructor(img, speed, yOffset) {
this.x = 0;
this.y = 0;
this.speed = speed;
this.yOffset = yOffset;
this.game = gameEngine;
this.images = [];
for (const image of img) {
this.images.push(ASSET_MANAGER.getAsset(image));
}
}
update() {
this.x = this.speed * this.game.camera.x;
}
draw(ctx) {
//Starting from top background image drawn right then down one layer at a time
// j for vertical position
// i for horizontal position
for (var j = 0; j < this.images.length; j++) {
let image = this.images[j];
for (var i = 0; i < 9; i++) {
ctx.drawImage(
image,
this.x + i * image.width - this.game.camera.x,
this.y + this.yOffset - this.game.camera.y,
image.width, image.height
);
}
}
}
}
class Background extends ParallaxLayer {
constructor(game, img) {
super(img, 0.8, 0);
}
draw(ctx) { // Backgrounds tile differently, so it needs it's own draw()
//Starting from top background image drawn right then down one layer at a time
// j for vertical position
// i for horizontal position
for (var j = 0; j < this.images.length; j++) {
let image = this.images[j];
for (var i = 0; i < 7; i++) {
ctx.drawImage(
image,
this.x + (i * 1920) - this.game.camera.x,
this.y + (768 * j) - this.game.camera.y,
1920, 768
);
}
}
}
}
class Foreground extends ParallaxLayer {
constructor(game, img) {
super(img, 0, 1060);
}
draw(ctx) {
for (var j = 0; j < this.images.length; j++) {
let image = this.images[j];
for (var i = 0; i < 5; i++) {
ctx.drawImage(
image,
this.x + i * image.width - this.game.camera.x,
this.y + this.yOffset - this.game.camera.y,
image.width, image.height
);
}
}
}
}
class Pillars extends ParallaxLayer {
constructor(game, img) {
super(img, 0.6, 655);
}
}