RebusGame.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /************************************************************************
  2. * RebusGame.js
  3. ************************************************************************
  4. * Copyright (c) 2021 Pedro Tonini Rosenberg Schneider.
  5. *
  6. * This file is part of Alfabetiza.
  7. *
  8. * Alfabetiza is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * Alfabetiza is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with Alfabetiza. If not, see <https://www.gnu.org/licenses/>.
  20. *************************************************************************/
  21. class RebusGame extends Object2D
  22. {
  23. constructor(name)
  24. {
  25. super(name);
  26. /** @type {Object} */
  27. this.levelData = null;
  28. /** @type {Boolean} */
  29. this.gameFinished = false;
  30. /** @type {number} */
  31. this.points = 3;
  32. /** @type {Button} */
  33. this.backButton = null;
  34. /** @type {Button} */
  35. this.continueButton = null;
  36. /** @type {Button} */
  37. this.timer = null;
  38. }
  39. _initSignals()
  40. {
  41. this.addSignal("slected");
  42. }
  43. _setup()
  44. {
  45. var arr = [];
  46. for (let i = 0; i < this.levelData.optionCards.length; i++)
  47. arr.push(i);
  48. arr = shuffle(arr);
  49. for (let i = 0; i < this.levelData.optionCards.length; i++)
  50. {
  51. var j = arr[i];
  52. var newCard = new RebusOptionCard("OptionCard" + j);
  53. AssetHandler.loadTexture(this.levelData.optionCards[j].name, this.levelData.optionCards[j].path);
  54. newCard.thumb = AssetHandler.getTextureByName(this.levelData.optionCards[j].name);
  55. newCard.imgName = this.levelData.optionCards[j].name;
  56. newCard.isAnswer = this.levelData.optionCards[j].answer;
  57. newCard.setPosition((i + 1) * (1920 / 4), 3 * (1080 / 4));
  58. newCard.connect("selected", this, "_onCardSelected");
  59. this.addChild(newCard);
  60. }
  61. for (let i = 0; i < this.levelData.questionCards.length; i++)
  62. {
  63. var newCard = new RebusQuestionCard("OptionCard" + i);
  64. AssetHandler.loadTexture(this.levelData.questionCards[i].name, this.levelData.questionCards[i].path);
  65. newCard.thumb = AssetHandler.getTextureByName(this.levelData.questionCards[i].name);
  66. newCard.imgName = this.levelData.questionCards[i].name;
  67. newCard.setPosition((i + 1) * (1920 / (this.levelData.questionCards.length + 1)), 1080 / 4);
  68. this.addChild(newCard);
  69. }
  70. this.addChild(new RebusGameVisualEffects("GameVisualEffects"));
  71. this.backButton = new Button("BackButton");
  72. this.backButton.setLabel("Voltar");
  73. this.backButton.setFontSize(30);
  74. this.backButton.setPosition(20, 20);
  75. this.backButton.setSize(110, 75);
  76. this.backButton.connect("mouseClicked", this, "_onBackClicked");
  77. this.addChild(this.backButton);
  78. this.continueButton = new Button("ContinueButton");
  79. this.continueButton.setLabel("Continuar");
  80. this.continueButton.setFontSize(40);
  81. this.continueButton.setPosition((1920 - this.continueButton.getSize().x) / 2, 1080 - 450);
  82. this.continueButton.hide();
  83. this.continueButton.connect("mouseClicked", this, "_onContinueClicked");
  84. this.addChild(this.continueButton);
  85. this.timer = new Timer("Timer", 2, false, true);
  86. this.timer.connect("timeout", this, "_onTimerTimeout");
  87. this.addChild(this.timer);
  88. }
  89. _draw( /** @type {number} */ delta, /** @type {p5.Graphics} */ db)
  90. {
  91. background(52);
  92. }
  93. returnToMenu()
  94. {
  95. AssetHandler.clearTextureCache();
  96. GameHandler.addRootObject(new RebusLevelSelector("LevelSelector"));
  97. this.queueFree();
  98. }
  99. _onCardSelected( /** @type {Boolean} */ isAnswer)
  100. {
  101. if (!isAnswer)
  102. this.points--;
  103. else
  104. {
  105. this.gameFinished = true;
  106. this.backButton.hide();
  107. this.timer.start();
  108. for (let i = 0; i < this.children.length; i++)
  109. {
  110. if (this.children[i] instanceof RebusOptionCard)
  111. this.children[i].selectable = false;
  112. }
  113. }
  114. }
  115. _onBackClicked()
  116. {
  117. this.returnToMenu();
  118. }
  119. _onContinueClicked()
  120. {
  121. this.returnToMenu();
  122. }
  123. _onTimerTimeout()
  124. {
  125. this.continueButton.show();
  126. }
  127. }