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 3.7KB

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