import { Physics } from 'phaser';

export class Rocket extends Physics.Arcade.Sprite {
  constructor(scene, x, y) {
    super(scene, x, y, 'rocket');
    scene.add.existing(this);
    scene.physics.add.existing(this, true);

    this.setScale(0.45);
    this.body.setSize(this.width * 0.5, this.height * 0.7);
    this.body.setOffset(this.width * 0.25, this.height * 0.15);
    this.consumed = false;

    this.floatTween = scene.tweens.add({
      targets: this,
      y: y - 8,
      duration: 600,
      yoyo: true,
      repeat: -1,
      ease: 'Sine.easeInOut',
    });
  }

  onPlayerTouch(player) {
    if (this.consumed) return false;
    this.consumed = true;
    this.body.enable = false;
    this.setVisible(false);
    player.startRocket();
    this.destroy();
    return true;
  }

  destroy(fromScene) {
    if (this.floatTween) {
      this.floatTween.stop();
      this.floatTween = null;
    }
    super.destroy(fromScene);
  }
}
