UserComponent.js.txt 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. class UserComponent {
  2. /**
  3. * @param {Phaser.GameObjects.GameObject} gameObject The entity.
  4. */
  5. constructor(gameObject) {
  6. this.scene = gameObject.scene;
  7. const listenAwake = this.awake !== UserComponent.prototype.awake;
  8. const listenStart = this.start !== UserComponent.prototype.start;
  9. const listenUpdate = this.update !== UserComponent.prototype.update;
  10. const listenDestroy = this.destroy !== UserComponent.prototype.destroy;
  11. if (listenAwake) {
  12. gameObject.once("components-awake", this.awake, this);
  13. }
  14. if (listenStart) {
  15. this.scene.events.once(Phaser.Scenes.Events.UPDATE, this.start, this);
  16. }
  17. if (listenUpdate) {
  18. this.scene.events.on(Phaser.Scenes.Events.UPDATE, this.update, this);
  19. }
  20. if (listenStart || listenUpdate || listenDestroy) {
  21. gameObject.on(Phaser.GameObjects.Events.DESTROY, () => {
  22. this.scene.events.off(Phaser.Scenes.Events.UPDATE, this.start, this);
  23. this.scene.events.off(Phaser.Scenes.Events.UPDATE, this.update, this);
  24. if (listenDestroy) {
  25. this.destroy();
  26. }
  27. });
  28. }
  29. }
  30. /** @type {Phaser.Scene} */
  31. scene;
  32. awake() {
  33. // override this
  34. }
  35. start() {
  36. // override this
  37. }
  38. update() {
  39. // override this
  40. }
  41. destroy() {
  42. // override this
  43. }
  44. }