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;
score = 0;
raspberry = new Raspberry();
raspberry.image = RASPBERRY_IMAGE_PATH;
hasAlreadyScored = false;
raspberry = new Raspberry(RASPBERRY_IMAGE_PATH);
Obstacle.distanceBetweenPipes = height / 2.5;
Obstacle.startX = width;
// Create all obstacles
obstacles = [];

View file

@ -1,11 +1,11 @@
class Obstacle extends Entity implements Collidable {
private pipeTop: Pipe;
private pipeBottom: Pipe;
private static distanceBetweenPipes: number;
private readonly padding: number = 150;
private readonly speed: number = 3;
private static startX: number;
public static distanceBetweenPipes: number;
public static startX: number;
/**
* 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.pipeTop.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 {
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.height = height - this.pipeTop.height - this.padding;
}
@ -59,9 +53,8 @@ class Obstacle extends Entity implements Collidable {
}
public update(): void {
// TODO: Put into pipe.update
this.pipeTop.position.x -= this.speed;
this.pipeBottom.position.x -= this.speed;
this.pipeTop.move(this.speed);
this.pipeBottom.move(this.speed);
this.position.x = this.pipeTop.position.x;
}

View file

@ -37,6 +37,14 @@ class Pipe extends Entity implements Collidable {
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
* @param o other entity

View file

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