GameObject.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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.isRoot = false;
  32. this.signals = [];
  33. this.initSignals();
  34. GameHandler.instanceGameObject(this);
  35. }
  36. // Getters
  37. getChildren()
  38. {
  39. return this.children;
  40. }
  41. getChildByIndex(idx)
  42. {
  43. if (idx >= 0 && idx < this.children.length)
  44. return this.children[idx];
  45. return null;
  46. }
  47. getChildByName(name)
  48. {
  49. for (let i = 0; i < this.children.length; i++)
  50. if (this.children[i].name == name) return this.children[i];
  51. return null;
  52. }
  53. getParent()
  54. {
  55. if (!this.parented) return null;
  56. return this.parent;
  57. }
  58. // Methods
  59. addSignal(name)
  60. {
  61. this.signals.push(new Signal(name));
  62. }
  63. connect(signalName, target, callback)
  64. {
  65. for (let i = 0; i < this.signals.length; i++)
  66. {
  67. if (this.signals[i].name == signalName)
  68. {
  69. this.signals[i].targets.push(target);
  70. this.signals[i].callbacks.push(callback);
  71. return;
  72. }
  73. }
  74. }
  75. emitSignal(signalName, ...params)
  76. {
  77. for (let i = 0; i < this.signals.length; i++)
  78. {
  79. if (this.signals[i].name == signalName)
  80. {
  81. for (let j = 0; j < this.signals[i].callbacks.length; j++)
  82. this.signals[i].targets[j][this.signals[i].callbacks[j]](...params);
  83. return;
  84. }
  85. }
  86. }
  87. addChild(child)
  88. {
  89. child.parent = this;
  90. child.parented = true;
  91. this.children.push(child);
  92. if (this.isOnTree) child.setup();
  93. }
  94. removeChildById(id)
  95. {
  96. for (let i = 0; i < this.children.length; i++)
  97. {
  98. if (this.children[i].id == id)
  99. {
  100. this.children.splice(i, 1);
  101. return;
  102. }
  103. }
  104. }
  105. free()
  106. {
  107. if (this.parented)
  108. this.getParent().removeChildById(this.id);
  109. else if (this.isRoot)
  110. GameHandler.removeRootObjectById(this.id);
  111. this.destroy();
  112. }
  113. destroy()
  114. {
  115. for (let i = 0; i < this.children.length; i++)
  116. this.children[i].destroy();
  117. for (var prop in this)
  118. this[prop] = null;
  119. }
  120. initSignals()
  121. {
  122. this._initSignals();
  123. }
  124. setup()
  125. {
  126. this.isOnTree = true;
  127. this._setup();
  128. for (let i = 0; i < this.children.length; i++)
  129. {
  130. this.children[i].setup();
  131. }
  132. }
  133. update(delta)
  134. {
  135. this._update(delta);
  136. for (let i = 0; i < this.children.length; i++)
  137. this.children[i].update(delta);
  138. }
  139. draw(delta, db)
  140. {
  141. this._draw(delta, db);
  142. for (let i = 0; i < this.children.length; i++)
  143. this.children[i].draw(delta, db);
  144. }
  145. // Callbacks
  146. _initSignals()
  147. {
  148. }
  149. _setup()
  150. {
  151. }
  152. _update(delta)
  153. {
  154. }
  155. _draw(delta, db)
  156. {
  157. }
  158. }