import * as mc from "@minecraft/server";
import * as utils from "../utils/index";
const meatCount = 10;
const itemDropPool = [
    'minecraft:cooked_beef',
    'minecraft:cooked_chicken',
    'minecraft:cooked_cod',
    'minecraft:cooked_mutton',
    'minecraft:cooked_porkchop',
    'minecraft:cooked_rabbit',
    'minecraft:cooked_salmon',
];
export function furnaceAbility(player) {
    utils.mclog(`furnace`);
    const origin = new utils.Vector3(player.getHeadLocation()).add(new utils.Vector3(player.getViewDirection()).multiply(1.5));
    mc.world.playSound('firework.launch', player.location);
    mc.world.playSound('ability.furnace.activate', player.location);
    for (let index = 0; index < meatCount; index++) {
        const skull = player.dimension.spawnEntity('ldz:furnace_ability', origin);
        let direction = new utils.Vector3(player.getViewDirection()).rotateByAngle(utils.getRandomInteger(-15, 15)).normalize();
        direction = rotateYByAngle(utils.getRandomInteger(-15, 15), direction);
        skull.applyImpulse(direction.multiply(2));
    }
    const localHurtEvent = hurtEvent;
    mc.world.afterEvents.entityHurt.subscribe(localHurtEvent);
    utils.waitForEvent(40, () => {
        mc.world.afterEvents.entityHurt.unsubscribe(localHurtEvent);
    });
}
function hurtEvent(event) {
    var _a;
    if (event.damageSource.cause === mc.EntityDamageCause.projectile && ((_a = event.damageSource.damagingProjectile) === null || _a === void 0 ? void 0 : _a.typeId) === 'ldz:furnace_ability') {
        const target = new utils.Vector3(event.damageSource.damagingProjectile.location);
        const stack = event.damageSource.damagingProjectile.dimension.spawnItem(new mc.ItemStack(itemDropPool[utils.getRandomInteger(0, itemDropPool.length - 1)], utils.getRandomInteger(1, 64)), target);
        const impulse = new utils.Vector3({ x: 1, y: 0, z: 1 }).rotateByAngle(utils.getRandomInteger(-180, 180)).normalize().multiply(0.1);
        mc.world.playSound('random.pop', target);
        stack.applyImpulse(impulse);
    }
}
function rotateYByAngle(angle, vector) {
    const sin = Math.sin(angle * (3.14 / 180));
    const cos = Math.cos(angle * (3.14 / 180));
    const vectorA = new utils.Vector3({ x: cos, y: sin, z: 0 });
    const vectorB = new utils.Vector3({ x: -sin, y: cos, z: 0 });
    const newVector = vectorA.multiply(vector.x).add(vectorB.multiply(vector.y));
    newVector.z = vector.z;
    return newVector;
}
