C'est le jeu des tubes de couleur dans les pubs la
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

drawer.js 2.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. if(game.isCompleted)
  26. this.drawWinView();
  27. }
  28. drawTubes(x, y, tubes, selectedTube)
  29. {
  30. let tubeX = x;
  31. let tubeY = y;
  32. let selectionOffset = 0;
  33. tubes.forEach(tube => {
  34. selectionOffset = (tube == selectedTube ? this.tubeSize : 0);
  35. this.drawTube(tubeX, tubeY - selectionOffset, tube);
  36. tubeX += this.padding + this.tubeSize;
  37. });
  38. }
  39. drawTube(x, y, tube)
  40. {
  41. y += (this.tubeLevels-1) * this.tubeSize;
  42. for(let i = 0; i < this.tubeLevels; i++)
  43. {
  44. color = tube.getColorAtLevel(i);
  45. color == null ? noFill() : fill(color);
  46. if (i >= 0)
  47. {
  48. rect(x, y - i * this.tubeSize, this.tubeSize);
  49. }
  50. }
  51. }
  52. getTubeIdAt(x, y)
  53. {
  54. if (!this.isInboundsCanvas(x,y)) return -1;
  55. let idWithPadding = (x - this.padding) / (this.tubeSize + this.padding);
  56. return idWithPadding % 1 > 1 - (this.padding / (this.tubeSize + this.padding)) ? -1 : int(idWithPadding);
  57. }
  58. isInboundsCanvas(x, y)
  59. {
  60. return x >= this.padding
  61. && y >= this.padding
  62. && x < this.#canvasWidth - this.padding
  63. && y < this.#canvasHeight - this.padding;
  64. }
  65. drawWinView(){
  66. fill('red');
  67. textSize(50);
  68. let textX = this.#canvasWidth / 2 - 175 + 10 * sin(frameCount/10);
  69. let textY = this.#canvasHeight / 2 + 10 * cos(frameCount/10);
  70. let message = "👑 You Win! 👑"
  71. text(message, textX, textY);
  72. textX ++;
  73. textY ++;
  74. fill('yellow');
  75. text(message, textX, textY);
  76. }
  77. }