From f272d3a44fc6dbef0c52ba5148c9fc530ed4d1f6 Mon Sep 17 00:00:00 2001 From: dhain Date: Tue, 6 Dec 2022 10:36:14 +0100 Subject: [PATCH] Formatted files, added types and updated class diagram --- frontend/README.md | 47 ++++++++++++++++++++++++------------ frontend/models/Entity.ts | 8 +++--- frontend/models/Obstacle.ts | 8 +++--- frontend/models/Pipe.ts | 2 +- frontend/models/Position.ts | 2 ++ frontend/models/Raspberry.ts | 17 +++++++------ 6 files changed, 52 insertions(+), 32 deletions(-) diff --git a/frontend/README.md b/frontend/README.md index 146fbaf..8343dd3 100644 --- a/frontend/README.md +++ b/frontend/README.md @@ -2,32 +2,47 @@ classDiagram Entity <|-- Raspberry Entity <|-- Obstacle -Entity: -Position position -Entity: -int width -Entity: -int height -Entity: -update() -Entity: -draw() +Entity <|-- Pipe +Entity: -number fill +Entity: +Position position +Entity: +number width +Entity: +number height +Entity: +abstract update() +Entity: +draw() Entity: -detectCollision(Entity other) + class Raspberry{ --input() +-number lift +-number gravity +-static number maxVelocity ++number velocity + +-applyGravity() +-forceBoundaries() -boost() ++update() } class Obstacle{ -Entity pipeTop -Entity pipeBottom --int distanceBetweenPipes --int padding --resetPosition() +-number distanceBetweenPipes +-number padding +-number speed +-static number startX + +-randomRange() ++resetPosition() ++update() ++draw() +} + +class Pipe { ++update() } class Position{ --int _x --int _y - -+get x() -+set x() -+get y() -+set y() ++int x ++int y } ``` \ No newline at end of file diff --git a/frontend/models/Entity.ts b/frontend/models/Entity.ts index 6b0f1a5..77f699b 100644 --- a/frontend/models/Entity.ts +++ b/frontend/models/Entity.ts @@ -2,7 +2,7 @@ abstract class Entity { private _position: Position; private _width: number; private _height: number; - private _fill: number; + private fill: number; //region Getter & Setter get position(): Position { @@ -34,13 +34,13 @@ abstract class Entity { this.position = position; this.width = width; this.height = height; - this._fill = fill; + this.fill = fill; } public abstract update(): void; - public draw() { - fill(this._fill); + public draw(): void { + fill(this.fill); rect(this.position.x, this.position.y, this.width, this.height); } } diff --git a/frontend/models/Obstacle.ts b/frontend/models/Obstacle.ts index c9abf7f..9f30713 100644 --- a/frontend/models/Obstacle.ts +++ b/frontend/models/Obstacle.ts @@ -21,7 +21,7 @@ class Obstacle extends Entity { Obstacle.startX = width; } - public resetPosition() { + public resetPosition(): void { this.pipeBottom.position.y = this.distanceBetweenPipes + this.randomRange(0, height - this.padding - 1.2 * this.distanceBetweenPipes); this.pipeBottom.position.x = Obstacle.startX; @@ -29,16 +29,16 @@ class Obstacle extends Entity { this.pipeTop.position.x = Obstacle.startX; } - private randomRange(min: number, max: number) { + private randomRange(min: number, max: number): number { return Math.random() * (max - min) + min; } - public update() { + public update(): void { this.pipeTop.position.x -= this.speed; this.pipeBottom.position.x -= this.speed; } - public draw() { + public draw(): void { fill(10, 200, 100); //TODO do not make static rect( this.pipeTop.position.x, diff --git a/frontend/models/Pipe.ts b/frontend/models/Pipe.ts index df270fd..3fa122a 100644 --- a/frontend/models/Pipe.ts +++ b/frontend/models/Pipe.ts @@ -3,6 +3,6 @@ class Pipe extends Entity { super(position, width, height, 0); } - update(): void { + public update(): void { } } \ No newline at end of file diff --git a/frontend/models/Position.ts b/frontend/models/Position.ts index 99ca835..6c7e28b 100644 --- a/frontend/models/Position.ts +++ b/frontend/models/Position.ts @@ -2,6 +2,7 @@ class Position { private _x: number; private _y: number; + //region Getter & Setter get x(): number { return this._x; } @@ -17,6 +18,7 @@ class Position { set y(value: number) { this._y = value; } + //endregion constructor(x: number, y: number) { this._x = x; diff --git a/frontend/models/Raspberry.ts b/frontend/models/Raspberry.ts index a600c59..8f6c627 100644 --- a/frontend/models/Raspberry.ts +++ b/frontend/models/Raspberry.ts @@ -1,13 +1,14 @@ -class Raspberry extends Entity{ +class Raspberry extends Entity { private lift: number = -10; private gravity: number = 1; private _velocity: number = 0; - private static maxVelocity = 5; + private static maxVelocity: number = 5; constructor() { - super(new Position(2*width/6, height/2), 10, 10, 0); + super(new Position(2 * width / 6, height / 2), 10, 10, 0); } + //region Getter & Setter get velocity(): number { return this._velocity; } @@ -16,18 +17,20 @@ class Raspberry extends Entity{ this._velocity = (this.velocity > Raspberry.maxVelocity) ? Raspberry.maxVelocity : value; } - update() { + //endregion + + public update(): void { this.applyGravity(); this.forceBoundaries(); } - private applyGravity() { + private applyGravity(): void { if (this.position.y - this.height > 0) { this.velocity += this.gravity; } } - private forceBoundaries() { + private forceBoundaries(): void { if (this.position.y > height) { this.position.y = height; this.velocity = 0; @@ -39,7 +42,7 @@ class Raspberry extends Entity{ } } - public boost() { + public boost(): void { this.velocity += this.lift; } } \ No newline at end of file