Object2D.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /************************************************************************
  2. * Object2D.js
  3. ************************************************************************
  4. * Copyright (c) 2021 Pedro Tonini Rosenberg Schneider.
  5. *
  6. * This file is part of Pandora.
  7. *
  8. * Pandora 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. * Pandora 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 Pandora. If not, see <https://www.gnu.org/licenses/>.
  20. *************************************************************************/
  21. class Object2D extends GameObject
  22. {
  23. constructor(name)
  24. {
  25. super(name);
  26. this.position = Vector2.ZERO();
  27. this.rotationDegrees = 0;
  28. this.scale = Vector2.ONE();
  29. this.visible = true;
  30. }
  31. show()
  32. {
  33. this.visible = true;
  34. for (let i = 0; i < this.children.length; i++)
  35. {
  36. if(!this.children[i].show) continue;
  37. this.children[i].show();
  38. }
  39. }
  40. hide()
  41. {
  42. this.visible = false;
  43. for (let i = 0; i < this.children.length; i++)
  44. {
  45. if(!this.children[i].hide) continue;
  46. this.children[i].hide();
  47. }
  48. }
  49. setVisibility(val)
  50. {
  51. this.visible = val;
  52. for (let i = 0; i < this.children.length; i++)
  53. {
  54. if(!this.children[i].setVisibility) continue;
  55. this.children[i].setVisibility(val);
  56. }
  57. }
  58. getVisibility()
  59. {
  60. return this.visible;
  61. }
  62. draw(delta, db)
  63. {
  64. if (!this.visible) return;
  65. db.push();
  66. db.translate(this.position.x, this.position.y);
  67. db.rotate(this.rotationDegrees);
  68. db.scale(this.scale.x, this.scale.y);
  69. this._draw(delta, db);
  70. for (let i = 0; i < this.children.length; i++)
  71. this.children[i].draw(delta, db);
  72. db.pop()
  73. }
  74. }