Commented everything except some Methods in Raspberry.ts

This commit is contained in:
dhain 2022-12-20 09:40:21 +01:00
parent fafbfa1b32
commit 6c459c1581
7 changed files with 87 additions and 19 deletions

View file

@ -22,6 +22,9 @@ function setup() {
setupGame();
}
/**
* Sets up everything needed for the game
*/
function setupGame() {
paused = true;
@ -29,6 +32,7 @@ function setupGame() {
raspberry = new Raspberry();
raspberry.image = raspberryImagePath;
// Create all obstacles
obstacles = [];
obstacles.push(new Obstacle(
new Position(width, 0),
@ -49,7 +53,8 @@ function setupGame() {
pipeImagePath,
));
obstacles.forEach((obstacle) => obstacle.resetPosition(false));
// Randomize position of all Obstacles
obstacles.forEach((obstacle) => obstacle.randomizeHeight());
}
function draw() {
@ -59,6 +64,7 @@ function draw() {
}
raspberry.draw();
// Reset Obstacles Position
obstacles.forEach((obstacle) => {
if (!paused) {
obstacle.update();
@ -68,6 +74,7 @@ function draw() {
obstacle.draw();
});
// Check for collisions with pipes and set score
if (!paused) {
if (obstacles[0].collides(raspberry)) {
setupGame();
@ -75,21 +82,29 @@ function draw() {
checkRaspberryScore();
obstacles[0].draw();
}
push();
fill(200, 100, 60);
text(score, width / 2, height / 10, width, height);
pop();
}
/**
* Check if obstacle positions should be reset and reset if so
* @param obstacle obstacle to check
*/
function checkObstacleReset(obstacle: Obstacle) {
if (obstacle.position.x < -obstacleWidth) {
obstacle.resetPosition(true);
obstacle.resetPosition();
obstacles.shift();
obstacles.push(obstacle);
hasAlreadyScored = false;
}
}
/**
* Check if the raspberry should score and set score
*/
function checkRaspberryScore() {
if ((obstacles[0].position.x + obstacles[0].width / 2) < (raspberry.position.x + raspberry.width / 2)
&& !hasAlreadyScored) {
@ -99,9 +114,12 @@ function checkRaspberryScore() {
}
function keyPressed() {
// Jump
if (key.toLowerCase() == "k") {
raspberry.boost();
}
// Pause the Game
if (key == "Escape") {
paused = !paused;
} else if (paused) {

View file

@ -1,3 +1,7 @@
interface Collidable {
/**
* Determines when two entities collide
* @param o other entity
*/
collides(o: Entity): boolean;
}

View file

@ -37,9 +37,15 @@ abstract class Entity {
set showHitbox(value: boolean) {
this._showHitbox = value;
}
//endregion
//endregion
/**
* Constructs the Entity
* @param position starting Position
* @param width entity width
* @param height entity height
* @param fill fill color
*/
protected constructor(position: Position, width: number, height: number, fill: number) {
this.position = position;
this.width = width;

View file

@ -8,8 +8,12 @@ class Obstacle extends Entity implements Collidable {
private static startX: number;
/**
* Constructs the Obstacle using the top and bottom Pipe
* Constructs the Obstacle with the given image
* (fill is not used here)
* @param position starting position of the obstacle
* @param obstacleWidth width of the obstacle
* @param obstacleHeight height of the obstacle
* @param pipeImagePath path to the image to be used
*/
constructor(position: Position, obstacleWidth: number, obstacleHeight: number, pipeImagePath: string) {
super(position, obstacleWidth, obstacleHeight, 0);
@ -22,18 +26,32 @@ class Obstacle extends Entity implements Collidable {
Obstacle.startX = width;
}
public resetPosition(resetX: boolean): void {
/**
* Resets the position of the obstacle to the Obstacle.startX variable
* Randomises the height of the pipes using the padding variable
*/
public resetPosition(): void {
this.randomizeHeight();
this.pipeBottom.position.x = Obstacle.startX;
this.pipeTop.position.x = Obstacle.startX;
}
/**
* Randomizes the height of the pipes
*/
public randomizeHeight(): void {
this.pipeTop.height = this.randomRange(this.padding, height - this.padding - this.distanceBetweenPipes);
this.pipeTop.position.y = 0;
this.pipeBottom.position.y = this.pipeTop.height + this.distanceBetweenPipes;
this.pipeBottom.height = height - this.pipeTop.height - this.padding;
if (resetX) {
this.pipeBottom.position.x = Obstacle.startX;
this.pipeTop.position.x = Obstacle.startX;
}
}
/**
* Creates a random number between the min and max parameter
* @param min minimum number
* @param max maximum number
*/
private randomRange(min: number, max: number): number {
return Math.random() * (max - min) + min;
}
@ -49,6 +67,10 @@ class Obstacle extends Entity implements Collidable {
this.pipeBottom.draw();
}
/**
* Determines when the obstacle is colliding with another entity
* @param o other entity
*/
public collides(o: Entity): boolean {
return this.pipeTop.collides(o) || this.pipeBottom.collides(o);
}

View file

@ -9,9 +9,14 @@ class Pipe extends Entity implements Collidable {
set image(path: string) {
this._image = loadImage(path);
}
//endregion
/**
* Constructs the pipe
* @param positionX starting x-Position
* @param width pipe width
* @param height pipe height
*/
constructor(positionX: number, width: number, height: number) {
super(new Position(positionX, 0), width, height, 0);
}
@ -32,6 +37,10 @@ class Pipe extends Entity implements Collidable {
pop();
}
/**
* Determines when the pipe is colliding with another entity
* @param o other entity
*/
collides(o: Entity): boolean {
return this.position.x < (o.position.x + o.width) && //inside left border
(this.position.x + this.width) > o.position.x && //but not outside right border

View file

@ -20,6 +20,11 @@ class Position {
}
//endregion
/**
* Constructs the position
* @param x x-Position
* @param y y-Position
*/
constructor(x: number, y: number) {
this._x = x;
this._y = y;

View file

@ -6,8 +6,6 @@ class Raspberry extends Entity {
private static readonly maxVelocity: number = 100;
//region Getter & Setter
get velocity(): number {
return this._velocity;
}
@ -24,8 +22,11 @@ class Raspberry extends Entity {
this._image = loadImage(path);
}
//endregion
//endregion
/**
* Constructs the Raspberry with fixed sizes
*/
constructor() {
super(new Position(width / 6, height / 2), 180, 70, 0);
}
@ -35,14 +36,17 @@ class Raspberry extends Entity {
this.forceBoundaries();
}
/**
* Lets the Raspberry fall to the ground
*/
private applyGravity(): void {
this.velocity += this.gravity;
this.position.y += this.velocity;
}
private forceBoundaries(): void {
if (this.position.y+this.height > height) {
this.position.y = height-this.height;
if (this.position.y + this.height > height) {
this.position.y = height - this.height;
this.velocity = 0;
}
@ -51,7 +55,7 @@ class Raspberry extends Entity {
this.velocity = 0;
}
}
public boost(): void {
this.velocity += this.lift;
}
@ -60,7 +64,7 @@ class Raspberry extends Entity {
push();
noFill();
translate(this.position.x, this.position.y);
rotate((PI/2)*(this.velocity/Raspberry.maxVelocity));
rotate((PI / 2) * (this.velocity / Raspberry.maxVelocity));
image(this.image, 0, 0, this.width, this.height);
if (!this.showHitbox) {
noStroke();