refactor game.ts

This commit is contained in:
s-prechtl 2023-01-10 22:50:48 +01:00
parent 8b86437e06
commit e4d7935d27

View file

@ -19,14 +19,26 @@ let ready: boolean = true;
function setup() { function setup() {
backgroundImage = loadImage(BACKGROUND_IMAGE_PATH); backgroundImage = loadImage(BACKGROUND_IMAGE_PATH);
createCanvas(2000, 1000); createCanvas(2000, 1000);
setupObstacleConsts();
setupFont();
setupGame();
}
/**
* Sets up the constants needed for the game.
*/
function setupObstacleConsts() {
obstacleOffset = width / 3; obstacleOffset = width / 3;
Obstacle.distanceBetweenPipes = height / 2.5 Obstacle.distanceBetweenPipes = height / 2.5
Obstacle.startX = width; Obstacle.startX = width;
}
/**
* Set up the font.
*/
function setupFont() {
textSize(150); textSize(150);
textFont("resources/PressStart2P-Regular.ttf"); textFont("resources/PressStart2P-Regular.ttf");
setupGame();
} }
/** /**
@ -35,22 +47,18 @@ function setup() {
function setupGame() { function setupGame() {
paused = true; paused = true;
raspberry = new Raspberry(RASPBERRY_IMAGE_PATH); raspberry = new Raspberry(RASPBERRY_IMAGE_PATH);
setupObstacles();
}
Obstacle.distanceBetweenPipes = height / 2.5; /**
Obstacle.startX = width; * Clears the obstacles and reinitializes them.
*/
// Create all obstacles function setupObstacles() {
obstacles = []; obstacles = [];
for (let i = 0; i < 3; i++) { for (let i = 0; i < 3; i++) {
obstacles.push(new Obstacle( obstacles.push(
new Position(width + obstacleOffset * i, 0), new Obstacle(new Position(width + obstacleOffset * i, 0), OBSTACLE_WIDTH, height, PIPE_IMAGE_PATH));
OBSTACLE_WIDTH,
height,
PIPE_IMAGE_PATH,
));
} }
// Randomize position of all Obstacles
obstacles.forEach((obstacle) => obstacle.randomizeHeight()); obstacles.forEach((obstacle) => obstacle.randomizeHeight());
} }
@ -71,6 +79,7 @@ function drawGame() {
drawEntities(); drawEntities();
displayScore(); displayScore();
} }
/** /**
* Draws the game's enities. * Draws the game's enities.
*/ */
@ -94,7 +103,7 @@ function gameLoop() {
* Checks the collision between an obstacle and the raspberry. * Checks the collision between an obstacle and the raspberry.
* @param o * @param o
*/ */
function collisionCheck(o: Obstacle){ function collisionCheck(o: Obstacle) {
if (o.collides(raspberry)) { if (o.collides(raspberry)) {
die(); die();
setupGame(); setupGame();
@ -104,7 +113,7 @@ function collisionCheck(o: Obstacle){
/** /**
* Timeouts key events. * Timeouts key events.
*/ */
async function die() { function die() {
ready = false; ready = false;
hasDied = true; hasDied = true;
setTimeout(() => ready = true, 1000); setTimeout(() => ready = true, 1000);
@ -113,7 +122,7 @@ async function die() {
/** /**
* Displays the game score. * Displays the game score.
*/ */
function displayScore(){ function displayScore() {
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);
@ -174,7 +183,7 @@ function checkRaspberryScore() {
* Resets the score if game is started * Resets the score if game is started
*/ */
function resetScore(): void { function resetScore(): void {
if(hasDied){ if (hasDied) {
hasDied = false; hasDied = false;
score = 0; score = 0;
hasAlreadyScored = false; hasAlreadyScored = false;