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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. function setup()
  2. {
  3. this.width = 800
  4. this.height = 600
  5. this.ui = new UI(this.width, this.height);
  6. this.lastTime = 0;
  7. newGame();
  8. }
  9. function newGame() {
  10. const params = gatherMenuValues();
  11. this.game = new Game(params.TUBESNUMBERS, params.TUBESLEVELS);
  12. this.drawer = new Drawer(params.PADDING, this.width, this.height);
  13. this.drawer.setTubesParams(params.TUBESNUMBERS, params.TUBESLEVELS, params.TUBESIZE);
  14. this.needUpdate = true;
  15. }
  16. function mousePressed(e)
  17. {
  18. let tubeId = this.drawer.getTubeIdAt(mouseX, mouseY);
  19. if (tubeId != -1)
  20. {
  21. this.game.selectTube(tubeId);
  22. }
  23. this.needUpdate = true;
  24. }
  25. function gatherMenuValues()
  26. {
  27. let inputTubesNumbers = parseInt(document.getElementById("tbNumber").value);
  28. let inputTubesLevels = parseInt(document.getElementById("tbLevel").value);
  29. let inputTubeSize = parseInt(document.getElementById("tbSize").value);
  30. let inputPadding = parseInt(document.getElementById("padding").value);
  31. return {
  32. TUBESNUMBERS: inputTubesNumbers,
  33. TUBESLEVELS: inputTubesLevels,
  34. TUBESIZE: inputTubeSize,
  35. PADDING: inputPadding
  36. };
  37. }
  38. function draw()
  39. {
  40. const timeDelta = millis() - lastTime;
  41. // peut-être pas nécessaire de limiter le framerate
  42. if (timeDelta < 50)
  43. return;
  44. this.lastTime = millis();
  45. const steps = [
  46. (this.needUpdate || this.game.isCompleted) && drawGame,
  47. this.game.isCompleted && drawUI
  48. ]
  49. steps.forEach(step => step && step());
  50. }
  51. function drawGame()
  52. {
  53. this.drawer.draw(this.game);
  54. this.needUpdate = false;
  55. }
  56. function drawUI()
  57. {
  58. this.ui.drawWinView();
  59. }