From c18fb9312dbe8b97bd48e287a4681c3bf97f2fd7 Mon Sep 17 00:00:00 2001 From: nbillard Date: Mon, 12 Dec 2022 15:34:06 +0100 Subject: [PATCH 1/2] starting levelManager class --- index.html | 6 +++--- modules/levelSelection.mjs | 17 +++++++++++------ modules/levels.mjs | 16 ++++++++++++++++ modules/timer.mjs | 6 ++---- 4 files changed, 32 insertions(+), 13 deletions(-) diff --git a/index.html b/index.html index 41c256b..e307bfe 100644 --- a/index.html +++ b/index.html @@ -22,11 +22,11 @@
-
+
Time

@@ -34,7 +34,7 @@
-
+
diff --git a/modules/levelSelection.mjs b/modules/levelSelection.mjs index 974d5cc..cc31074 100644 --- a/modules/levelSelection.mjs +++ b/modules/levelSelection.mjs @@ -18,15 +18,20 @@ 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; listElement.appendChild(selectionButton); levelList.appendChild(listElement); } } + +export class LevelManager ( + constructor(StartingLevelId = 0) { + self.CurrentLevelId = StartingLevelId; + } + + next(ctx, gameState) { + self.CurrentLevelId++; + selectLevel(ctx, gameState, self.CurrentLevelId); + } +) diff --git a/modules/levels.mjs b/modules/levels.mjs index 346efae..4b6e860 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, + level3Blueprint, ] diff --git a/modules/timer.mjs b/modules/timer.mjs index 8ecf015..2ecff4e 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; } From 34a25d40064bfcab54e83f50cd09fdf96c93df78 Mon Sep 17 00:00:00 2001 From: nbillard Date: Mon, 12 Dec 2022 16:03:41 +0100 Subject: [PATCH 2/2] =?UTF-8?q?quelque=20chose=20se=20passe=20=C3=A0=20la?= =?UTF-8?q?=20fin=20des=20niveaux?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- modules/levelSelection.mjs | 22 ++++++++++++++++++---- modules/levels.mjs | 4 ++-- script.js | 8 +++++--- 3 files changed, 25 insertions(+), 9 deletions(-) diff --git a/modules/levelSelection.mjs b/modules/levelSelection.mjs index cc31074..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();}; @@ -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); } -) +} diff --git a/modules/levels.mjs b/modules/levels.mjs index 4b6e860..e5f0289 100644 --- a/modules/levels.mjs +++ b/modules/levels.mjs @@ -42,6 +42,6 @@ const level3Blueprint = { export const levelsBlueprint = [ level1Blueprint, - level2Blueprint, - level3Blueprint, + // level2Blueprint, + // level3Blueprint, ] 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();