From ad50d14f8a4e77b86925f318203bb02712f794bc Mon Sep 17 00:00:00 2001 From: dhain Date: Tue, 13 Dec 2022 09:28:57 +0100 Subject: [PATCH] improved `Obstacle` constructor --- frontend/game.ts | 31 +++++++++++++++---------------- frontend/models/Obstacle.ts | 9 +++++---- 2 files changed, 20 insertions(+), 20 deletions(-) diff --git a/frontend/game.ts b/frontend/game.ts index 2dafa32..d405d30 100644 --- a/frontend/game.ts +++ b/frontend/game.ts @@ -12,30 +12,28 @@ function setup() { backgroundImage = loadImage(backgroundImagePath); createCanvas(1000, 1000); - obstacleOffset = width / 4; + obstacleOffset = width / 3; raspberry = new Raspberry(); raspberry.image = raspberryImagePath; obstacles.push(new Obstacle( - new Pipe(width, obstacleWidth, height), - new Pipe(width, obstacleWidth, height), - pipeImagePath + new Position(width, 0), + obstacleWidth, + height, + pipeImagePath, )); obstacles.push(new Obstacle( - new Pipe(width + obstacleOffset, obstacleWidth, height), - new Pipe(width + obstacleOffset, obstacleWidth, height), - pipeImagePath + new Position(width + obstacleOffset, 0), + obstacleWidth, + height, + pipeImagePath, )); obstacles.push(new Obstacle( - new Pipe(width + obstacleOffset * 2, obstacleWidth, height), - new Pipe(width + obstacleOffset * 2, obstacleWidth, height), - pipeImagePath - )); - obstacles.push(new Obstacle( - new Pipe(width + obstacleOffset * 3, obstacleWidth, height), - new Pipe(width + obstacleOffset * 3, obstacleWidth, height), - pipeImagePath + new Position(width + obstacleOffset * 2, 0), + obstacleWidth, + height, + pipeImagePath, )); obstacles.forEach((obstacle) => obstacle.resetPosition(false)); @@ -50,7 +48,8 @@ function draw() { obstacle.draw(); obstacle.update(); - if(obstacle.position.x < -obstacleWidth) { + // console.log(obstacle.position.x); + if(obstacle.position.x <= -obstacleWidth) { obstacle.resetPosition(true); } }); diff --git a/frontend/models/Obstacle.ts b/frontend/models/Obstacle.ts index b75be4a..ebdcbf0 100644 --- a/frontend/models/Obstacle.ts +++ b/frontend/models/Obstacle.ts @@ -11,10 +11,10 @@ class Obstacle extends Entity { * Constructs the Obstacle using the top and bottom Pipe * (fill is not used here) */ - constructor(pipeTop: Pipe, pipeBottom: Pipe, pipeImagePath: string) { - super(pipeTop.position, pipeTop.width, pipeBottom.height, 0); - this.pipeTop = pipeTop; - this.pipeBottom = pipeBottom; + constructor(position: Position, obstacleWidth: number, obstacleHeight: number, pipeImagePath: string) { + super(position, obstacleWidth, obstacleHeight, 0); + this.pipeTop = new Pipe(position.x, obstacleWidth, obstacleHeight); + this.pipeBottom = new Pipe(position.x, obstacleWidth, obstacleHeight); this.pipeTop.image = pipeImagePath; this.pipeBottom.image = pipeImagePath; @@ -42,6 +42,7 @@ class Obstacle extends Entity { public update(): void { this.pipeTop.position.x -= this.speed; this.pipeBottom.position.x -= this.speed; + this.position.x = this.pipeTop.position.x; } public draw(): void {