Commented everything except some Methods in Raspberry.ts
This commit is contained in:
parent
fafbfa1b32
commit
6c459c1581
7 changed files with 87 additions and 19 deletions
|
|
@ -22,6 +22,9 @@ function setup() {
|
||||||
setupGame();
|
setupGame();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets up everything needed for the game
|
||||||
|
*/
|
||||||
function setupGame() {
|
function setupGame() {
|
||||||
paused = true;
|
paused = true;
|
||||||
|
|
||||||
|
|
@ -29,6 +32,7 @@ function setupGame() {
|
||||||
raspberry = new Raspberry();
|
raspberry = new Raspberry();
|
||||||
raspberry.image = raspberryImagePath;
|
raspberry.image = raspberryImagePath;
|
||||||
|
|
||||||
|
// Create all obstacles
|
||||||
obstacles = [];
|
obstacles = [];
|
||||||
obstacles.push(new Obstacle(
|
obstacles.push(new Obstacle(
|
||||||
new Position(width, 0),
|
new Position(width, 0),
|
||||||
|
|
@ -49,7 +53,8 @@ function setupGame() {
|
||||||
pipeImagePath,
|
pipeImagePath,
|
||||||
));
|
));
|
||||||
|
|
||||||
obstacles.forEach((obstacle) => obstacle.resetPosition(false));
|
// Randomize position of all Obstacles
|
||||||
|
obstacles.forEach((obstacle) => obstacle.randomizeHeight());
|
||||||
}
|
}
|
||||||
|
|
||||||
function draw() {
|
function draw() {
|
||||||
|
|
@ -59,6 +64,7 @@ function draw() {
|
||||||
}
|
}
|
||||||
raspberry.draw();
|
raspberry.draw();
|
||||||
|
|
||||||
|
// Reset Obstacles Position
|
||||||
obstacles.forEach((obstacle) => {
|
obstacles.forEach((obstacle) => {
|
||||||
if (!paused) {
|
if (!paused) {
|
||||||
obstacle.update();
|
obstacle.update();
|
||||||
|
|
@ -68,6 +74,7 @@ function draw() {
|
||||||
obstacle.draw();
|
obstacle.draw();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Check for collisions with pipes and set score
|
||||||
if (!paused) {
|
if (!paused) {
|
||||||
if (obstacles[0].collides(raspberry)) {
|
if (obstacles[0].collides(raspberry)) {
|
||||||
setupGame();
|
setupGame();
|
||||||
|
|
@ -75,21 +82,29 @@ function draw() {
|
||||||
checkRaspberryScore();
|
checkRaspberryScore();
|
||||||
obstacles[0].draw();
|
obstacles[0].draw();
|
||||||
}
|
}
|
||||||
|
|
||||||
push();
|
push();
|
||||||
fill(200, 100, 60);
|
fill(200, 100, 60);
|
||||||
text(score, width / 2, height / 10, width, height);
|
text(score, width / 2, height / 10, width, height);
|
||||||
pop();
|
pop();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check if obstacle positions should be reset and reset if so
|
||||||
|
* @param obstacle obstacle to check
|
||||||
|
*/
|
||||||
function checkObstacleReset(obstacle: Obstacle) {
|
function checkObstacleReset(obstacle: Obstacle) {
|
||||||
if (obstacle.position.x < -obstacleWidth) {
|
if (obstacle.position.x < -obstacleWidth) {
|
||||||
obstacle.resetPosition(true);
|
obstacle.resetPosition();
|
||||||
obstacles.shift();
|
obstacles.shift();
|
||||||
obstacles.push(obstacle);
|
obstacles.push(obstacle);
|
||||||
hasAlreadyScored = false;
|
hasAlreadyScored = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check if the raspberry should score and set score
|
||||||
|
*/
|
||||||
function checkRaspberryScore() {
|
function checkRaspberryScore() {
|
||||||
if ((obstacles[0].position.x + obstacles[0].width / 2) < (raspberry.position.x + raspberry.width / 2)
|
if ((obstacles[0].position.x + obstacles[0].width / 2) < (raspberry.position.x + raspberry.width / 2)
|
||||||
&& !hasAlreadyScored) {
|
&& !hasAlreadyScored) {
|
||||||
|
|
@ -99,9 +114,12 @@ function checkRaspberryScore() {
|
||||||
}
|
}
|
||||||
|
|
||||||
function keyPressed() {
|
function keyPressed() {
|
||||||
|
// Jump
|
||||||
if (key.toLowerCase() == "k") {
|
if (key.toLowerCase() == "k") {
|
||||||
raspberry.boost();
|
raspberry.boost();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Pause the Game
|
||||||
if (key == "Escape") {
|
if (key == "Escape") {
|
||||||
paused = !paused;
|
paused = !paused;
|
||||||
} else if (paused) {
|
} else if (paused) {
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,7 @@
|
||||||
interface Collidable {
|
interface Collidable {
|
||||||
|
/**
|
||||||
|
* Determines when two entities collide
|
||||||
|
* @param o other entity
|
||||||
|
*/
|
||||||
collides(o: Entity): boolean;
|
collides(o: Entity): boolean;
|
||||||
}
|
}
|
||||||
|
|
@ -37,9 +37,15 @@ abstract class Entity {
|
||||||
set showHitbox(value: boolean) {
|
set showHitbox(value: boolean) {
|
||||||
this._showHitbox = value;
|
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) {
|
protected constructor(position: Position, width: number, height: number, fill: number) {
|
||||||
this.position = position;
|
this.position = position;
|
||||||
this.width = width;
|
this.width = width;
|
||||||
|
|
|
||||||
|
|
@ -8,8 +8,12 @@ class Obstacle extends Entity implements Collidable {
|
||||||
private static startX: number;
|
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)
|
* (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) {
|
constructor(position: Position, obstacleWidth: number, obstacleHeight: number, pipeImagePath: string) {
|
||||||
super(position, obstacleWidth, obstacleHeight, 0);
|
super(position, obstacleWidth, obstacleHeight, 0);
|
||||||
|
|
@ -22,18 +26,32 @@ class Obstacle extends Entity implements Collidable {
|
||||||
Obstacle.startX = width;
|
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.height = this.randomRange(this.padding, height - this.padding - this.distanceBetweenPipes);
|
||||||
this.pipeTop.position.y = 0;
|
this.pipeTop.position.y = 0;
|
||||||
this.pipeBottom.position.y = this.pipeTop.height + this.distanceBetweenPipes;
|
this.pipeBottom.position.y = this.pipeTop.height + this.distanceBetweenPipes;
|
||||||
this.pipeBottom.height = height - this.pipeTop.height - this.padding;
|
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 {
|
private randomRange(min: number, max: number): number {
|
||||||
return Math.random() * (max - min) + min;
|
return Math.random() * (max - min) + min;
|
||||||
}
|
}
|
||||||
|
|
@ -49,6 +67,10 @@ class Obstacle extends Entity implements Collidable {
|
||||||
this.pipeBottom.draw();
|
this.pipeBottom.draw();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Determines when the obstacle is colliding with another entity
|
||||||
|
* @param o other entity
|
||||||
|
*/
|
||||||
public collides(o: Entity): boolean {
|
public collides(o: Entity): boolean {
|
||||||
return this.pipeTop.collides(o) || this.pipeBottom.collides(o);
|
return this.pipeTop.collides(o) || this.pipeBottom.collides(o);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -9,9 +9,14 @@ class Pipe extends Entity implements Collidable {
|
||||||
set image(path: string) {
|
set image(path: string) {
|
||||||
this._image = loadImage(path);
|
this._image = loadImage(path);
|
||||||
}
|
}
|
||||||
|
|
||||||
//endregion
|
//endregion
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructs the pipe
|
||||||
|
* @param positionX starting x-Position
|
||||||
|
* @param width pipe width
|
||||||
|
* @param height pipe height
|
||||||
|
*/
|
||||||
constructor(positionX: number, width: number, height: number) {
|
constructor(positionX: number, width: number, height: number) {
|
||||||
super(new Position(positionX, 0), width, height, 0);
|
super(new Position(positionX, 0), width, height, 0);
|
||||||
}
|
}
|
||||||
|
|
@ -32,6 +37,10 @@ class Pipe extends Entity implements Collidable {
|
||||||
pop();
|
pop();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Determines when the pipe is colliding with another entity
|
||||||
|
* @param o other entity
|
||||||
|
*/
|
||||||
collides(o: Entity): boolean {
|
collides(o: Entity): boolean {
|
||||||
return this.position.x < (o.position.x + o.width) && //inside left border
|
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
|
(this.position.x + this.width) > o.position.x && //but not outside right border
|
||||||
|
|
|
||||||
|
|
@ -20,6 +20,11 @@ class Position {
|
||||||
}
|
}
|
||||||
//endregion
|
//endregion
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructs the position
|
||||||
|
* @param x x-Position
|
||||||
|
* @param y y-Position
|
||||||
|
*/
|
||||||
constructor(x: number, y: number) {
|
constructor(x: number, y: number) {
|
||||||
this._x = x;
|
this._x = x;
|
||||||
this._y = y;
|
this._y = y;
|
||||||
|
|
|
||||||
|
|
@ -6,8 +6,6 @@ class Raspberry extends Entity {
|
||||||
private static readonly maxVelocity: number = 100;
|
private static readonly maxVelocity: number = 100;
|
||||||
|
|
||||||
//region Getter & Setter
|
//region Getter & Setter
|
||||||
|
|
||||||
|
|
||||||
get velocity(): number {
|
get velocity(): number {
|
||||||
return this._velocity;
|
return this._velocity;
|
||||||
}
|
}
|
||||||
|
|
@ -24,8 +22,11 @@ class Raspberry extends Entity {
|
||||||
this._image = loadImage(path);
|
this._image = loadImage(path);
|
||||||
}
|
}
|
||||||
|
|
||||||
//endregion
|
//endregion
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructs the Raspberry with fixed sizes
|
||||||
|
*/
|
||||||
constructor() {
|
constructor() {
|
||||||
super(new Position(width / 6, height / 2), 180, 70, 0);
|
super(new Position(width / 6, height / 2), 180, 70, 0);
|
||||||
}
|
}
|
||||||
|
|
@ -35,14 +36,17 @@ class Raspberry extends Entity {
|
||||||
this.forceBoundaries();
|
this.forceBoundaries();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Lets the Raspberry fall to the ground
|
||||||
|
*/
|
||||||
private applyGravity(): void {
|
private applyGravity(): void {
|
||||||
this.velocity += this.gravity;
|
this.velocity += this.gravity;
|
||||||
this.position.y += this.velocity;
|
this.position.y += this.velocity;
|
||||||
}
|
}
|
||||||
|
|
||||||
private forceBoundaries(): void {
|
private forceBoundaries(): void {
|
||||||
if (this.position.y+this.height > height) {
|
if (this.position.y + this.height > height) {
|
||||||
this.position.y = height-this.height;
|
this.position.y = height - this.height;
|
||||||
this.velocity = 0;
|
this.velocity = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -60,7 +64,7 @@ class Raspberry extends Entity {
|
||||||
push();
|
push();
|
||||||
noFill();
|
noFill();
|
||||||
translate(this.position.x, this.position.y);
|
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);
|
image(this.image, 0, 0, this.width, this.height);
|
||||||
if (!this.showHitbox) {
|
if (!this.showHitbox) {
|
||||||
noStroke();
|
noStroke();
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue