more refactoring even more comments
This commit is contained in:
parent
5f517c1a47
commit
52d1e2b5a5
2 changed files with 146 additions and 24 deletions
|
|
@ -11,6 +11,7 @@ let paused: boolean;
|
||||||
let score: number = 0;
|
let score: number = 0;
|
||||||
let hasAlreadyScored: boolean = false;
|
let hasAlreadyScored: boolean = false;
|
||||||
let hasDied: boolean = false;
|
let hasDied: boolean = false;
|
||||||
|
let ready: boolean = true;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* p5 setup function.
|
* p5 setup function.
|
||||||
|
|
@ -58,19 +59,55 @@ function setupGame() {
|
||||||
*/
|
*/
|
||||||
function draw() {
|
function draw() {
|
||||||
update();
|
update();
|
||||||
|
gameLoop();
|
||||||
|
drawGame();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Draws the game's elements.
|
||||||
|
*/
|
||||||
|
function drawGame() {
|
||||||
background(backgroundImage);
|
background(backgroundImage);
|
||||||
|
drawEntities();
|
||||||
|
displayScore();
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Draws the game's enities.
|
||||||
|
*/
|
||||||
|
function drawEntities() {
|
||||||
raspberry.draw();
|
raspberry.draw();
|
||||||
drawObstacles();
|
drawObstacles();
|
||||||
// Check for collisions with pipes and set score
|
|
||||||
if (!paused) {
|
|
||||||
if (obstacles[0].collides(raspberry)) {
|
|
||||||
hasDied = true;
|
|
||||||
setupGame();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Operations for the game's functionality.
|
||||||
|
*/
|
||||||
|
function gameLoop() {
|
||||||
|
if (!paused) {
|
||||||
|
collisionCheck(obstacles[0]);
|
||||||
checkRaspberryScore();
|
checkRaspberryScore();
|
||||||
obstacles[0].draw();
|
obstacles[0].draw();
|
||||||
}
|
}
|
||||||
displayScore();
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Checks the collision between an obstacle and the raspberry.
|
||||||
|
* @param o
|
||||||
|
*/
|
||||||
|
function collisionCheck(o: Obstacle){
|
||||||
|
if (o.collides(raspberry)) {
|
||||||
|
die();
|
||||||
|
setupGame();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Timeouts key events.
|
||||||
|
*/
|
||||||
|
async function die() {
|
||||||
|
ready = false;
|
||||||
|
hasDied = true;
|
||||||
|
setTimeout(() => ready = true, 1000);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -148,6 +185,7 @@ function resetScore(): void {
|
||||||
* Handler for key events.
|
* Handler for key events.
|
||||||
*/
|
*/
|
||||||
function keyPressed() {
|
function keyPressed() {
|
||||||
|
if (!ready) return;
|
||||||
// Jump
|
// Jump
|
||||||
if (key.toLowerCase() == "k") {
|
if (key.toLowerCase() == "k") {
|
||||||
resetScore();
|
resetScore();
|
||||||
|
|
|
||||||
|
|
@ -1,36 +1,97 @@
|
||||||
|
/**
|
||||||
|
* Raspberry class.
|
||||||
|
*/
|
||||||
class Raspberry extends Entity {
|
class Raspberry extends Entity {
|
||||||
|
/**
|
||||||
|
* Amount of lift applied when boosting.
|
||||||
|
* @private
|
||||||
|
*/
|
||||||
private readonly lift: number = -20;
|
private readonly lift: number = -20;
|
||||||
private readonly gravity: number = 1.314159265358979323846264338;
|
|
||||||
private _velocity: number = 0;
|
|
||||||
private _image: any;
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gravity applied.
|
||||||
|
* @private
|
||||||
|
*/
|
||||||
|
private readonly gravity: number = 1.314159265358979323846264338;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Current speed.
|
||||||
|
* @private
|
||||||
|
*/
|
||||||
|
private _velocity: number = 0;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Image for the raspberry.
|
||||||
|
* @private
|
||||||
|
*/
|
||||||
|
private _image: p5.Image;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Position.
|
||||||
|
* @private
|
||||||
|
*/
|
||||||
private static position: Position;
|
private static position: Position;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Maximum velocity, so the raspberry doesn't get to infinite speed when boosting.
|
||||||
|
* @private
|
||||||
|
*/
|
||||||
private static readonly maxVelocity: number = 100;
|
private static readonly maxVelocity: number = 100;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Width.
|
||||||
|
* @private
|
||||||
|
*/
|
||||||
private static readonly WIDTH: number = 180;
|
private static readonly WIDTH: number = 180;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Height.
|
||||||
|
* @private
|
||||||
|
*/
|
||||||
private static readonly HEIGHT: number = 70;
|
private static readonly HEIGHT: number = 70;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Color.
|
||||||
|
* @private
|
||||||
|
*/
|
||||||
private static readonly FILL: number = 0;
|
private static readonly FILL: number = 0;
|
||||||
|
|
||||||
//region Getter & Setter
|
//region Getter & Setter
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the velocity.
|
||||||
|
*/
|
||||||
get velocity(): number {
|
get velocity(): number {
|
||||||
return this._velocity;
|
return this._velocity;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the velocity.
|
||||||
|
* @param value
|
||||||
|
*/
|
||||||
set velocity(value: number) {
|
set velocity(value: number) {
|
||||||
this._velocity = (Math.abs(this.velocity) > Raspberry.maxVelocity) ? Raspberry.maxVelocity : value;
|
this._velocity = (Math.abs(this.velocity) > Raspberry.maxVelocity) ? Raspberry.maxVelocity : value;
|
||||||
}
|
}
|
||||||
|
|
||||||
get image(): any {
|
/**
|
||||||
|
* Gets the image.
|
||||||
|
*/
|
||||||
|
get image(): p5.Image {
|
||||||
return this._image;
|
return this._image;
|
||||||
}
|
}
|
||||||
|
|
||||||
set image(path: string) {
|
/**
|
||||||
|
* Sets the image by path.
|
||||||
|
* @param {string} path
|
||||||
|
*/
|
||||||
|
set image(path: any) {
|
||||||
this._image = loadImage(path);
|
this._image = loadImage(path);
|
||||||
}
|
}
|
||||||
|
|
||||||
//endregion
|
//endregion
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructs the Raspberry with fixed sizes
|
* Constructs the Raspberry with fixed sizes.
|
||||||
*/
|
*/
|
||||||
constructor(image: string) {
|
constructor(image: string) {
|
||||||
Raspberry.position = new Position(width / 6, height / 2);
|
Raspberry.position = new Position(width / 6, height / 2);
|
||||||
|
|
@ -38,6 +99,9 @@ class Raspberry extends Entity {
|
||||||
this.image = image;
|
this.image = image;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Applies gravity and keeps the raspberry within the canvas.
|
||||||
|
*/
|
||||||
public update(): void {
|
public update(): void {
|
||||||
this.applyGravity();
|
this.applyGravity();
|
||||||
this.forceBoundaries();
|
this.forceBoundaries();
|
||||||
|
|
@ -51,22 +115,47 @@ class Raspberry extends Entity {
|
||||||
this.position.y += this.velocity;
|
this.position.y += this.velocity;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Limits the raspberry's movement to the shown canvas.
|
||||||
|
* @private
|
||||||
|
*/
|
||||||
private forceBoundaries(): void {
|
private forceBoundaries(): void {
|
||||||
if (this.position.y + this.height > height) {
|
this.boundaryTop();
|
||||||
this.position.y = height - this.height;
|
this.boundaryBottom();
|
||||||
this.velocity = 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Forces the boundaries at the canvas' top.
|
||||||
|
* @private
|
||||||
|
*/
|
||||||
|
private boundaryTop(): void {
|
||||||
if (this.position.y < 0) {
|
if (this.position.y < 0) {
|
||||||
this.position.y = 0;
|
this.position.y = 0;
|
||||||
this.velocity = 0;
|
this.velocity = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Forces the boundaries at the canvas' bottom.
|
||||||
|
* @private
|
||||||
|
*/
|
||||||
|
private boundaryBottom(): void {
|
||||||
|
if (this.position.y + this.height > height) {
|
||||||
|
this.position.y = height - this.height;
|
||||||
|
this.velocity = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Lets the raspberry jump.
|
||||||
|
*/
|
||||||
public boost(): void {
|
public boost(): void {
|
||||||
this.velocity += this.lift;
|
this.velocity += this.lift;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Draws raspberry.
|
||||||
|
*/
|
||||||
public draw(): void {
|
public draw(): void {
|
||||||
push();
|
push();
|
||||||
noFill();
|
noFill();
|
||||||
|
|
@ -76,12 +165,7 @@ class Raspberry extends Entity {
|
||||||
if (!this.showHitbox) {
|
if (!this.showHitbox) {
|
||||||
noStroke();
|
noStroke();
|
||||||
}
|
}
|
||||||
rect(
|
rect(0, 0, this.width, this.height);
|
||||||
0,
|
|
||||||
0,
|
|
||||||
this.width,
|
|
||||||
this.height
|
|
||||||
);
|
|
||||||
pop();
|
pop();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue