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;
// Create all obstacles
obstacles = [];
for (let i = 0; i < 3; i++) {
obstacles.push(new Obstacle(
new Position(width + obstacleOffset * i, 0),
OBSTACLE_WIDTH,
height,
PIPE_IMAGE_PATH,
));
} }
// Randomize position of all Obstacles /**
* Clears the obstacles and reinitializes them.
*/
function setupObstacles() {
obstacles = [];
for (let i = 0; i < 3; i++) {
obstacles.push(
new Obstacle(new Position(width + obstacleOffset * i, 0), OBSTACLE_WIDTH, height, PIPE_IMAGE_PATH));
}
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.
*/ */
@ -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);