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.

drawer.js 1.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. background('orange');
  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. for(let i = 0; i < tubeLevels; i++)
  39. {
  40. color = tube.getColorAtLevel(i);
  41. color == null ? noFill() : fill(color);
  42. if (i >= 0)
  43. {
  44. rect(x, y - i * this.scale, this.scale);
  45. }
  46. }
  47. }
  48. getTubeIdAt(x, y)
  49. {
  50. if (!this.isInboundsCanvas(x,y)) return -1;
  51. let idWithPadding = (x - this.padding) / (this.scale + this.padding);
  52. return idWithPadding % 1 > 1 - (this.padding / (this.scale + this.padding)) ? -1 : int(idWithPadding);
  53. }
  54. isInboundsCanvas(x, y)
  55. {
  56. return x >= this.padding
  57. && y >= this.padding
  58. && x < this.#canvas.width - this.padding
  59. && y < this.#canvas.height - this.padding;
  60. }
  61. }