import * as mc from "@minecraft/server";
import { ComponentCompanion } from "./base";
export class CompanionShootDragonComponent extends ComponentCompanion {
    constructor() {
        super(...arguments);
        this.projectileBinding = this.onProjectileHit.bind(this);
    }
    async onComponentAdded(npc) {
        super.onComponentAdded(npc);
        mc.world.afterEvents.projectileHitEntity.subscribe(this.projectileBinding);
    }
    async onComponentRemoved(npc) {
        mc.world.afterEvents.projectileHitEntity.unsubscribe(this.projectileBinding);
    }
    onProjectileHit(event) {
        const hitEntity = event.getEntityHit().entity;
        if (this.npc && hitEntity && hitEntity.typeId === 'minecraft:ender_dragon') {
            hitEntity.applyDamage(10, { cause: mc.EntityDamageCause.entityAttack, damagingEntity: this.npc.Owner.UnsafeEntity });
            if (event.projectile.isValid()) {
                event.projectile.remove();
            }
        }
    }
}
