UserComponent.module.js.txt 1.5 KB

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