Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Define the poison effect
- const poisonEffect = {
- label: "Poison",
- icon: "icons/svg/poison.svg", // Updated icon path
- origin: null,
- disabled: false,
- duration: { rounds: 9999, startRound: game.combat?.round, startTime: game.time.worldTime },
- flags: {
- custom: { poisonDamageHookId: null }
- },
- changes: []
- };
- // Function to apply poison damage
- async function applyPoisonDamage(actor) {
- const poisonDamage = 3;
- // Reduce the actor's HP by poison damage
- const newHP = Math.max(actor.system.derivedStats.hp.value - poisonDamage, 0);
- await actor.update({
- 'system.derivedStats.hp.value': newHP
- });
- // Inform the user
- ChatMessage.create({
- content: `${actor.name} takes ${poisonDamage} poison damage!`,
- speaker: ChatMessage.getSpeaker({ actor: actor })
- });
- }
- // Function to prompt endurance check
- function promptEnduranceCheck(actor) {
- ChatMessage.create({
- content: `${actor.name}, you must make a DC 15 Endurance check to shake off the poison effect.`,
- speaker: ChatMessage.getSpeaker({ actor: actor })
- });
- }
- // Function to apply or remove poison effect
- async function togglePoisonEffect() {
- const token = canvas.tokens.controlled[0];
- if (!token) {
- ui.notifications.warn("Please select a token first.");
- return;
- }
- const actor = token.actor;
- // Check if the poison effect is already applied
- let existingEffect = actor.effects.find(e => e.label === poisonEffect.label);
- if (existingEffect) {
- console.log(`Removing ongoing effect: ${poisonEffect.label}`);
- // Remove the combat hook
- try {
- const hookId = existingEffect.flags.custom?.poisonDamageHookId;
- if (hookId) {
- Hooks.off("updateCombat", hookId);
- console.log(`Removed combat hook with ID: ${hookId}`);
- }
- } catch (error) {
- console.error("Failed to get flag 'custom' or flag does not exist.", error);
- }
- // Remove the effect from the actor
- await actor.deleteEmbeddedDocuments("ActiveEffect", [existingEffect.id]);
- } else {
- console.log(`Applying ongoing effect: ${poisonEffect.label}`);
- poisonEffect.origin = actor.uuid; // Set the origin to the actor's UUID
- // Create the effect on the actor
- const createdEffect = await actor.createEmbeddedDocuments("ActiveEffect", [poisonEffect]);
- const effectId = createdEffect[0].id;
- // Set up a hook to apply poison damage at the start of the affected token's turn
- const hookId = Hooks.on("updateCombat", (combat, update, options, userId) => {
- const combatant = combat.combatants.find(c => c.tokenId === token.id);
- // If it's the start of the token's turn
- if (combatant && combatant.tokenId === combat.current.tokenId && combat.turns[combat.turn].tokenId === token.id) {
- console.log(`Applying poison damage to ${token.name}`);
- // Apply poison damage
- applyPoisonDamage(actor);
- // Prompt for Endurance check
- promptEnduranceCheck(actor);
- }
- });
- 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.poisonDamageHookId": hookId
- }]);
- } catch (error) {
- console.error("Failed to update flag 'custom' with hookId.", error);
- }
- }
- }
- // Run the toggle function
- togglePoisonEffect();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement