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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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.initializeColorTubes(params.TUBESLEVELS - 1);
  16. this.needUpdate = true;
  17. }
  18. gatherMenuValues()
  19. {
  20. let inputTubesNumbers = parseInt(document.getElementById("tbNumber").value);
  21. let inputTubesLevels = parseInt(document.getElementById("tbLevel").value);
  22. let inputScale = parseInt(document.getElementById("scale").value);
  23. let inputPadding = parseInt(document.getElementById("padding").value);
  24. return {
  25. TUBESNUMBERS: inputTubesNumbers,
  26. TUBESLEVELS: inputTubesLevels,
  27. SCALE: inputScale,
  28. PADDING: inputPadding
  29. };
  30. }
  31. initializeColorTubes(LayersPerTube)
  32. {
  33. let nbTubes = this.game.tubesNumber() - 1;
  34. let allColors = [];
  35. for (let i = 0; i < nbTubes; i++)
  36. {
  37. let coloridx = i % this.#colorsBank.length;
  38. for (let times = 0; times < LayersPerTube; times++)
  39. {
  40. allColors.push(this.#colorsBank[coloridx]);
  41. }
  42. }
  43. for (let index = allColors.length - 1; index >= 0; index--) {
  44. let randomIndex = Math.floor(Math.random() * (index + 1));
  45. let tubeIndex = Math.floor(index / LayersPerTube);
  46. this.game.tubeAt(tubeIndex).addColorLayer(allColors[randomIndex]);
  47. allColors[randomIndex] = allColors[index];
  48. }
  49. }
  50. clickTube(tubeIndex)
  51. {
  52. let _clickedTube = this.game.tubeAt(tubeIndex);
  53. if (_clickedTube == null) return;
  54. if (_clickedTube == this.game.selectedTube())
  55. this.game.unselectTube();
  56. else if (this.canPourInto(this.game.selectedTube(), _clickedTube))
  57. {
  58. this.pourColorInto(this.game.selectedTube(), _clickedTube);
  59. this.game.unselectTube();
  60. }
  61. else if (!_clickedTube.isEmpty())
  62. this.game.selectTubeAt(tubeIndex);
  63. }
  64. canPourInto(sourceTube, targetTube)
  65. {
  66. return sourceTube != null && targetTube != null
  67. && sourceTube != targetTube
  68. && (targetTube.isEmpty() || sourceTube.peekTopColor() == targetTube.peekTopColor())
  69. && !targetTube.isFull();
  70. }
  71. pourColorInto(sourceTube, targetTube)
  72. {
  73. while (this.canPourInto(sourceTube, targetTube))
  74. {
  75. targetTube.addColorLayer(sourceTube.removeColorLayer());
  76. }
  77. this.checkTubeCompletion(sourceTube);
  78. this.checkTubeCompletion(targetTube);
  79. }
  80. checkTubeCompletion(tube)
  81. {
  82. if (tube.isComplete())
  83. {
  84. this.game.removeTube(tube);
  85. this.checkGameCompletion();
  86. }
  87. }
  88. checkGameCompletion()
  89. {
  90. if (this.game.tubesNumber() == 1 && this.game.tubeAt(0).isEmpty())
  91. {
  92. this.game.clean();
  93. }
  94. }
  95. mousePressed(e)
  96. {
  97. let clickedTubeId = this.drawer.getTubeIdAt(mouseX, mouseY);
  98. this.clickTube(clickedTubeId);
  99. this.needUpdate = true;
  100. }
  101. runLoop()
  102. {
  103. if (!this.needUpdate) return;
  104. this.drawer.draw(this.game);
  105. this.needUpdate = false;
  106. }
  107. }