import { system, EntityDamageCause } from "@minecraft/server";
import { lastKickAttackTime, playSoundEffect, spawnParticleEffect } from "./Attacking";
export function kickAttack(sourceEntity, log, config) {
    try {
        const currentTime = Date.now();
        const lastTime = lastKickAttackTime[sourceEntity.id] || 0;
        const cooldown = 1600;
        if (currentTime - lastTime < cooldown) {
            return;
        }
        const damageConfig = config.pegasus;
        lastKickAttackTime[sourceEntity.id] = currentTime;
        const { x: x1, y: y1, z: z1 } = sourceEntity.location;
        const { x: vX, y: vY, z: vZ } = sourceEntity.getViewDirection();
        if (damageConfig) {
            const totalSteps = damageConfig.knockbackDuration / damageConfig.knockbackInterval;
            if (sourceEntity &&
                (sourceEntity.typeId === "kicu_mmad:pegasus" || sourceEntity.typeId === "kicu_mmad:spectral_pegasus")) {
                const baseParticles = [
                    "kicu_mmad:pegasus_base_particle",
                    "kicu_mmad:pegasus_ring1_particle",
                    "kicu_mmad:pegasus_ring2_particle",
                ];
                const airburstParticle = sourceEntity.typeId === "kicu_mmad:pegasus"
                    ? "kicu_mmad:pegasus_airburst_particle"
                    : "kicu_mmad:spectral_pegasus_airburst_particle";
                system.run(() => {
                    [...baseParticles, airburstParticle].forEach((particle, index) => {
                        system.runTimeout(() => sourceEntity.dimension.spawnParticle(particle, sourceEntity.location), index);
                    });
                });
            }
            const invertedDirection = {
                x: -vX,
                y: vY,
                z: -vZ,
            };
            const entities = getEntitiesInCylinder(sourceEntity, { x: x1, y: y1, z: z1 }, { height: config.skillAttackHeight, width: config.skillAttackRadius });
            entities.forEach((entity) => {
                if (entity.id !== sourceEntity.id && entity.isValid() && entity.typeId !== "minecraft:player") {
                    entity.addEffect("levitation", damageConfig.knockbackDuration, { showParticles: true, amplifier: 1 });
                    entity.applyDamage(config.skillDamage, { cause: EntityDamageCause.blockExplosion });
                    spawnParticleEffect(sourceEntity, entity.location);
                    applyKnockbackAndLevitationCycle(sourceEntity, entity, invertedDirection, config, false, totalSteps, 0);
                }
            });
            spawnParticleEffect(sourceEntity, { x: x1, y: y1, z: z1 });
            playSoundEffect(sourceEntity, { x: x1, y: y1, z: z1 });
        }
    }
    catch { }
}
function getEntitiesInCylinder(sourceEntity, center, radius) {
    const { height, width } = radius;
    const halfHeight = height / 2;
    const boundingRadius = Math.sqrt(width * width + halfHeight * halfHeight);
    const potentialEntities1 = sourceEntity.dimension.getEntities({
        location: center,
        maxDistance: boundingRadius,
        families: ["monster"],
    });
    const potentialEntities2 = sourceEntity.dimension.getEntities({
        location: center,
        maxDistance: boundingRadius,
        families: ["hostile"],
    });
    const potentialEntities3 = sourceEntity.dimension.getEntities({
        location: center,
        maxDistance: boundingRadius,
        families: ["kicu_mmad_wild_mount"],
    });
    const potentialEntities = [...potentialEntities1, ...potentialEntities2, ...potentialEntities3];
    const entities = [];
    for (let i = 0, len = potentialEntities.length; i < len; i++) {
        const ent = potentialEntities[i];
        const loc = ent.location;
        const dx = loc.x - center.x;
        const dz = loc.z - center.z;
        if (dx * dx + dz * dz > width * width)
            continue;
        if (loc.y < center.y - halfHeight || loc.y > center.y + halfHeight)
            continue;
        entities.push(ent);
    }
    return entities;
}
function applyKnockbackAndLevitationCycle(sourceEntity, target, direction, config, isPlayer, totalSteps, step) {
    if (step > totalSteps || !target.isValid())
        return;
    if (config && config.pegasus) {
        const spiralRadius = config.velocity * (step / totalSteps);
        const angle = step * ((config.pegasus?.rotations * 2 * Math.PI) / totalSteps);
        const knockbackX = spiralRadius * Math.cos(angle) * config.pegasus.horizontalKnockbackMultiplier;
        const knockbackZ = spiralRadius * Math.sin(angle) * config.pegasus.horizontalKnockbackMultiplier;
        const knockbackY = (Math.sin(angle / 2) *
            config.velocity *
            config.pegasus.verticalKnockbackMultiplier);
        if (!isPlayer) {
            target.applyImpulse({
                x: knockbackX,
                y: knockbackY,
                z: knockbackZ,
            });
        }
        if (step % config.pegasus.particleInterval === 0) {
            spawnParticleEffect(sourceEntity, target.location);
        }
        system.runTimeout(() => applyKnockbackAndLevitationCycle(sourceEntity, target, direction, config, isPlayer, totalSteps, step + 1), config.pegasus.knockbackInterval);
        system.runTimeout(() => {
            if (target.isValid() && !isPlayer) {
                target.applyImpulse({ x: 0, y: -1, z: 0 });
            }
        }, config.pegasus.levitationDuration);
    }
}

//# sourceMappingURL=../../../../../_KiCuMountsBPDebug/kicu/mmad/C/PGSAttack.js.map
