GameObject.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. GameHandler.instanceGameObject(this);
  11. }
  12. // Getters
  13. getChildren()
  14. {
  15. return this.children;
  16. }
  17. getChildByIndex(idx)
  18. {
  19. if (idx >= 0 && idx < this.children.length)
  20. return this.children[idx];
  21. return null;
  22. }
  23. getChildByName(name)
  24. {
  25. for (let i = 0; i < this.children.length; i++)
  26. if (this.children[i].name == name) return this.children[i];
  27. return null;
  28. }
  29. getParent()
  30. {
  31. if (!this.parented) return null;
  32. return this.parent;
  33. }
  34. // Methods
  35. addChild(child)
  36. {
  37. child.parent = this;
  38. child.parented = true;
  39. this.children.push(child);
  40. }
  41. update(delta)
  42. {
  43. this._update(delta);
  44. for (let i = 0; i < this.children.length; i++)
  45. this.children[i].update(delta);
  46. }
  47. draw(delta)
  48. {
  49. this._draw(delta);
  50. for (let i = 0; i < this.children.length; i++)
  51. this.children[i].draw(delta);
  52. }
  53. // Callbacks
  54. _update(delta)
  55. {
  56. }
  57. _draw(delta)
  58. {
  59. }
  60. }