C'est le jeu des tubes de couleur dans les pubs la
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

controller.js 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. class Controller
  2. {
  3. #game;
  4. #drawer;
  5. #colorsBank = ['black','silver','gray','maroon','red','purple','fuchsia','green','lime','olive','yellow','navy','blue','teal','aqua'];
  6. #historyHandler;
  7. constructor()
  8. {
  9. this.initializeGame();
  10. }
  11. drawer()
  12. {
  13. return this.#drawer
  14. }
  15. initializeGame() {
  16. let params = this.gatherMenuValues();
  17. this.#game = new Game(params.TUBESNUMBERS, params.TUBESLEVELS);
  18. this.#drawer = new Drawer(params.PADDING, params.SCALE, this.#game);
  19. this.#historyHandler = new HistoryHandler();
  20. this.ui = new UI(800, 600);
  21. this.lastTime = millis();
  22. this.initializeColorTubes(params.TUBESLEVELS - 1);
  23. this.needUpdate = true;
  24. }
  25. gatherMenuValues()
  26. {
  27. let inputTubesNumbers = parseInt(document.getElementById("tbNumber").value);
  28. let inputTubesLevels = parseInt(document.getElementById("tbLevel").value);
  29. let inputScale = parseInt(document.getElementById("scale").value);
  30. let inputPadding = parseInt(document.getElementById("padding").value);
  31. return {
  32. TUBESNUMBERS: inputTubesNumbers,
  33. TUBESLEVELS: inputTubesLevels,
  34. SCALE: inputScale,
  35. PADDING: inputPadding
  36. };
  37. }
  38. initializeColorTubes(LayersPerTube)
  39. {
  40. let nbTubes = this.#game.tubesNumber() - 1;
  41. let allColors = [];
  42. for (let i = 0; i < nbTubes; i++)
  43. {
  44. let coloridx = i % this.#colorsBank.length;
  45. for (let times = 0; times < LayersPerTube; times++)
  46. {
  47. allColors.push(this.#colorsBank[coloridx]);
  48. }
  49. }
  50. for (let index = allColors.length - 1; index >= 0; index--) {
  51. let randomIndex = Math.floor(Math.random() * (index + 1));
  52. let tubeIndex = Math.floor(index / LayersPerTube);
  53. this.#game.tubeAt(tubeIndex).addColorLayer(allColors[randomIndex]);
  54. allColors[randomIndex] = allColors[index];
  55. }
  56. }
  57. clickTube(tubeIndex)
  58. {
  59. let _clickedTube = this.#game.tubeAt(tubeIndex);
  60. let _selectedTube = this.#game.selectedTube();
  61. if (_clickedTube == null) return;
  62. if (_clickedTube == _selectedTube)
  63. {
  64. this.#drawer.removeSelectedEffect(this.#game.selectedTubeIndex());
  65. this.#game.unselectTube();
  66. }
  67. else if (this.canPourInto(_selectedTube, _clickedTube))
  68. {
  69. this.pourColorInto(_selectedTube, _clickedTube);
  70. this.#drawer.removeSelectedEffect(this.#game.selectedTubeIndex());
  71. this.#game.unselectTube();
  72. }
  73. else if (!_clickedTube.isEmpty())
  74. {
  75. if (_selectedTube != null) this.#drawer.removeSelectedEffect(this.#game.selectedTubeIndex());
  76. this.#drawer.applySelectedEffect(tubeIndex);
  77. this.#game.selectTubeAt(tubeIndex);
  78. }
  79. }
  80. canPourInto(sourceTube, targetTube)
  81. {
  82. return sourceTube != null && targetTube != null
  83. && sourceTube != targetTube
  84. && (targetTube.isEmpty() || sourceTube.peekTopColor() == targetTube.peekTopColor())
  85. && !targetTube.isFull();
  86. }
  87. pourColorInto(sourceTube, targetTube)
  88. {
  89. let times = 0;
  90. do
  91. {
  92. targetTube.addColorLayer(sourceTube.removeColorLayer());
  93. times++;
  94. } while (this.canPourInto(sourceTube, targetTube) && !targetTube.isComplete());
  95. this.#historyHandler.log(sourceTube, targetTube, times);
  96. //this.checkTubeCompletion(sourceTube);
  97. //this.checkTubeCompletion(targetTube);
  98. }
  99. checkTubeCompletion(tube)
  100. {
  101. if (tube.isComplete())
  102. {
  103. //this.#game.removeTube(tube);
  104. this.checkGameCompletion();
  105. }
  106. }
  107. checkGameCompletion()
  108. {
  109. if (this.#game.tubesNumber() == 1 && this.#game.tubeAt(0).isEmpty())
  110. {
  111. this.#game.clean();
  112. }
  113. }
  114. undoPreviousAction()
  115. {
  116. this.#historyHandler.restore();
  117. }
  118. mousePressed(e)
  119. {
  120. let clickedTubeId = this.#drawer.getTubeIdAt(mouseX, mouseY);
  121. this.clickTube(clickedTubeId);
  122. this.needUpdate = true;
  123. }
  124. runLoop()
  125. {
  126. let timeDelta = millis() - this.lastTime;
  127. // peut-être pas nécessaire de limiter le framerate
  128. if (timeDelta < 1000 / 60)
  129. return;
  130. this.lastTime = millis();
  131. const steps = [
  132. (this.needUpdate || this.#game.isCompleted) && this.drawGame,
  133. this.#game.isComplete() && this.drawUI
  134. ]
  135. steps.forEach(step => step && step.call(this));
  136. }
  137. drawGame()
  138. {
  139. this.#drawer.draw();
  140. }
  141. drawUI()
  142. {
  143. this.ui.drawWinView();
  144. }
  145. }