C'est le jeu des tubes de couleur dans les pubs la
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

drawer.js 2.0KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. class Drawer
  2. {
  3. #canvas;
  4. constructor(_padding, _scale)
  5. {
  6. this.padding = _padding;
  7. this.scale = _scale;
  8. this.#canvas = createCanvas(0, 0);
  9. }
  10. computeGameParams(_tubesNumbers, _tubeLevels)
  11. {
  12. let canvasWidth = _tubesNumbers * (this.scale + this.padding) + this.padding;
  13. let canvasHeight = 2 * this.padding + (_tubeLevels+1) * this.scale;
  14. resizeCanvas(canvasWidth, canvasHeight);
  15. }
  16. draw(game)
  17. {
  18. clear();
  19. this.drawTubes(this.padding, this.padding+this.scale, game);
  20. }
  21. drawTubes(x, y, game)
  22. {
  23. let tubeX = x;
  24. let tubeY = y;
  25. let selectionOffset = 0;
  26. for (let i = 0; i < game.tubesNumber(); i++)
  27. {
  28. let tube = game.tubeAt(i);
  29. selectionOffset = (tube == game.selectedTube() ? this.scale : 0);
  30. this.drawTube(tubeX, tubeY - selectionOffset, tube);
  31. tubeX += this.padding + this.scale;
  32. }
  33. }
  34. drawTube(x, y, tube)
  35. {
  36. let tubeLevels = tube.height();
  37. y += (tubeLevels-1) * this.scale;
  38. strokeWeight(2);
  39. for(let i = 0; i < tubeLevels; i++)
  40. {
  41. let layerY = y - i * this.scale;
  42. let color = tube.getColorAtLevel(i);
  43. color == null ? noFill() : fill(color);
  44. if (i > 0)
  45. {
  46. noStroke();
  47. rect(x, layerY, this.scale);
  48. stroke('black');
  49. line(x, layerY, x, layerY + this.scale);
  50. line(x + this.scale, layerY, x + this.scale, layerY + this.scale);
  51. }
  52. else
  53. {
  54. noStroke();
  55. rect(x, layerY, this.scale, this.scale/2);
  56. stroke('black');
  57. line(x, layerY, x, layerY + this.scale/2);
  58. line(x + this.scale, layerY, x + this.scale, layerY + this.scale/2);
  59. arc(x + this.scale/2, layerY + this.scale/2, this.scale, this.scale, 0, PI);
  60. }
  61. }
  62. }
  63. getTubeIdAt(x, y)
  64. {
  65. if (!this.isInboundsCanvas(x,y)) return -1;
  66. let idWithPadding = (x - this.padding) / (this.scale + this.padding);
  67. return idWithPadding % 1 > 1 - (this.padding / (this.scale + this.padding)) ? -1 : int(idWithPadding);
  68. }
  69. isInboundsCanvas(x, y)
  70. {
  71. return x >= this.padding
  72. && y >= this.padding
  73. && x < this.#canvas.width - this.padding
  74. && y < this.#canvas.height - this.padding;
  75. }
  76. }