quelque chose se passe à la fin des niveaux

This commit is contained in:
nbillard 2022-12-12 16:03:41 +01:00
parent dbdc35ec26
commit 34a25d4006
3 changed files with 25 additions and 9 deletions

View file

@ -3,6 +3,9 @@ import { generatePlayground } from '/modules/playground.mjs'
import { Timer } from '/modules/timer.mjs'
export const selectLevel = (ctx, gameState, id) => {
console.log(ctx);
console.log(gameState);
console.log(id);
gameState.playground = generatePlayground(levelsBlueprint[id].structure, gameState.width, gameState.height);
// TODO transfer expireFunction without a fail
// const expireFunc = () => {gameState.timer.expireFunction();};
@ -19,19 +22,30 @@ export const fillLevelsSelection = (gameState, ctx) => {
selectionButton.addEventListener("click", (click) => {
selectLevel(ctx, gameState, click.srcElement.getAttribute("array-index"));
});
selectionButton.innerText = "Level" + i;
selectionButton.innerText = "Level " + i;
listElement.appendChild(selectionButton);
levelList.appendChild(listElement);
}
}
export class LevelManager (
constructor(StartingLevelId = 0) {
export class LevelManager {
constructor(winFunction, StartingLevelId = 0) {
self.CurrentLevelId = StartingLevelId;
self.Completed = levelsBlueprint.map(() => { return false; });
self.winFunction = winFunction;
}
next(ctx, gameState) {
self.Completed[self.CurrentLevelId] = true;
console.log(self.Completed);
let allLevelsFinished = self.Completed.reduce((a, b) => {
return a && b;
}, true);
console.log(allLevelsFinished);
if (allLevelsFinished) {
self.winFunction();
}
self.CurrentLevelId++;
selectLevel(ctx, gameState, self.CurrentLevelId);
}
)
}

View file

@ -42,6 +42,6 @@ const level3Blueprint = {
export const levelsBlueprint = [
level1Blueprint,
level2Blueprint,
level3Blueprint,
// level2Blueprint,
// level3Blueprint,
]

View file

@ -1,7 +1,7 @@
import { generatePlayground } from '/modules/playground.mjs'
import { levelsBlueprint } from '/modules/levels.mjs'
import { MoveDirection } from '/modules/enums.mjs'
import { fillLevelsSelection, selectLevel } from '/modules/levelSelection.mjs'
import { fillLevelsSelection, selectLevel, LevelManager } from '/modules/levelSelection.mjs'
import { Timer } from '/modules/timer.mjs'
import { TutorialControler } from '/modules/tutorialControler.mjs'
@ -25,6 +25,9 @@ let gameState = {
alert("Les vaches mangent le foin");
}),
playable: false,
levelManager: new LevelManager( () => {
alert("Toutes les bottes sont rangées");
} ),
levelId: 0,
};
@ -54,8 +57,7 @@ window.addEventListener("keydown", (event) => {
}
gameState.playground.draw(ctx, canvas.width, canvas.height);
if (gameState.playground.isSolved()) {
gameState.levelId++;
selectLevel(ctx, gameState, gameState.levelId);
gameState.levelManager.next(ctx, gameState);
}
} else {
tutorial.next();