GameObject.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /************************************************************************
  2. * GameObject.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 GameObject
  22. {
  23. constructor(name)
  24. {
  25. this.id = 0;
  26. this.name = name;
  27. this.children = [];
  28. this.parented = false;
  29. this.parent = null;
  30. this.isOnTree = false;
  31. this.signals = [];
  32. this.initSignals();
  33. GameHandler.instanceGameObject(this);
  34. }
  35. // Getters
  36. getChildren()
  37. {
  38. return this.children;
  39. }
  40. getChildByIndex(idx)
  41. {
  42. if (idx >= 0 && idx < this.children.length)
  43. return this.children[idx];
  44. return null;
  45. }
  46. getChildByName(name)
  47. {
  48. for (let i = 0; i < this.children.length; i++)
  49. if (this.children[i].name == name) return this.children[i];
  50. return null;
  51. }
  52. getParent()
  53. {
  54. if (!this.parented) return null;
  55. return this.parent;
  56. }
  57. // Methods
  58. addSignal(name)
  59. {
  60. this.signals.push(new Signal(name));
  61. }
  62. connect(signalName, target, callback)
  63. {
  64. for (let i = 0; i < this.signals.length; i++)
  65. {
  66. if (this.signals[i].name == signalName)
  67. {
  68. this.signals[i].targets.push(target);
  69. this.signals[i].callbacks.push(callback);
  70. return;
  71. }
  72. }
  73. }
  74. emitSignal(signalName, ...params)
  75. {
  76. for (let i = 0; i < this.signals.length; i++)
  77. {
  78. if (this.signals[i].name == signalName)
  79. {
  80. for (let j = 0; j < this.signals[i].callbacks.length; j++)
  81. this.signals[i].targets[j][this.signals[i].callbacks[j]](...params);
  82. return;
  83. }
  84. }
  85. }
  86. addChild(child)
  87. {
  88. child.parent = this;
  89. child.parented = true;
  90. this.children.push(child);
  91. if (this.isOnTree) child.setup();
  92. }
  93. initSignals()
  94. {
  95. this._initSignals();
  96. }
  97. setup()
  98. {
  99. this.isOnTree = true;
  100. this._setup();
  101. for (let i = 0; i < this.children.length; i++)
  102. {
  103. this.children[i].setup();
  104. }
  105. }
  106. update(delta)
  107. {
  108. this._update(delta);
  109. for (let i = 0; i < this.children.length; i++)
  110. this.children[i].update(delta);
  111. }
  112. draw(delta, db)
  113. {
  114. this._draw(delta, db);
  115. for (let i = 0; i < this.children.length; i++)
  116. this.children[i].draw(delta, db);
  117. }
  118. // Callbacks
  119. _initSignals()
  120. {
  121. }
  122. _setup()
  123. {
  124. }
  125. _update(delta)
  126. {
  127. }
  128. _draw(delta, db)
  129. {
  130. }
  131. }