Add some comments

This commit is contained in:
Ronan 2023-01-06 16:00:47 +01:00
parent b818f74190
commit cf32c46c32
4 changed files with 50 additions and 8 deletions

View file

@ -1,13 +1,15 @@
/**
* @fileoverview This file contains the functions to select a level
*/
import { levelsBlueprint } from '/modules/levels.mjs'
import { generatePlayground } from '/modules/playground.mjs'
import { Timer } from '/modules/timer.mjs'
const prionicSequence = [0, 2, 6, 12, 20, 30, 42];
// This funcion is called when the player selects a level and starts the game
export const selectLevel = (ctx, gameState, id) => {
gameState.playground = generatePlayground(levelsBlueprint[id].structure, gameState.width, gameState.height);
// TODO transfer expireFunction without a fail
// const expireFunc = () => {gameState.timer.expireFunction();};
gameState.timer.setTime(levelsBlueprint[id].time);
gameState.playable = true;
gameState.tutorial.hide();
@ -15,6 +17,7 @@ export const selectLevel = (ctx, gameState, id) => {
gameState.playground.draw(ctx, gameState.width, gameState.height);
}
// This function fills the level selection menu with the levels
export const fillLevelsSelection = (gameState, ctx) => {
let levelList = document.getElementById('level-list');
for (let i = 0; i < levelsBlueprint.length; ++i) {
@ -30,6 +33,7 @@ export const fillLevelsSelection = (gameState, ctx) => {
}
}
// This class manages the levels and their completion
export class LevelManager {
constructor(winFunction, StartingLevelId = 0) {
self.CurrentLevelId = StartingLevelId;
@ -45,16 +49,13 @@ export class LevelManager {
};
}
// getFirstUncompleted() {
// self.getFirstUncompleted();
// }
// This function is called when the player completes a level
// It checks if all levels are completed and calls the winFunction
// If not, it selects the next level
next(ctx, gameState) {
let score = gameState.timer.getTime();
gameState.scoreboard.updateScoreCurrentGamer(score);
self.Completed[self.CurrentLevelId] = true;
let allLevelsFinished = self.Completed.reduce((a, b) => {
return a && b;
@ -62,8 +63,9 @@ export class LevelManager {
if (allLevelsFinished) {
self.winFunction();
}
self.CurrentLevelId = self.getFirstUncompleted();
console.log(self.CurrentLevelId);
// console.log(self.CurrentLevelId);
selectLevel(ctx, gameState, self.CurrentLevelId);
}
}

View file

@ -1,7 +1,9 @@
/**
* @fileoverview This file contains the blueprints for the levels.
*/
import { Square } from '/modules/enums.mjs';
// Blueprint for the first level
//
const level1Blueprint = {
structure:[[ Square.Wall, Square.Wall, Square.Wall, Square.Wall, Square.Wall, Square.Wall ],
[ Square.Wall, Square.Destination, Square.Box, Square.Floor, Square.Player, Square.Wall ],
@ -25,6 +27,7 @@ const level2Blueprint = {
time: 23000,
};
// Blueprint for the third level
const level3Blueprint = {
structure: [
[Square.Wall, Square.Wall, Square.Wall, Square.Wall, Square.Wall, Square.Wall, Square.Wall, Square.Wall, Square.Wall, Square.Wall,],
@ -40,6 +43,7 @@ const level3Blueprint = {
time: 8000,
}
// Blueprint for the fourth level
const level4Blueprint = {
structure: [
[Square.Wall, Square.Wall, Square.Wall, Square.Wall, Square.Wall, Square.Wall, Square.Wall, Square.Wall, Square.Wall, Square.Wall, Square.Wall,],
@ -56,6 +60,7 @@ const level4Blueprint = {
time: 3000,
}
// Blueprint for the fifth level
const level5Blueprint = {
structure: [
[Square.Wall, Square.Wall, Square.Wall, Square.Wall, Square.Wall, Square.Wall, Square.Wall, Square.Wall, Square.Wall, Square.Wall, Square.Wall, Square.Wall, Square.Wall, Square.Wall, Square.Wall, Square.Wall, Square.Wall, ],
@ -72,6 +77,7 @@ const level5Blueprint = {
time: 40000
}
// Array of blueprints for all levels
export const levelsBlueprint = [
level1Blueprint,
level2Blueprint,

View file

@ -1,3 +1,7 @@
/**
* @fileoverview This file contains the Playground class.
*/
import { Square, CanMove, MoveDirection } from '/modules/enums.mjs'
import { Position, copyPosition } from '/modules/position.mjs'
import { ForegroundTile, BackgroundTile } from '/modules/tiles.mjs'
@ -18,6 +22,7 @@ export const generatePlayground = (levelBlueprint, canvasWidth, canvasHeight) =>
x: NaN,
y: NaN,
};
levelBlueprint.forEach((levelRow, indexRow) => {
background.push([]);
foreground.push([]);

View file

@ -1,4 +1,14 @@
/**
* @fileoverview Timer class
*/
/**
* @class Timer
* @classdesc This class is responsible for the timer.
*/
export class Timer {
// Set time to solve the level by using the difficulty slider
readDifficulty(slider) {
self.difficulty = 1.5 - slider.value * 0.01;
self.time = self.originalTime * self.difficulty;
@ -8,13 +18,17 @@ export class Timer {
self.originalTime = time;
self.difficulty = 1.0;
self.time = time;
let difficultySlider = document.getElementById('difficulty-slider');
this.readDifficulty(difficultySlider);
difficultySlider.addEventListener("change", () => {
this.readDifficulty(difficultySlider);
})
self.expireFunction = expireFunction;
self.timerElement = document.getElementById('timer');
// Feature : Move the pause button when the mouse is over it
self.pauseButton1 = document.getElementById('pause-1');
self.pauseButton2 = document.getElementById('pause-2');
self.pauseButton2.style.visibility = "hidden";
@ -40,6 +54,8 @@ export class Timer {
self.pauseButton1.addEventListener("click", () => {
alert("All this effort for nothing");
});
self.timeRunning = false;
self.intervalController = setInterval(() => {
// self.timerElement.innerHTML = "Time : " + String(self.time).padStart(5, ' ').InsertAt('.',3);
@ -56,18 +72,31 @@ export class Timer {
}, 10);
}
/**
* Set the time of the timer.
* @param {Number} time
*/
setTime(time) {
self.time = time;
}
/**
* @returns {Number} The time of the timer.
*/
getTime() {
return self.time;
}
/**
* Start the timer.
*/
start() {
self.timeRunning = true;
}
/**
* Stop the timer.
*/
stop() {
self.timeRunning = false;
}