123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163 |
- class Controller
- {
- #game;
- #drawer;
- #colorsBank = ['black','silver','gray','maroon','red','purple','fuchsia','green','lime','olive','yellow','navy','blue','teal','aqua'];
- #historyHandler;
-
- 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.#historyHandler = new HistoryHandler();
- this.#drawer.computeGameParams(params.TUBESNUMBERS, params.TUBESLEVELS);
-
- this.ui = new UI(800, 600);
- this.lastTime = millis();
-
- 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)
- {
- let times = 0;
-
- do
- {
- targetTube.addColorLayer(sourceTube.removeColorLayer());
- times++;
- } while (this.canPourInto(sourceTube, targetTube) && !targetTube.isComplete());
-
- this.#historyHandler.log(sourceTube, targetTube, times);
-
- //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();
- }
- }
-
- undoPreviousAction()
- {
- this.#historyHandler.restore();
- }
-
- mousePressed(e)
- {
- let clickedTubeId = this.#drawer.getTubeIdAt(mouseX, mouseY);
- this.clickTube(clickedTubeId);
-
- this.needUpdate = true;
- }
-
- runLoop()
- {
- let timeDelta = millis() - this.lastTime;
- // peut-être pas nécessaire de limiter le framerate
- if (timeDelta < 1000 / 60)
- return;
- this.lastTime = millis();
-
- const steps = [
- (this.needUpdate || this.#game.isCompleted) && this.drawGame,
- this.#game.isComplete() && this.drawUI
- ]
-
- steps.forEach(step => step && step.call(this));
- }
-
- drawGame()
- {
- this.#drawer.draw(this.#game);
- }
-
- drawUI()
- {
- this.ui.drawWinView();
- }
- }
|