import * as mc from "@minecraft/server";
import * as utils from "../utils/index";
import { ignoreEntities } from "../shared/constants";
import { PlayParticlesInRaycast } from "../shared/particles";
const durationTicks = 80;
const down = { x: 0, y: -1, z: 0 };
export function enderDragonAbility(player) {
    utils.mclog(`enderDragon`);
    mc.world.playSound('ability.enderdragon.growl', player.getHeadLocation());
    const endTime = mc.system.currentTick + durationTicks;
    utils.waitForLoopEvent(5, async () => {
        const direction = new utils.Vector3({ x: player.getViewDirection().x, y: 0, z: player.getViewDirection().z });
        const origin = new utils.Vector3(player.getHeadLocation()).add(direction.multiply(3));
        const block = player.dimension.getBlockFromRay(origin, down, { includeLiquidBlocks: false, includePassableBlocks: false, maxDistance: 5 });
        let target = new utils.Vector3(player.location).add(direction.multiply(3));
        if (block) {
            target = new utils.Vector3(block.block.location).add(new utils.Vector3(block.faceLocation));
        }
        const particle_direction = target.subtract(player.getHeadLocation()).normalize();
        PlayParticlesInRaycast(new utils.Vector3(player.getHeadLocation()), particle_direction, target.distance(player.getHeadLocation()), 0.5, 'ldz:ender_dragon_ability_stream', 1);
        utils.runCommand(`particle ldz:ender_dragon_ability_fire ${target.toCommandString()}`);
        mc.world.playSound('fire.fire', target, { volume: 2.0 });
        player.dimension.getEntities({ location: target, maxDistance: 4, excludeTypes: ignoreEntities, excludeNames: [player.nameTag], excludeTags: ['friendlyFireOff'] }).forEach(victim => {
            victim.runCommand('summon ldz:igniter ^ ^ ^');
            victim.applyDamage(3, { damagingEntity: player.UnsafeEntity, cause: mc.EntityDamageCause.magic });
        });
        return mc.system.currentTick >= endTime;
    });
}
