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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. class Drawer
  2. {
  3. #canvasWidth
  4. #canvasHeight
  5. constructor(_padding)
  6. {
  7. this.padding = _padding;
  8. createCanvas(0, 0);
  9. }
  10. setTubesParams(_tubesNumbers, _tubeLevels, _tubeSize)
  11. {
  12. this.tubesNumbers = _tubesNumbers;
  13. this.tubeLevels = _tubeLevels;
  14. this.tubeSize = _tubeSize;
  15. this.#canvasWidth = this.tubesNumbers * (this.tubeSize + this.padding) + this.padding;
  16. this.#canvasHeight = 2 * this.padding + (this.tubeLevels+1) * this.tubeSize;
  17. resizeCanvas(this.#canvasWidth, this.#canvasHeight);
  18. }
  19. draw(game)
  20. {
  21. let tubes = game.tubes;
  22. let selectedTube = game.selectedTube;
  23. background('white');
  24. this.drawTubes(this.padding, this.padding+this.tubeSize, tubes, selectedTube);
  25. }
  26. drawTubes(x, y, tubes, selectedTube)
  27. {
  28. let tubeX = x;
  29. let tubeY = y;
  30. let selectionOffset = 0;
  31. tubes.forEach(tube => {
  32. selectionOffset = (tube == selectedTube ? this.tubeSize : 0);
  33. this.drawTube(tubeX, tubeY - selectionOffset, tube);
  34. tubeX += this.padding + this.tubeSize;
  35. });
  36. }
  37. drawTube(x, y, tube)
  38. {
  39. y += (this.tubeLevels-1) * this.tubeSize;
  40. for(let i = 0; i < this.tubeLevels; i++)
  41. {
  42. color = tube.getColorAtLevel(i);
  43. color == null ? noFill() : fill(color);
  44. if (i >= 0)
  45. {
  46. rect(x, y - i * this.tubeSize, this.tubeSize);
  47. }
  48. }
  49. }
  50. getTubeIdAt(x, y)
  51. {
  52. if (!this.isInboundsCanvas(x,y)) return -1;
  53. let idWithPadding = (x - this.padding) / (this.tubeSize + this.padding);
  54. return idWithPadding % 1 > 1 - (this.padding / (this.tubeSize + this.padding)) ? -1 : int(idWithPadding);
  55. }
  56. isInboundsCanvas(x, y)
  57. {
  58. return x >= this.padding
  59. && y >= this.padding
  60. && x < this.#canvasWidth - this.padding
  61. && y < this.#canvasHeight - this.padding;
  62. }
  63. }