import GamePlay from "../Controllers/GamePlay";
import Bubble from "../Tools/Bubble";

const { ccclass, property } = cc._decorator;

@ccclass
export default class Dog extends cc.Component {
  static instance: Dog;
  attacked = false;

  protected onLoad(): void {
    Dog.instance = this;
  }

  onBeginContact(contact, selfCollider, otherCollider) {
    if (GamePlay.instance.isCompleted) return;
    if (!this.attacked && otherCollider.tag == 1) {
      this.attacked = true;
      GamePlay.instance.isGameover = true;
      this.node.getComponent(cc.Animation).play("DogHurt");
    }
    if (otherCollider.tag == 3) {
      let effect = cc.instantiate(GamePlay.instance.fadePrefab);
      effect.parent = this.node;
      effect.position = this.node.convertToNodeSpaceAR(
        this.node.parent.convertToWorldSpaceAR(this.node.position)
      );
      cc.tween(this.node)
        .to(1, { opacity: 0 })
        .call(() => {
          this.node.destroy();
          GamePlay.instance.onReloadLevel();
        })
        .start();
      this.node.getComponent(cc.RigidBody).linearVelocity = cc.Vec2.ZERO;
      this.node.getComponent(cc.Animation).pause();
      this.node.getComponent(cc.RigidBody).fixedRotation = true;
    }
  }
}
