GameObject.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  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. /**
  22. * The {@code GameObject} class represents a minimal structure for any object in the
  23. * game. All objects added to the tree, either as a root or a child of an object
  24. * on the tree must be or inherit from GameObject.
  25. *
  26. * ! All GameObjects need to be inside the tree to do anything (can be added as a child
  27. * ! of another GameObject on the tree or as a root).
  28. *
  29. * @author Pedro Schneider
  30. *
  31. * @class
  32. */
  33. class GameObject
  34. {
  35. /**
  36. * @constructor
  37. * Creates an empty GameObject, with default properties.
  38. *
  39. * @param {String} name name of the new GameObject.
  40. */
  41. constructor(name)
  42. {
  43. this.id = 0; // Global unique indentifier for the object.
  44. this.name = name; // Set the name for the object.
  45. this.children = []; // List of children.
  46. this.parented = false; // Does this GameObject have a parent?
  47. this.parent = null; // Who is the parent? null if orphan.
  48. this.isOnTree = false; // Is this GameObject on the tree?
  49. this.isRoot = false; // Is this GameObject a root object?
  50. this.signals = []; // List of signals.
  51. this.initSignals();
  52. GameHandler.instanceGameObject(this);
  53. }
  54. /**
  55. * Returns the list of children of this GameObject.
  56. * @returns {Array} array containing a reference to all of this GameObject's
  57. * children.
  58. */
  59. getChildren()
  60. {
  61. return this.children;
  62. }
  63. /**
  64. * Query for a child of this GameObject with the child's index.
  65. *
  66. * ! The index refers to the order you added the children to this
  67. * ! GameObject, starting at 0.
  68. *
  69. * @param {String} idx index of the desired child on the GameObject's
  70. * children list.
  71. *
  72. * @returns {GameObject} a reference to the child with the given index, or null if
  73. * no child has that index.
  74. */
  75. getChildByIndex(idx)
  76. {
  77. if (idx >= 0 && idx < this.children.length)
  78. return this.children[idx];
  79. return null;
  80. }
  81. /**
  82. * Query for a child of this GameObject with the child's id.
  83. *
  84. * @param {String} id id of the desired child on the GameObject's
  85. * children list.
  86. *
  87. * @returns {GameObject} a reference to the child with the given id, or null if
  88. * no child has that id.
  89. */
  90. getChildById(id)
  91. {
  92. for (let i = 0; i < this.children.length; i++)
  93. {
  94. if (this.children[i].id == id)
  95. return this.children[i];
  96. }
  97. return null;
  98. }
  99. /**
  100. * Query for a child of this GameObject with the child's name.
  101. *
  102. * @param {String} name name of the desired child.
  103. *
  104. * @returns {GameObject} a reference to the child with the given name, or null
  105. * if no child has that name.
  106. */
  107. getChildByName(name)
  108. {
  109. for (let i = 0; i < this.children.length; i++)
  110. if (this.children[i].name == name) return this.children[i];
  111. return null;
  112. }
  113. /**
  114. * Get a reference to this GameObject's parent.
  115. *
  116. * @returns {GameObject} a reference to this GameObject's parent if it
  117. * exists, null if it doesn't.
  118. */
  119. getParent()
  120. {
  121. if (!this.parented) return null;
  122. return this.parent;
  123. }
  124. /**
  125. * Add a new signal to this GameObject.
  126. *
  127. * @param {String} name name for the new signal.
  128. */
  129. addSignal(name)
  130. {
  131. this.signals.push(new Signal(name));
  132. }
  133. /**
  134. * Connect another GameObject to one of this GameObject's signals.
  135. *
  136. * @param {String} signalName name of the signal to be connected.
  137. * @param {GameObject} target reference to the GameObject that wants
  138. * to be connected to this signal.
  139. * @param {String} callback name of the method to be called every
  140. * time this signal is emited.
  141. */
  142. connect(signalName, target, callback)
  143. {
  144. for (let i = 0; i < this.signals.length; i++)
  145. {
  146. if (this.signals[i].name == signalName)
  147. {
  148. this.signals[i].targets.push(target);
  149. this.signals[i].callbacks.push(callback);
  150. return;
  151. }
  152. }
  153. }
  154. /**
  155. * Emits one of this GameObject's signals.
  156. *
  157. * @param {String} signalName name of the signal to be emited.
  158. * @param {...any} params parameters the connected callbacks
  159. * should receive.
  160. */
  161. emitSignal(signalName, ...params)
  162. {
  163. for (let i = 0; i < this.signals.length; i++)
  164. {
  165. if (this.signals[i].name == signalName)
  166. {
  167. for (let j = 0; j < this.signals[i].callbacks.length; j++)
  168. this.signals[i].targets[j][this.signals[i].callbacks[j]](...params);
  169. return;
  170. }
  171. }
  172. }
  173. /**
  174. * Add the GameObject as a child of this GameObject.
  175. *
  176. * @param {GameObject} child reference to the GameObject to be
  177. * added as a child.
  178. */
  179. addChild(child)
  180. {
  181. child.parent = this;
  182. child.parented = true;
  183. this.children.push(child);
  184. if (this.isOnTree) child.setup();
  185. }
  186. /**
  187. * Remove a child of this GameObject by its index. This action does not
  188. * delete the child GameObject; if this is the functionality you want,
  189. * freeing a game object from memory automatically removes it from its
  190. * parent.
  191. *
  192. * ! The index refers to the order you added the children to this
  193. * ! GameObject, starting at 0.
  194. *
  195. * @param {number} idx index of the child to be removed.
  196. */
  197. removeChildByIndex(idx)
  198. {
  199. if (idx >= 0 && idx < this.children.length)
  200. this.children.splice(idx, 1);
  201. }
  202. /**
  203. * Remove a child of this GameObject by its id. This action does not
  204. * delete the child GameObject; if this is the functionality you want,
  205. * freeing a game object from memory automatically removes it from its
  206. * parent.
  207. *
  208. * @param {number} id id of the child to be removed.
  209. */
  210. removeChildById(id)
  211. {
  212. for (let i = 0; i < this.children.length; i++)
  213. {
  214. if (this.children[i].id == id)
  215. {
  216. this.children.splice(i, 1);
  217. return;
  218. }
  219. }
  220. }
  221. /**
  222. * Remove a child of this GameObject by its name. This action does not
  223. * delete the child GameObject; if this is the functionality you want,
  224. * freeing a game object from memory automatically removes it from its
  225. * parent.
  226. *
  227. * @param {String} name name of the child to be removed.
  228. */
  229. removeChildByName(name)
  230. {
  231. for (let i = 0; i < this.children.length; i++)
  232. {
  233. if (this.children[i].name == name)
  234. {
  235. this.children.splice(i, 1);
  236. return;
  237. }
  238. }
  239. }
  240. /**
  241. * De-parents the GameObject from its parent, or removes it from the root of the
  242. * tree if orphan, and recursively marks this GameObject's and all of its children's
  243. * memory for garbage collection.
  244. */
  245. free()
  246. {
  247. if (this.parented)
  248. this.getParent().removeChildById(this.id);
  249. else if (this.isRoot)
  250. GameHandler.removeRootObjectById(this.id);
  251. this.destroy();
  252. }
  253. /**
  254. * Recursively marks this GameObject's and all of its children's
  255. * memory for garbage collection.
  256. */
  257. destroy()
  258. {
  259. for (let i = 0; i < this.children.length; i++)
  260. this.children[i].destroy();
  261. for (var prop in this)
  262. this[prop] = null;
  263. }
  264. /**
  265. * Caller for the _initSignals() callback
  266. */
  267. initSignals()
  268. {
  269. this._initSignals();
  270. }
  271. /**
  272. * Caller for the _setup() callback. Recursively calls itself for all
  273. * of this GameObject's children.
  274. */
  275. setup()
  276. {
  277. this.isOnTree = true;
  278. this._setup();
  279. for (let i = 0; i < this.children.length; i++)
  280. {
  281. this.children[i].setup();
  282. }
  283. }
  284. /**
  285. * Caller for the _update(delta) callback. Recrusively calls itself for
  286. * all of this GameOject's children.
  287. *
  288. * @param {number} delta ellapsed seconds since the last frame.
  289. */
  290. update(delta)
  291. {
  292. this._update(delta);
  293. for (let i = 0; i < this.children.length; i++)
  294. this.children[i].update(delta);
  295. }
  296. /**
  297. * Caller for the _draw(delta, db) callback. Recursively calls itself for
  298. * all of this GameObject's children.
  299. *
  300. * @param {number} delta ellapsed seconds since the last frame.
  301. * @param {p5.Graphics} db secondary buffer to draw on.
  302. */
  303. draw(delta, db)
  304. {
  305. this._draw(delta, db);
  306. for (let i = 0; i < this.children.length; i++)
  307. this.children[i].draw(delta, db);
  308. }
  309. /**
  310. * @callback
  311. * ! This function should be overriden, it provides no default functionality.
  312. * This function is called once when the GameObject is created and should declare
  313. * any and all signals the user wants for the GameObject.
  314. */
  315. _initSignals()
  316. {
  317. }
  318. /**
  319. * @callback
  320. * ! This function should be overriden, it provides no default functionality.
  321. * This function is called once when the GameObject is added to the tree (as a child
  322. * of another GameObject or as a root).
  323. */
  324. _setup()
  325. {
  326. }
  327. /**
  328. * @callback
  329. * ! This function should be overriden, it provides no default functionality.
  330. * This function is called once at the start of every frame and should be used for
  331. * any logic that doesn't have anything to do with drawing graphics to the screen buffer.
  332. *
  333. * @param {number} delta ellapsed seconds since the last frame.
  334. */
  335. _update(delta)
  336. {
  337. }
  338. /**
  339. * @callback
  340. * ! This function should be overriden, it provides no default functionality.
  341. * This function is called once at the start of every frame after all update() calls
  342. * have been completed and should be used for any logic that results in something
  343. * beeing drawn to the screen buffer.
  344. *
  345. * @param {number} delta ellapsed seconds since the last frame.
  346. * @param {p5.Graphics} db secondary buffer to draw on.
  347. */
  348. _draw(delta, db)
  349. {
  350. }
  351. }