From d98cd7b10eee77081d46b1a575061b9125f9bf0a Mon Sep 17 00:00:00 2001 From: dhain Date: Tue, 10 Jan 2023 09:37:12 +0100 Subject: [PATCH] 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 --- frontend/game.ts | 7 +++++-- frontend/model/Obstacle.ts | 15 ++++----------- frontend/model/Pipe.ts | 8 ++++++++ frontend/model/Raspberry.ts | 11 ++++++++--- 4 files changed, 25 insertions(+), 16 deletions(-) diff --git a/frontend/game.ts b/frontend/game.ts index ac58eab..007d18e 100644 --- a/frontend/game.ts +++ b/frontend/game.ts @@ -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 = []; diff --git a/frontend/model/Obstacle.ts b/frontend/model/Obstacle.ts index bdf923c..c920ddd 100644 --- a/frontend/model/Obstacle.ts +++ b/frontend/model/Obstacle.ts @@ -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; } diff --git a/frontend/model/Pipe.ts b/frontend/model/Pipe.ts index 26e402a..d0c3652 100644 --- a/frontend/model/Pipe.ts +++ b/frontend/model/Pipe.ts @@ -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 diff --git a/frontend/model/Raspberry.ts b/frontend/model/Raspberry.ts index aaec5d8..861055d 100644 --- a/frontend/model/Raspberry.ts +++ b/frontend/model/Raspberry.ts @@ -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 {