GameObject.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. class GameObject
  2. {
  3. constructor(name)
  4. {
  5. this.id = 0;
  6. this.name = name;
  7. this.children = [];
  8. this.parented = false;
  9. this.parent = null;
  10. this.isOnTree = false;
  11. GameHandler.instanceGameObject(this);
  12. }
  13. // Getters
  14. getChildren()
  15. {
  16. return this.children;
  17. }
  18. getChildByIndex(idx)
  19. {
  20. if (idx >= 0 && idx < this.children.length)
  21. return this.children[idx];
  22. return null;
  23. }
  24. getChildByName(name)
  25. {
  26. for (let i = 0; i < this.children.length; i++)
  27. if (this.children[i].name == name) return this.children[i];
  28. return null;
  29. }
  30. getParent()
  31. {
  32. if (!this.parented) return null;
  33. return this.parent;
  34. }
  35. // Methods
  36. addChild(child)
  37. {
  38. child.parent = this;
  39. child.parented = true;
  40. this.children.push(child);
  41. if (this.isOnTree) child.setup();
  42. }
  43. setup()
  44. {
  45. this.isOnTree = true;
  46. this._setup();
  47. for (let i = 0; i < this.children.length; i++)
  48. {
  49. this.children[i].setup();
  50. }
  51. }
  52. update(delta)
  53. {
  54. this._update(delta);
  55. for (let i = 0; i < this.children.length; i++)
  56. this.children[i].update(delta);
  57. }
  58. draw(delta)
  59. {
  60. this._draw(delta);
  61. for (let i = 0; i < this.children.length; i++)
  62. this.children[i].draw(delta);
  63. }
  64. // Callbacks
  65. _setup()
  66. {
  67. }
  68. _update(delta)
  69. {
  70. }
  71. _draw(delta)
  72. {
  73. }
  74. }