Added background

This commit is contained in:
Thomas Forgione 2018-08-21 16:58:30 +02:00
parent 07251677cd
commit c398ba3087
No known key found for this signature in database
GPG Key ID: 203DAEA747F48F41
2 changed files with 16 additions and 6 deletions

View File

@ -36,9 +36,10 @@ class Scene extends Screen {
} }
drawBackground() { drawBackground() {
this.context.fillStyle = 'rgb(10, 0, 0)'; let pattern = this.context.createPattern(Scene.background, 'repeat');
this.context.fillStyle = pattern;
this.context.beginPath(); this.context.beginPath();
this.context.rect(0, 0, this.width(), this.height()); this.context.rect(0, - this.cameraHeight * this.height(), this.width(), this.height());
this.context.fill(); this.context.fill();
} }
@ -106,8 +107,8 @@ class Scene extends Screen {
} }
// Collisions with the border of the screen // Collisions with the border of the screen
this.player.x = Math.max(this.player.x, this.player.size / 2); this.player.x = Math.max(this.player.x, this.player.size / 4);
this.player.x = Math.min(this.player.x, 1 - this.player.size / 2); this.player.x = Math.min(this.player.x, 1 - this.player.size / 4);
} }
addPlatform(object) { addPlatform(object) {
@ -116,6 +117,9 @@ class Scene extends Screen {
render() { render() {
this.clear(); this.clear();
this.context.translate(0, this.cameraHeight * this.height());
this.drawBackground(); this.drawBackground();
for (let platform of this.platforms) { for (let platform of this.platforms) {
@ -124,6 +128,8 @@ class Scene extends Screen {
this.drawObject(this.player); this.drawObject(this.player);
this.context.resetTransform();
this.drawHud(); this.drawHud();
} }
@ -134,7 +140,7 @@ class Scene extends Screen {
Box.texture, Box.texture,
0, 64 * this.player.frameNumber, 64, 64, 0, 64 * this.player.frameNumber, 64, 64,
(object.x - object.size / 2) * this.width(), (object.x - object.size / 2) * this.width(),
(1 - object.y - object.size / 2 + this.cameraHeight) * this.height(), (1 - object.y - object.size / 2) * this.height(),
size, size size, size
); );
} }
@ -147,7 +153,7 @@ class Scene extends Screen {
Platform.texture, Platform.texture,
0, 0, 64, 64, 0, 0, 64, 64,
(object.x - object.width / 2) * this.width(), (object.x - object.width / 2) * this.width(),
(1 - object.y - object.height / 2 + this.cameraHeight) * this.height(), (1 - object.y - object.height / 2) * this.height(),
width, width, width, width,
); );
} }
@ -170,3 +176,6 @@ class Scene extends Screen {
} }
} }
Scene.background = new Image();
Scene.background.src = "img/background.png";

View File

@ -47,6 +47,7 @@ class Screen {
} }
clear() { clear() {
this.context.resetTransform();
this.context.clearRect(0, 0, this.width(), this.height()); this.context.clearRect(0, 0, this.width(), this.height());
} }