
import { system } from "@minecraft/server"

export function projectileSpawn(player, projectile, speedMultiplier = 3, despawnTime = 200) {
    let coords = player.getHeadLocation();
    const arrow = player.dimension.spawnEntity(projectile, {
        x: (coords.x + player.getViewDirection().x),
        y: (coords.y + player.getViewDirection().y),
        z: (coords.z + player.getViewDirection().z)
    });
    arrow.addTag("over_powered");
    const projectileComp = arrow.getComponent('minecraft:projectile');
    projectileComp.owner = player;
    projectileComp?.shoot(player.getViewDirection());
    arrow.clearVelocity();
    arrow.applyImpulse({
        x: player.getViewDirection().x * speedMultiplier,
        y: (player.getViewDirection().y * speedMultiplier),
        z: player.getViewDirection().z * speedMultiplier
    })
    system.runTimeout(() => {
        try {
            arrow.remove()
        } catch (error) { }
    }, despawnTime);
}