import * as mc from "@minecraft/server";
import * as utils from "../utils/index";
import { ignoreEntities } from "../shared/constants";
import { HeartAbilityManager } from "../systems/index";
const range = 10;
const maxDamage = 11;
export async function slimeAbility(player) {
    utils.mclog(`slime`);
    mc.world.playSound('ability.slime.boing', player.location);
    mc.world.playSound('mob.slime.jump', player.location);
    HeartAbilityManager.addInvulnerability(player, 'noFallDamage');
    player.applyKnockback(0, 0, 0, 1.5);
    await utils.waitForEvent(8, () => { });
    await utils.waitForLoopEvent(0, () => {
        player.applyKnockback(0, 0, 0, -2);
        return player.hasTag('onGround');
    });
    HeartAbilityManager.removeInvulnerability(player, 'noFallDamage');
    // Do FX
    utils.runCommand(`particle ldz:ground_slam ~ ~ ~`, player);
    mc.world.playSound('mob.slime.big', player.location);
    mc.world.playSound('ability.slime.slam', player.location);
    // Do Damage
    const origin = new utils.Vector3(player.location);
    player.dimension.getEntities({ location: player.location, maxDistance: range, excludeTypes: ignoreEntities, excludeNames: [player.nameTag], excludeTags: ['friendlyFireOff'] }).forEach(victim => {
        const direction = new utils.Vector3(victim.location).subtract(origin).normalize();
        const distance = origin.distance(new utils.Vector3(victim.location));
        utils.waitForEvent(distance, () => {
            if (victim.isValid()) {
                victim.applyKnockback(direction.x, direction.z, maxDamage - distance, maxDamage - distance);
                victim.applyDamage(maxDamage - distance, { damagingEntity: player.UnsafeEntity, cause: mc.EntityDamageCause.entityExplosion });
            }
        });
    });
}
