comments, comments, comments, refactor, refactor, refactor

This commit is contained in:
s-prechtl 2023-01-10 22:42:41 +01:00
parent ff3c31f077
commit 8b86437e06
6 changed files with 172 additions and 27 deletions

View file

@ -1,3 +1,6 @@
/**
* Collide-able objects.
*/
interface Collidable { interface Collidable {
/** /**
* Determines when two entities collide * Determines when two entities collide

View file

@ -1,46 +1,102 @@
/**
* General rectangular entities.
*/
abstract class Entity { abstract class Entity {
/**
* Position.
* @private
*/
private _position: Position; private _position: Position;
/**
* Width.
* @private
*/
private _width: number; private _width: number;
/**
* Height.
* @private
*/
private _height: number; private _height: number;
/**
* Color.
* @private
*/
private fill: number; private fill: number;
/**
* Whether the hitbox (rectangular surrounding) is shown, or not.
* @private
*/
private _showHitbox: boolean; private _showHitbox: boolean;
//region Getter & Setter //region Getter & Setter
/**
* Get position.
*/
get position(): Position { get position(): Position {
return this._position; return this._position;
} }
/**
* Set position.
* @param value
*/
set position(value: Position) { set position(value: Position) {
this._position = value; this._position = value;
} }
/**
* Get width.
*/
get width(): number { get width(): number {
return this._width; return this._width;
} }
/**
* Set width.
* @param value
*/
set width(value: number) { set width(value: number) {
this._width = value; this._width = value;
} }
/**
* Get height.
*/
get height(): number { get height(): number {
return this._height; return this._height;
} }
/**
* Set height.
* @param value
*/
set height(value: number) { set height(value: number) {
this._height = value; this._height = value;
} }
/**
* Get the hitbox's visibility.
*/
get showHitbox(): boolean { get showHitbox(): boolean {
return this._showHitbox; return this._showHitbox;
} }
/**
* Set the hitbox's visibility.
* @param value
*/
set showHitbox(value: boolean) { set showHitbox(value: boolean) {
this._showHitbox = value; this._showHitbox = value;
} }
//endregion //endregion
/** /**
* Constructs the Entity * Constructs the Entity.
* @param position starting Position * @param position starting Position
* @param width entity width * @param width entity width
* @param height entity height * @param height entity height

View file

@ -1,3 +1,6 @@
/**
* Obstacle of the game. Built from 2 pipes, one at the bottom, one at the top.
*/
class Obstacle extends Entity implements Collidable { class Obstacle extends Entity implements Collidable {
private pipeTop: Pipe; private pipeTop: Pipe;
private pipeBottom: Pipe; private pipeBottom: Pipe;
@ -16,8 +19,7 @@ class Obstacle extends Entity implements Collidable {
} }
/** /**
* Constructs the Obstacle with the given image * Constructs the Obstacle with the given image.
* (fill is not used here)
* @param position starting position of the obstacle * @param position starting position of the obstacle
* @param obstacleWidth width of the obstacle * @param obstacleWidth width of the obstacle
* @param obstacleHeight height of the obstacle * @param obstacleHeight height of the obstacle
@ -25,10 +27,20 @@ class Obstacle extends Entity implements Collidable {
*/ */
constructor(position: Position, obstacleWidth: number, obstacleHeight: number, pipeImagePath: string) { constructor(position: Position, obstacleWidth: number, obstacleHeight: number, pipeImagePath: string) {
super(position, obstacleWidth, obstacleHeight, 0); super(position, obstacleWidth, obstacleHeight, 0);
this.pipeTop = new Pipe(position.x, obstacleWidth, obstacleHeight); this.createPipes(position, obstacleHeight, obstacleWidth, pipeImagePath);
this.pipeBottom = new Pipe(position.x, obstacleWidth, obstacleHeight); }
this.pipeTop.image = pipeImagePath;
this.pipeBottom.image = pipeImagePath; /**
* Creates the pipes.
* @param position
* @param obstacleHeight
* @param obstacleWidth
* @param pipeImagePath
* @private
*/
private createPipes(position: Position, obstacleHeight: number, obstacleWidth: number, pipeImagePath: string) {
this.pipeTop = new Pipe(position.x, obstacleWidth, obstacleHeight, pipeImagePath);
this.pipeBottom = new Pipe(position.x, obstacleWidth, obstacleHeight, pipeImagePath);
} }
/** /**

View file

@ -1,39 +1,55 @@
/**
* Rectangular obstacle.
*/
class Pipe extends Entity implements Collidable { class Pipe extends Entity implements Collidable {
private _image: any; /**
* Pipe's image.
* @private
*/
private _image: p5.Image;
//region Getter & Setter //region Getter & Setter
get image(): any { /**
* Gets the image.
*/
get image(): p5.Image {
return this._image; return this._image;
} }
set image(path: string) { /**
* Sets the image.
* @param path Path to image
*/
set image(path: any) {
this._image = loadImage(path); this._image = loadImage(path);
} }
//endregion //endregion
/** /**
* Constructs the pipe * Constructs the pipe.
* @param positionX starting x-Position * @param positionX starting x-Position
* @param width pipe width * @param width pipe width
* @param height pipe height * @param height pipe height
* @param image path to image.
*/ */
constructor(positionX: number, width: number, height: number) { constructor(positionX: number, width: number, height: number, image: string) {
super(new Position(positionX, 0), width, height, 0); super(new Position(positionX, 0), width, height, 0);
this.image = image;
} }
public update(): void { /**
} * YAGNI.
*/
public update(): void {}
/**
* Draws the pipe.
*/
public draw(): void { public draw(): void {
push(); push();
image(this.image, this.position.x, this.position.y, this.width, this.height);
noFill(); noFill();
rect( image(this.image, this.position.x, this.position.y, this.width, this.height);
this.position.x, rect(this.position.x, this.position.y, this.width, this.height);
this.position.y,
this.width,
this.height
);
pop(); pop();
} }

View file

@ -1,27 +1,54 @@
/**
* 2D Point.
*/
class Position { class Position {
/**
* X coordinate.
* @private
*/
private _x: number; private _x: number;
/**
* Y coordinate.
* @private
*/
private _y: number; private _y: number;
//region Getter & Setter //region Getter & Setter
/**
* Get x.
*/
get x(): number { get x(): number {
return this._x; return this._x;
} }
/**
* Set x.
* @param value
*/
set x(value: number) { set x(value: number) {
this._x = value; this._x = value;
} }
/**
* Get y.
*/
get y(): number { get y(): number {
return this._y; return this._y;
} }
/**
* Set y.
* @param value
*/
set y(value: number) { set y(value: number) {
this._y = value; this._y = value;
} }
//endregion //endregion
/** /**
* Constructs the position * Constructs the position.
* @param x x-Position * @param x x-Position
* @param y y-Position * @param y y-Position
*/ */

View file

@ -92,6 +92,7 @@ class Raspberry extends Entity {
/** /**
* Constructs the Raspberry with fixed sizes. * Constructs the Raspberry with fixed sizes.
* @param image Path to image
*/ */
constructor(image: string) { constructor(image: string) {
Raspberry.position = new Position(width / 6, height / 2); Raspberry.position = new Position(width / 6, height / 2);
@ -159,13 +160,43 @@ class Raspberry extends Entity {
public draw(): void { public draw(): void {
push(); push();
noFill(); noFill();
translate(this.position.x, this.position.y); this.setPose();
rotate((PI / 2) * (this.velocity / Raspberry.maxVelocity)); this.drawObject();
pop();
}
/**
* Draws the rocket.
* @private
*/
private drawObject() {
this.drawHitBox();
this.drawRocket();
}
/**
* Handles the drawing of the object.
*/
private drawRocket() {
image(this.image, 0, 0, this.width, this.height); image(this.image, 0, 0, this.width, this.height);
rect(0, 0, this.width, this.height);
}
/**
* If enabled, draws the hitbox.
*/
private drawHitBox() {
if (!this.showHitbox) { if (!this.showHitbox) {
noStroke(); noStroke();
} }
rect(0, 0, this.width, this.height); }
pop();
/**
* Rotation and position of the rocket.
* @private
*/
private setPose() {
translate(this.position.x, this.position.y);
rotate((PI / 2) * (this.velocity / Raspberry.maxVelocity));
} }
} }