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' import { Timer } from '/modules/timer.mjs'
export const selectLevel = (ctx, gameState, id) => { 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); gameState.playground = generatePlayground(levelsBlueprint[id].structure, gameState.width, gameState.height);
// TODO transfer expireFunction without a fail // TODO transfer expireFunction without a fail
// const expireFunc = () => {gameState.timer.expireFunction();}; // const expireFunc = () => {gameState.timer.expireFunction();};
@ -25,13 +28,24 @@ export const fillLevelsSelection = (gameState, ctx) => {
} }
} }
export class LevelManager ( export class LevelManager {
constructor(StartingLevelId = 0) { constructor(winFunction, StartingLevelId = 0) {
self.CurrentLevelId = StartingLevelId; self.CurrentLevelId = StartingLevelId;
self.Completed = levelsBlueprint.map(() => { return false; });
self.winFunction = winFunction;
} }
next(ctx, gameState) { 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++; self.CurrentLevelId++;
selectLevel(ctx, gameState, self.CurrentLevelId); selectLevel(ctx, gameState, self.CurrentLevelId);
} }
) }

View file

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

View file

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