UserComponent.module.ts.txt 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. import Phaser from "phaser";
  2. export default class UserComponent {
  3. /**
  4. * @param gameObject The entity.
  5. */
  6. constructor(gameObject: Phaser.GameObjects.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. scene: Phaser.Scene;
  32. protected awake() {
  33. // override this
  34. }
  35. protected start() {
  36. // override this
  37. }
  38. protected update() {
  39. // override this
  40. }
  41. protected destroy() {
  42. // override this
  43. }
  44. }