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

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