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.5KB

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