Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Define the staggered effect
- const staggeredEffect = {
- label: "Staggered",
- icon: "icons/svg/sword.svg",
- origin: null,
- disabled: false,
- duration: { rounds: 2, startRound: game.combat?.round, startTime: game.time.worldTime },
- flags: {
- custom: { staggeredHookId: null }
- },
- changes: [
- // Apply a -2 penalty to specific skills
- { key: 'system.skills.ref.brawling.value', mode: CONST.ACTIVE_EFFECT_MODES.ADD, value: -2 },
- { key: 'system.skills.ref.melee.value', mode: CONST.ACTIVE_EFFECT_MODES.ADD, value: -2 },
- { key: 'system.skills.ref.smallblades.value', mode: CONST.ACTIVE_EFFECT_MODES.ADD, value: -2 },
- { key: 'system.skills.ref.staffspear.value', mode: CONST.ACTIVE_EFFECT_MODES.ADD, value: -2 },
- { key: 'system.skills.ref.swordsmanship.value', mode: CONST.ACTIVE_EFFECT_MODES.ADD, value: -2 },
- { key: 'system.skills.dex.archery.value', mode: CONST.ACTIVE_EFFECT_MODES.ADD, value: -2 },
- { key: 'system.skills.dex.crossbow.value', mode: CONST.ACTIVE_EFFECT_MODES.ADD, value: -2 },
- { key: 'system.skills.will.spellcast.value', mode: CONST.ACTIVE_EFFECT_MODES.ADD, value: -2 },
- { key: 'system.skills.will.resistmagic.value', mode: CONST.ACTIVE_EFFECT_MODES.ADD, value: -2 },
- { key: 'system.skills.ref.dodge.value', mode: CONST.ACTIVE_EFFECT_MODES.ADD, value: -2 }
- ]
- };
- // Function to remove the staggered effect and restore stats
- async function removeStaggeredEffect(actor, effectId) {
- console.log(`Removing ongoing effect: ${staggeredEffect.label}`);
- await actor.deleteEmbeddedDocuments("ActiveEffect", [effectId]);
- // Inform the user
- ChatMessage.create({
- content: `${actor.name} has recovered from being staggered.`,
- speaker: ChatMessage.getSpeaker({ actor: actor })
- });
- }
- // Toggle staggered effect
- async function toggleStaggeredEffect() {
- const token = canvas.tokens.controlled[0];
- if (!token) {
- ui.notifications.warn("Please select a token first.");
- return;
- }
- const actor = token.actor;
- // Check if the staggered effect is already applied
- let existingEffect = actor.effects.find(e => e.label === staggeredEffect.label);
- if (existingEffect) {
- await removeStaggeredEffect(actor, existingEffect.id);
- // Remove the combat hook
- const hookId = existingEffect.flags.custom?.staggeredHookId;
- if (hookId) {
- Hooks.off("updateCombat", hookId);
- console.log(`Removed combat hook with ID: ${hookId}`);
- }
- } else {
- console.log(`Applying ongoing effect: ${staggeredEffect.label}`);
- staggeredEffect.origin = actor.uuid; // Set the origin to the actor's UUID
- // Create the effect on the actor
- const createdEffect = await actor.createEmbeddedDocuments("ActiveEffect", [staggeredEffect]);
- const effectId = createdEffect[0].id;
- // Set up a hook to remove the effect at the start of the next turn
- const hookId = Hooks.on("updateCombat", (combat, update, options, userId) => {
- const combatant = combat.combatants.find(c => c.tokenId === token.id);
- // Check if it's the start of the token's next turn
- if (combatant && combat.turns[combat.turn].tokenId === token.id) {
- console.log(`Removing staggered effect for ${token.name}`);
- removeStaggeredEffect(actor, effectId);
- }
- });
- console.log(`Created combat hook with ID: ${hookId}`);
- // Store the hook ID in the effect so we can remove it later
- try {
- await actor.updateEmbeddedDocuments("ActiveEffect", [{
- _id: effectId,
- "flags.custom.staggeredHookId": hookId
- }]);
- } catch (error) {
- console.error("Failed to update flag 'custom' with hookId.", error);
- }
- // Inform the user
- ChatMessage.create({
- content: `${actor.name} is staggered and takes -2 to Attack Skills and Defensive Skills!`,
- speaker: ChatMessage.getSpeaker({ actor: actor })
- });
- }
- }
- // Run the toggle function
- toggleStaggeredEffect();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement