MrVauxs / pf2e-jb2a-macros

Discord: @vauxs
GNU General Public License v3.0
11 stars 27 forks source link

Dancing Lights macro places lights too far apart #6

Closed mechamaya closed 2 years ago

mechamaya commented 2 years ago

Pretty minor issue as they can be moved after spawning, but the lights are spawned 20ft apart, which is the 5e rules, I believe. But for PF2e they must not be more than 10ft apart. Here's the modified macro that I used for mine.

/* get the pixels equivalent to 5 feet */
const gridSquare = canvas.scene.data.size/(canvas.scene.data.gridDistance/5);

/* we will be doing this a lot */
const twentyFeet = gridSquare * 2;

/* assume click on our desired "center" */
const startingoffset = {x: -1*gridSquare, y: -1*gridSquare};

/* backwards since thats how pop do */
const placementOffsets = [{x:-twentyFeet}, {y:twentyFeet}, {x:twentyFeet},{x: -1*gridSquare, y: -1*gridSquare}];

/* pop the offsets off one at a time and mody 'location' by that much.
 * Remember, this is a culmulative iteration
 */
const offset = (location) => {
    new Sequence()
        .effect()
            .atLocation(location)
            .file("jb2a.template_circle.out_pulse.02.burst")
            .scaleToObject(2)
            .fadeOut(500)
        .play()
    if (!!placementOffsets.length) {
        const offset = placementOffsets.pop();
        console.log('location, offset',location, offset)
        location.x += offset.x ?? 0;
        location.y += offset.y ?? 0;
    }
}

/* we need to offset the initial placement with pre
 * and subsequent duplicates with post
 */
const callbacks = {
    pre: offset,
    post: offset
}

const config = {
    interval: -1
}

/* Helper function to find and remove an array element by value */
function findDelete(source, searchElement){
    const index = source.indexOf(searchElement);
    if (index > -1) {
        source.splice(index, 1);
        return true;
    }
    return false;
}

/* Spawn in our lights with dimLight already set*/
const lightsIds = await warpgate.spawn("Dancing Light", {}, callbacks, {duplicates:4, crosshairs:config});

/* We are already dismissing the current actor, hence
 * this trigger firing. We need to now dismiss
 * the remianing 3 lights.
 */
const deleteOthers = async (eventData) => {

    /* whoever we are, we are already being deleted */
    findDelete(lightsIds, eventData.actorData.token._id);

    for( const id of lightsIds ){
        // @todo dismiss really should take an array of ids
        new Sequence()
            .effect()
                .atLocation(id)
                .file("jb2a.template_circle.out_pulse.02.burst")
                .scaleToObject(2)
                .fadeOut(500)
            .play()
        await warpgate.dismiss(id, eventData.sceneId);
    }
}

/* we only want our event to trigger when it involves
 * one of our just-spawned lightsIds
 */
const condition = (eventData) => {
    return lightsIds.includes(eventData.actorData.token._id)
}

/* Set up a trigger for when one of our
 * lights is dismissed, all of them
 * get dismissed
 */
warpgate.event.trigger(warpgate.EVENT.DISMISS, deleteOthers, condition)
MrVauxs commented 2 years ago

Fixed for next release 👍