diff --git a/modules/levelSelection.mjs b/modules/levelSelection.mjs
index 974d5cc..f51c1f0 100644
--- a/modules/levelSelection.mjs
+++ b/modules/levelSelection.mjs
@@ -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();};
@@ -18,15 +21,31 @@ export const fillLevelsSelection = (gameState, ctx) => {
selectionButton.setAttribute("array-index", i);
selectionButton.addEventListener("click", (click) => {
selectLevel(ctx, gameState, click.srcElement.getAttribute("array-index"));
- // let blueprint = levelsBlueprint[
- // click.srcElement.getAttribute("array-index")
- // ];
- // gameState.playground = generatePlayground(blueprint.structure, gameState.width, gameState.height);
- // gameState.timer = new Timer(blueprint.time, gameState.timer.expireFunction);
- // gameState.playground.draw(ctx, gameState.width, gameState.height);
});
- selectionButton.innerText = "Level" + i;
+ selectionButton.innerText = "Level " + i;
listElement.appendChild(selectionButton);
levelList.appendChild(listElement);
}
}
+
+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);
+ }
+}
diff --git a/modules/levels.mjs b/modules/levels.mjs
index 346efae..e5f0289 100644
--- a/modules/levels.mjs
+++ b/modules/levels.mjs
@@ -25,7 +25,23 @@ const level2Blueprint = {
time: 23000,
};
+const level3Blueprint = {
+ structure: [
+ [Square.Wall, Square.Wall, Square.Wall, Square.Wall, Square.Wall, Square.Wall, Square.Wall, Square.Wall, Square.Wall, Square.Wall,],
+ [Square.Wall, Square.Floor, Square.Floor, Square.Floor, Square.Wall, Square.Wall, Square.Wall, Square.Wall, Square.Wall, Square.Wall,],
+ [Square.Wall, Square.Floor, Square.Box, Square.Floor, Square.Destination, Square.Destination, Square.Destination, Square.Wall, Square.Wall, Square.Wall,],
+ [Square.Wall, Square.Wall, Square.Destination, Square.Box, Square.Box, Square.Destination, Square.Floor, Square.Floor, Square.Wall, Square.Wall,],
+ [Square.Wall, Square.Wall, Square.Destination, Square.Destination, Square.Floor, Square.Box, Square.Box, Square.Floor, Square.Wall, Square.Wall,],
+ [Square.Wall, Square.Wall, Square.Destination, Square.Box, Square.Box, Square.Floor, Square.Box, Square.Floor, Square.Floor, Square.Floor],
+ [Square.Wall, Square.Wall, Square.Player, Square.Destination, Square.Destination, Square.Box, Square.Box, Square.Floor, Square.Floor, Square.Wall,],
+ [Square.Wall, Square.Wall, Square.Floor, Square.Floor, Square.Floor, Square.Floor, Square.Floor, 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,],
+ ],
+ time: 8000,
+}
+
export const levelsBlueprint = [
level1Blueprint,
- level2Blueprint,
+ // level2Blueprint,
+ // level3Blueprint,
]
diff --git a/modules/timer.mjs b/modules/timer.mjs
index e79c6eb..916a2cb 100644
--- a/modules/timer.mjs
+++ b/modules/timer.mjs
@@ -2,8 +2,6 @@ export class Timer {
readDifficulty(slider) {
self.difficulty = 1.5 - slider.value * 0.01;
self.time = self.originalTime * self.difficulty;
- console.log("difficulty: " + self.difficulty);
- console.log("time: " + self.time);
}
constructor(time, expireFunction) {
@@ -48,8 +46,8 @@ export class Timer {
if (self.timeRunning) {
let timeStr = String(self.time).padStart(5, '0');
self.timerElement.innerHTML = "Time : " + timeStr.slice(0, 3) + '.' + timeStr.slice(3);
- if (self.time == 0) {
- this.expireFunction();
+ if (self.time <= 0) {
+ self.expireFunction();
clearInterval(self.intervalControler);
self.timeRunning = false;
}
diff --git a/script.js b/script.js
index b3c08cd..415cfd2 100644
--- a/script.js
+++ b/script.js
@@ -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();