diff --git a/frontend/game.ts b/frontend/game.ts index b3c5534..01c2ffc 100644 --- a/frontend/game.ts +++ b/frontend/game.ts @@ -58,7 +58,9 @@ function checkObstacleReset(obstacle: Obstacle){ } } -// -// function keyPressed() { -// -// } \ No newline at end of file + +function keyPressed() { + if (key.toLowerCase() == "k") { + raspberry.boost(); + } +} \ No newline at end of file diff --git a/frontend/models/Collidable.ts b/frontend/models/Collidable.ts new file mode 100644 index 0000000..066452f --- /dev/null +++ b/frontend/models/Collidable.ts @@ -0,0 +1,3 @@ +interface Collidable { + collides(o: Entity): boolean; +} \ No newline at end of file diff --git a/frontend/models/Entity.ts b/frontend/models/Entity.ts index 77f699b..1424271 100644 --- a/frontend/models/Entity.ts +++ b/frontend/models/Entity.ts @@ -30,7 +30,7 @@ abstract class Entity { } //endregion - constructor(position: Position, width: number, height: number, fill: number) { + protected constructor(position: Position, width: number, height: number, fill: number) { this.position = position; this.width = width; this.height = height; diff --git a/frontend/models/Obstacle.ts b/frontend/models/Obstacle.ts index 1a23002..1c2e6ef 100644 --- a/frontend/models/Obstacle.ts +++ b/frontend/models/Obstacle.ts @@ -1,4 +1,4 @@ -class Obstacle extends Entity { +class Obstacle extends Entity implements Collidable{ private pipeTop: Pipe; private pipeBottom: Pipe; private readonly distanceBetweenPipes: number; @@ -50,4 +50,8 @@ class Obstacle extends Entity { this.pipeTop.draw(); this.pipeBottom.draw(); } + + collides(o: Entity): boolean { + return this.pipeTop.collides(o) || this.pipeBottom.collides(o); + } } \ No newline at end of file diff --git a/frontend/models/Pipe.ts b/frontend/models/Pipe.ts index 2099e63..42a9ce1 100644 --- a/frontend/models/Pipe.ts +++ b/frontend/models/Pipe.ts @@ -1,4 +1,4 @@ -class Pipe extends Entity { +class Pipe extends Entity implements Collidable { private _image: any; //region Getter & Setter @@ -9,13 +9,15 @@ class Pipe extends Entity { set image(path: string) { this._image = loadImage(path); } + //endregion constructor(positionX: number, width: number, height: number) { super(new Position(positionX, 0), width, height, 0); } - public update(): void {} + public update(): void { + } public draw(): void { image(this.image, this.position.x, this.position.y, this.width, this.height); @@ -27,4 +29,11 @@ class Pipe extends Entity { this.height ); } + + collides(o: Entity): boolean { + return this.position.x < o.position.x + o.width && //inside left border + this.position.x + this.width > o.position.x && //but not outside right border + this.position.y < o.position.y + o.height && //inside top border + this.position.y + this.height > o.position.y; //but not outside bottom border + } } \ No newline at end of file