import * as mc from "@minecraft/server";
import { Projectile, SafetyCheckedPlayer, Vector3, mclog } from "./utils/index";
const LOG_TAG = "logBoggedWeapon";
const ARROW_SPACING_DEGREES = 15;
export function initBoggedAttack() {
    mc.system.afterEvents.scriptEventReceive.subscribe(event => {
        if (event.id === "ldz:bogged_weapon" && event.sourceEntity && event.sourceEntity.typeId === "minecraft:player") {
            boggedAttack(new SafetyCheckedPlayer(event.sourceEntity));
        }
    });
}
function boggedAttack(player) {
    mclog("====================", LOG_TAG);
    mclog("Bogged Attack", LOG_TAG);
    player.runCommand("playsound random.bow @a ~ ~ ~");
    const playerHeadLocation = player.getHeadLocation();
    const { x: playerXRotation, y: playerYRotation } = player.getRotation();
    const pitchAngleRad = -playerXRotation * (Math.PI / 180);
    const yawAngleRad = playerYRotation * (Math.PI / 180);
    const forwardVector = new Vector3({ x: -Math.sin(yawAngleRad) * Math.cos(pitchAngleRad), y: Math.sin(pitchAngleRad), z: Math.cos(yawAngleRad) * Math.cos(pitchAngleRad) });
    const rightVector = new Vector3({ x: -Math.cos(yawAngleRad), y: 0, z: -Math.sin(yawAngleRad) });
    const upVector = cross(rightVector, forwardVector);
    // player.dimension.spawnParticle("ldz:ray_debug", playerHeadLocation.add(forwardVector));
    // player.dimension.spawnParticle("ldz:ray_debug", playerHeadLocation.add(rightVector));
    // player.dimension.spawnParticle("ldz:ray_debug", playerHeadLocation.add(upVector));
    // mclog("§c"+forwardVector)
    // mclog("§a"+rightVector)
    // mclog("§9"+upVector)
    for (let index = -1; index <= 1; index++) {
        const localOffset = new Vector3({ x: 0, y: 0, z: 1.2 }).rotateByAngle(ARROW_SPACING_DEGREES * index);
        const arrowVector = new Vector3(0)
            .add(forwardVector.multiply(localOffset.z))
            .add(rightVector.multiply(localOffset.x))
            .add(upVector.multiply(localOffset.y));
        const antiHitAdjustment = Math.max(Math.sin(playerXRotation * (Math.PI / 180)), 0) * -1.0;
        const spawnLocation = playerHeadLocation.add({ x: 0, y: antiHitAdjustment, z: 0 }).add(arrowVector);
        // const arrow = new SafetyCheckedEntity(player.dimension.spawnEntity("ldz:bogged_weapon_projectile", spawnLocation));
        const arrow = new Projectile(spawnLocation, player.dimension, "ldz:bogged_weapon_projectile", arrowVector, player, {
            shoot_sound: "random.bow",
            hit_sound: "weapon.bogged_projectile.impact",
            faceDirection: true,
            gravity: 0.01,
            power: 4.5,
            stick_in_ground: true,
            on_hit: {
                filters: {
                    excludeTypes: ["ldz:bogged_weapon_projectile"]
                },
                destroy_on_hit: true,
                damage: 6,
                hit_callback: (projectile, hitEntities, hitBlock) => {
                    hitEntities.forEach(entity => entity.addEffect("fatal_poison", 5 * mc.TicksPerSecond));
                }
            }
        });
    }
    function cross(vectorA, vectorB) {
        return new Vector3({
            x: vectorA.y * vectorB.z - vectorA.z * vectorB.y,
            y: vectorA.z * vectorB.x - vectorA.x * vectorB.z,
            z: vectorA.x * vectorB.y - vectorA.y * vectorB.x
        });
    }
}
