Merge remote-tracking branch 'origin/models' into models

# Conflicts:
#	frontend/game.ts
This commit is contained in:
dhain 2022-12-13 10:04:44 +01:00
commit a8681c0eb4
5 changed files with 26 additions and 8 deletions

View file

@ -58,7 +58,9 @@ function checkObstacleReset(obstacle: Obstacle){
} }
} }
//
// function keyPressed() { function keyPressed() {
// if (key.toLowerCase() == "k") {
// } raspberry.boost();
}
}

View file

@ -0,0 +1,3 @@
interface Collidable {
collides(o: Entity): boolean;
}

View file

@ -30,7 +30,7 @@ abstract class Entity {
} }
//endregion //endregion
constructor(position: Position, width: number, height: number, fill: number) { protected constructor(position: Position, width: number, height: number, fill: number) {
this.position = position; this.position = position;
this.width = width; this.width = width;
this.height = height; this.height = height;

View file

@ -1,4 +1,4 @@
class Obstacle extends Entity { class Obstacle extends Entity implements Collidable{
private pipeTop: Pipe; private pipeTop: Pipe;
private pipeBottom: Pipe; private pipeBottom: Pipe;
private readonly distanceBetweenPipes: number; private readonly distanceBetweenPipes: number;
@ -50,4 +50,8 @@ class Obstacle extends Entity {
this.pipeTop.draw(); this.pipeTop.draw();
this.pipeBottom.draw(); this.pipeBottom.draw();
} }
collides(o: Entity): boolean {
return this.pipeTop.collides(o) || this.pipeBottom.collides(o);
}
} }

View file

@ -1,4 +1,4 @@
class Pipe extends Entity { class Pipe extends Entity implements Collidable {
private _image: any; private _image: any;
//region Getter & Setter //region Getter & Setter
@ -9,13 +9,15 @@ class Pipe extends Entity {
set image(path: string) { set image(path: string) {
this._image = loadImage(path); this._image = loadImage(path);
} }
//endregion //endregion
constructor(positionX: number, width: number, height: number) { constructor(positionX: number, width: number, height: number) {
super(new Position(positionX, 0), width, height, 0); super(new Position(positionX, 0), width, height, 0);
} }
public update(): void {} public update(): void {
}
public draw(): void { public draw(): void {
image(this.image, this.position.x, this.position.y, this.width, this.height); image(this.image, this.position.x, this.position.y, this.width, this.height);
@ -27,4 +29,11 @@ class Pipe extends Entity {
this.height 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
}
} }