GameObject.js 12 KB

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