UserComponent.ts.txt 1.5 KB

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