Formatted files, added types and updated class diagram

This commit is contained in:
dhain 2022-12-06 10:36:14 +01:00
parent 4d22976621
commit f272d3a44f
6 changed files with 52 additions and 32 deletions

View file

@ -2,32 +2,47 @@
classDiagram classDiagram
Entity <|-- Raspberry Entity <|-- Raspberry
Entity <|-- Obstacle Entity <|-- Obstacle
Entity: -Position position Entity <|-- Pipe
Entity: -int width Entity: -number fill
Entity: -int height Entity: +Position position
Entity: -update() Entity: +number width
Entity: -draw() Entity: +number height
Entity: +abstract update()
Entity: +draw()
Entity: -detectCollision(Entity other) Entity: -detectCollision(Entity other)
class Raspberry{ class Raspberry{
-input() -number lift
-number gravity
-static number maxVelocity
+number velocity
-applyGravity()
-forceBoundaries()
-boost() -boost()
+update()
} }
class Obstacle{ class Obstacle{
-Entity pipeTop -Entity pipeTop
-Entity pipeBottom -Entity pipeBottom
-int distanceBetweenPipes -number distanceBetweenPipes
-int padding -number padding
-resetPosition() -number speed
-static number startX
-randomRange()
+resetPosition()
+update()
+draw()
}
class Pipe {
+update()
} }
class Position{ class Position{
-int _x +int x
-int _y +int y
+get x()
+set x()
+get y()
+set y()
} }
``` ```

View file

@ -2,7 +2,7 @@ abstract class Entity {
private _position: Position; private _position: Position;
private _width: number; private _width: number;
private _height: number; private _height: number;
private _fill: number; private fill: number;
//region Getter & Setter //region Getter & Setter
get position(): Position { get position(): Position {
@ -34,13 +34,13 @@ abstract class Entity {
this.position = position; this.position = position;
this.width = width; this.width = width;
this.height = height; this.height = height;
this._fill = fill; this.fill = fill;
} }
public abstract update(): void; public abstract update(): void;
public draw() { public draw(): void {
fill(this._fill); fill(this.fill);
rect(this.position.x, this.position.y, this.width, this.height); rect(this.position.x, this.position.y, this.width, this.height);
} }
} }

View file

@ -21,7 +21,7 @@ class Obstacle extends Entity {
Obstacle.startX = width; 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.y = this.distanceBetweenPipes + this.randomRange(0, height - this.padding - 1.2 * this.distanceBetweenPipes);
this.pipeBottom.position.x = Obstacle.startX; this.pipeBottom.position.x = Obstacle.startX;
@ -29,16 +29,16 @@ class Obstacle extends Entity {
this.pipeTop.position.x = Obstacle.startX; 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; return Math.random() * (max - min) + min;
} }
public update() { public update(): void {
this.pipeTop.position.x -= this.speed; this.pipeTop.position.x -= this.speed;
this.pipeBottom.position.x -= this.speed; this.pipeBottom.position.x -= this.speed;
} }
public draw() { public draw(): void {
fill(10, 200, 100); //TODO do not make static fill(10, 200, 100); //TODO do not make static
rect( rect(
this.pipeTop.position.x, this.pipeTop.position.x,

View file

@ -3,6 +3,6 @@ class Pipe extends Entity {
super(position, width, height, 0); super(position, width, height, 0);
} }
update(): void { public update(): void {
} }
} }

View file

@ -2,6 +2,7 @@ class Position {
private _x: number; private _x: number;
private _y: number; private _y: number;
//region Getter & Setter
get x(): number { get x(): number {
return this._x; return this._x;
} }
@ -17,6 +18,7 @@ class Position {
set y(value: number) { set y(value: number) {
this._y = value; this._y = value;
} }
//endregion
constructor(x: number, y: number) { constructor(x: number, y: number) {
this._x = x; this._x = x;

View file

@ -2,12 +2,13 @@ class Raspberry extends Entity{
private lift: number = -10; private lift: number = -10;
private gravity: number = 1; private gravity: number = 1;
private _velocity: number = 0; private _velocity: number = 0;
private static maxVelocity = 5; private static maxVelocity: number = 5;
constructor() { 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 { get velocity(): number {
return this._velocity; return this._velocity;
} }
@ -16,18 +17,20 @@ class Raspberry extends Entity{
this._velocity = (this.velocity > Raspberry.maxVelocity) ? Raspberry.maxVelocity : value; this._velocity = (this.velocity > Raspberry.maxVelocity) ? Raspberry.maxVelocity : value;
} }
update() { //endregion
public update(): void {
this.applyGravity(); this.applyGravity();
this.forceBoundaries(); this.forceBoundaries();
} }
private applyGravity() { private applyGravity(): void {
if (this.position.y - this.height > 0) { if (this.position.y - this.height > 0) {
this.velocity += this.gravity; this.velocity += this.gravity;
} }
} }
private forceBoundaries() { private forceBoundaries(): void {
if (this.position.y > height) { if (this.position.y > height) {
this.position.y = height; this.position.y = height;
this.velocity = 0; this.velocity = 0;
@ -39,7 +42,7 @@ class Raspberry extends Entity{
} }
} }
public boost() { public boost(): void {
this.velocity += this.lift; this.velocity += this.lift;
} }
} }