123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148 |
- class Controller
- {
- #game;
- #drawer;
- #colorsBank = ['black','silver','gray','maroon','red','purple','fuchsia','green','lime','olive','yellow','navy','blue','teal','aqua'];
-
- constructor()
- {
- this.initializeGame();
- }
-
- initializeGame() {
- let params = this.gatherMenuValues();
-
- this.game = new Game(params.TUBESNUMBERS, params.TUBESLEVELS);
- this.drawer = new Drawer(params.PADDING, params.SCALE);
-
- this.drawer.computeGameParams(params.TUBESNUMBERS, params.TUBESLEVELS);
- this.width = 800
- this.height = 600
- this.ui = new UI(this.width, this.height);
- this.lastTime = 0;
-
- this.initializeColorTubes(params.TUBESLEVELS - 1);
-
- this.needUpdate = true;
- }
-
- gatherMenuValues()
- {
- let inputTubesNumbers = parseInt(document.getElementById("tbNumber").value);
- let inputTubesLevels = parseInt(document.getElementById("tbLevel").value);
- let inputScale = parseInt(document.getElementById("scale").value);
- let inputPadding = parseInt(document.getElementById("padding").value);
-
- return {
- TUBESNUMBERS: inputTubesNumbers,
- TUBESLEVELS: inputTubesLevels,
- SCALE: inputScale,
- PADDING: inputPadding
- };
- }
-
- initializeColorTubes(LayersPerTube)
- {
- let nbTubes = this.game.tubesNumber() - 1;
- let allColors = [];
-
- for (let i = 0; i < nbTubes; i++)
- {
- let coloridx = i % this.#colorsBank.length;
- for (let times = 0; times < LayersPerTube; times++)
- {
- allColors.push(this.#colorsBank[coloridx]);
- }
- }
-
- for (let index = allColors.length - 1; index >= 0; index--) {
- let randomIndex = Math.floor(Math.random() * (index + 1));
- let tubeIndex = Math.floor(index / LayersPerTube);
-
- this.game.tubeAt(tubeIndex).addColorLayer(allColors[randomIndex]);
-
- allColors[randomIndex] = allColors[index];
- }
- }
-
- clickTube(tubeIndex)
- {
- let _clickedTube = this.game.tubeAt(tubeIndex);
- if (_clickedTube == null) return;
-
- if (_clickedTube == this.game.selectedTube())
- this.game.unselectTube();
- else if (this.canPourInto(this.game.selectedTube(), _clickedTube))
- {
- this.pourColorInto(this.game.selectedTube(), _clickedTube);
- this.game.unselectTube();
- }
- else if (!_clickedTube.isEmpty())
- this.game.selectTubeAt(tubeIndex);
- }
-
- canPourInto(sourceTube, targetTube)
- {
- return sourceTube != null && targetTube != null
- && sourceTube != targetTube
- && (targetTube.isEmpty() || sourceTube.peekTopColor() == targetTube.peekTopColor())
- && !targetTube.isFull();
- }
-
- pourColorInto(sourceTube, targetTube)
- {
- while (this.canPourInto(sourceTube, targetTube))
- {
- targetTube.addColorLayer(sourceTube.removeColorLayer());
- }
-
- this.checkTubeCompletion(sourceTube);
- this.checkTubeCompletion(targetTube);
- }
-
- checkTubeCompletion(tube)
- {
- if (tube.isComplete())
- {
- this.game.removeTube(tube);
- this.checkGameCompletion();
- }
- }
-
- checkGameCompletion()
- {
- if (this.game.tubesNumber() == 1 && this.game.tubeAt(0).isEmpty())
- {
- this.game.clean();
- }
- }
-
- mousePressed(e)
- {
- let clickedTubeId = this.drawer.getTubeIdAt(mouseX, mouseY);
- this.clickTube(clickedTubeId);
-
- this.needUpdate = true;
- }
-
- runLoop()
- {
- const timeDelta = millis() - lastTime;
- // peut-être pas nécessaire de limiter le framerate
- if (timeDelta < 50)
- return;
- this.lastTime = millis();
-
- const steps = [
- (this.needUpdate || this.game.isCompleted) && drawGame,
- this.game.isCompleted && drawUI
- ]
-
- steps.forEach(step => step && step());
- }
-
- function drawUI()
- {
- this.ui.drawWinView();
- }
- }
|