import * as utils from "../utils/index";
import { HeartAbilityManager } from "../systems/index";
const UNBREAKABLE_BLOCKS = [
    "minecraft:bedrock",
    "minecraft:barrier",
    "minecraft:end_portal",
    "minecraft:end_portal_frame",
];
export async function goatAbility(player) {
    utils.mclog(`goat`);
    HeartAbilityManager.addInvulnerability(player, "noFallDamage");
    await utils.waitForTicks(0);
    const direction = utils.Vector3.SOUTH.rotateYaw(-player.getRotation().y);
    player.applyKnockback(direction.x, direction.z, 5.0, 0.25);
    player.runCommand('stopsound @a[r=20] ability.goat.charge');
    player.playSoundAt("ability.goat.charge");
    player.playSoundAt("ability.goat.dash");
    await utils.waitForTicks(1);
    do {
        const length = Math.max(player.getVelocity().length() * 2.0, 1.5);
        tryImpact(player, player.location.up(0.25), direction, length);
        tryImpact(player, player.getHeadLocation(), direction, length);
        await utils.waitForTicks(0);
    } while (player.getVelocity().length() > 1.0);
    HeartAbilityManager.removeInvulnerability(player, "noFallDamage");
}
function tryImpact(player, origin, direction, length) {
    const entityHits = utils.getEntitiesFromRaySafe(player.dimension, origin, direction, { excludeNames: [player.nameTag], maxDistance: length });
    const blockHit = player.dimension.getBlockFromRay(origin, direction, { maxDistance: length, includePassableBlocks: false });
    for (const hit of entityHits) {
        try {
            hit.entity.playSoundAt("ability.goat.impact");
            hit.entity.applyKnockback(direction.x, direction.z, 10.0, 2.0);
        }
        catch (error) {
            // Do nothing if applyKnockback fails
        }
    }
    if (blockHit && !UNBREAKABLE_BLOCKS.includes(blockHit.block.typeId)) {
        player.runCommand(`setblock ${new utils.Vector3(blockHit.block.location)} minecraft:air destroy`);
    }
}
