Refactor & Bug Fix

`game.ts`
 - fixed score on new game

`Obstacle.ts`
 - made distanceBetweenPipes and startX public
 - moved initialization of distanceBetweenPipes and startX to game.setupGame()

`Pipe.ts`
 - created move() method to move the pipe

 `Raspberry.ts`
  - added constant variables for Raspberry size
This commit is contained in:
dhain 2023-01-10 09:37:12 +01:00
parent a16f1203ff
commit d98cd7b10e
4 changed files with 25 additions and 16 deletions

View file

@ -30,8 +30,11 @@ function setupGame() {
paused = true; paused = true;
score = 0; score = 0;
raspberry = new Raspberry(); hasAlreadyScored = false;
raspberry.image = RASPBERRY_IMAGE_PATH; raspberry = new Raspberry(RASPBERRY_IMAGE_PATH);
Obstacle.distanceBetweenPipes = height / 2.5;
Obstacle.startX = width;
// Create all obstacles // Create all obstacles
obstacles = []; obstacles = [];

View file

@ -1,11 +1,11 @@
class Obstacle extends Entity implements Collidable { class Obstacle extends Entity implements Collidable {
private pipeTop: Pipe; private pipeTop: Pipe;
private pipeBottom: Pipe; private pipeBottom: Pipe;
private static distanceBetweenPipes: number;
private readonly padding: number = 150; private readonly padding: number = 150;
private readonly speed: number = 3; private readonly speed: number = 3;
private static startX: number; public static distanceBetweenPipes: number;
public static startX: number;
/** /**
* Constructs the Obstacle with the given image * Constructs the Obstacle with the given image
@ -21,10 +21,6 @@ class Obstacle extends Entity implements Collidable {
this.pipeBottom = new Pipe(position.x, obstacleWidth, obstacleHeight); this.pipeBottom = new Pipe(position.x, obstacleWidth, obstacleHeight);
this.pipeTop.image = pipeImagePath; this.pipeTop.image = pipeImagePath;
this.pipeBottom.image = pipeImagePath; this.pipeBottom.image = pipeImagePath;
Obstacle.distanceBetweenPipes = height / 2.5;
//TODO: Put into setupGame()
Obstacle.startX = width;
} }
/** /**
@ -43,8 +39,6 @@ class Obstacle extends Entity implements Collidable {
*/ */
public randomizeHeight(): void { public randomizeHeight(): void {
this.pipeTop.height = this.randomRange(this.padding, height - this.padding - Obstacle.distanceBetweenPipes); this.pipeTop.height = this.randomRange(this.padding, height - this.padding - Obstacle.distanceBetweenPipes);
//TODO: Soi des do sei?
this.pipeTop.position.y = 0;
this.pipeBottom.position.y = this.pipeTop.height + Obstacle.distanceBetweenPipes; this.pipeBottom.position.y = this.pipeTop.height + Obstacle.distanceBetweenPipes;
this.pipeBottom.height = height - this.pipeTop.height - this.padding; this.pipeBottom.height = height - this.pipeTop.height - this.padding;
} }
@ -59,9 +53,8 @@ class Obstacle extends Entity implements Collidable {
} }
public update(): void { public update(): void {
// TODO: Put into pipe.update this.pipeTop.move(this.speed);
this.pipeTop.position.x -= this.speed; this.pipeBottom.move(this.speed);
this.pipeBottom.position.x -= this.speed;
this.position.x = this.pipeTop.position.x; this.position.x = this.pipeTop.position.x;
} }

View file

@ -37,6 +37,14 @@ class Pipe extends Entity implements Collidable {
pop(); pop();
} }
/**
* Moves the pipe to the lift with the given speed
* @param speed how fast the pipe moves
*/
public move(speed: number): void {
this.position.x -= speed;
}
/** /**
* Determines when the pipe is colliding with another entity * Determines when the pipe is colliding with another entity
* @param o other entity * @param o other entity

View file

@ -3,7 +3,12 @@ class Raspberry extends Entity {
private readonly gravity: number = 1.314159265358979323846264338; private readonly gravity: number = 1.314159265358979323846264338;
private _velocity: number = 0; private _velocity: number = 0;
private _image: any; private _image: any;
private static readonly maxVelocity: number = 100; private static readonly maxVelocity: number = 100;
private static readonly POSITION: Position = new Position(width / 6, height / 2);
private static readonly WIDTH: number = 180;
private static readonly HEIGHT: number = 70;
private static readonly FILL: number = 0;
//region Getter & Setter //region Getter & Setter
get velocity(): number { get velocity(): number {
@ -27,9 +32,9 @@ class Raspberry extends Entity {
/** /**
* Constructs the Raspberry with fixed sizes * Constructs the Raspberry with fixed sizes
*/ */
constructor() { constructor(image: string) {
// TODO: Move literals to consta super(Raspberry.POSITION, Raspberry.WIDTH, Raspberry.HEIGHT, Raspberry.FILL);
super(new Position(width / 6, height / 2), 180, 70, 0); this.image = image;
} }
public update(): void { public update(): void {