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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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.width = 800
  16. this.height = 600
  17. this.ui = new UI(this.width, this.height);
  18. this.lastTime = 0;
  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. while (this.canPourInto(sourceTube, targetTube))
  78. {
  79. targetTube.addColorLayer(sourceTube.removeColorLayer());
  80. }
  81. this.checkTubeCompletion(sourceTube);
  82. this.checkTubeCompletion(targetTube);
  83. }
  84. checkTubeCompletion(tube)
  85. {
  86. if (tube.isComplete())
  87. {
  88. this.game.removeTube(tube);
  89. this.checkGameCompletion();
  90. }
  91. }
  92. checkGameCompletion()
  93. {
  94. if (this.game.tubesNumber() == 1 && this.game.tubeAt(0).isEmpty())
  95. {
  96. this.game.clean();
  97. }
  98. }
  99. mousePressed(e)
  100. {
  101. let clickedTubeId = this.drawer.getTubeIdAt(mouseX, mouseY);
  102. this.clickTube(clickedTubeId);
  103. this.needUpdate = true;
  104. }
  105. runLoop()
  106. {
  107. const timeDelta = millis() - lastTime;
  108. // peut-être pas nécessaire de limiter le framerate
  109. if (timeDelta < 50)
  110. return;
  111. this.lastTime = millis();
  112. const steps = [
  113. (this.needUpdate || this.game.isCompleted) && drawGame,
  114. this.game.isCompleted && drawUI
  115. ]
  116. steps.forEach(step => step && step());
  117. }
  118. function drawUI()
  119. {
  120. this.ui.drawWinView();
  121. }
  122. }