Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Define the hallucination effect
- const hallucinationEffect = {
- name: "Hallucination",
- icon: "icons/svg/daze.svg", // Update icon if needed
- origin: null,
- disabled: false,
- duration: { rounds: 9999, startRound: game.combat?.round, startTime: game.time.worldTime },
- flags: {
- custom: { hallucinationHookId: null }
- },
- changes: []
- };
- // Function to apply the hallucination effect and visual effect
- async function applyHallucinationEffect(token, actor) {
- console.log(`Applying ongoing effect: ${hallucinationEffect.name}`);
- hallucinationEffect.origin = actor.uuid; // Set the origin to the actor's UUID
- // Create the effect on the actor
- const createdEffect = await actor.createEmbeddedDocuments("ActiveEffect", [hallucinationEffect]);
- const effectId = createdEffect[0].id;
- // Apply visual effect to the token directly
- if (token) {
- await token.document.update({
- "light.bright": 0, // Bright radius
- "light.dim": 6, // Dim radius
- "light.color": "#000000", // White color
- "light.animation.type": "radialrainbow", // Rainbow pulse animation
- "light.animation.speed": 1,
- "light.animation.intensity": 0.5,
- "light.animation.range": 2, // Adjust as needed
- });
- // Add swirling rainbow effect as a background overlay
- await token.document.update({
- "effects": [{
- "changes": [{
- "key": "flags.core.animation.type",
- "value": "swirling-rainbow",
- "mode": 2
- }, {
- "key": "flags.core.animation.speed",
- "value": 1,
- "mode": 2
- }, {
- "key": "flags.core.animation.intensity",
- "value": 1,
- "mode": 2
- }],
- "icon": "icons/svg/poison.svg" // Use the icon for effect
- }]
- });
- }
- // Set up a hook to remind the player to make Deduction checks
- 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 && combat.current.tokenId === token.id) {
- console.log(`${token.name} is hallucinating.`);
- ChatMessage.create({
- content: `${actor.name} is hallucinating. Make a DC:15 Deduction check to recognize false images.`,
- speaker: ChatMessage.getSpeaker({ actor: 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.hallucinationHookId": hookId
- }]);
- } catch (error) {
- console.error("Failed to update flag 'custom' with hookId.", error);
- }
- // Notify about the applied effect
- ChatMessage.create({
- content: `${actor.name} is hallucinating. Visual distortions are affecting their perception.`,
- speaker: ChatMessage.getSpeaker({ actor: actor })
- });
- }
- // Function to remove the hallucination effect and visual effect
- async function removeHallucinationEffect(actor, effectId) {
- console.log(`Removing ongoing effect: ${hallucinationEffect.name}`);
- // Find the token associated with the actor
- const token = canvas.tokens.placeables.find(t => t.actor.id === actor.id);
- if (token) {
- // Remove lighting effect from the token's document
- await token.document.update({
- "light.bright": 0,
- "light.dim": 0,
- "light.color": null,
- "light.animation.type": null
- });
- // Remove the swirling rainbow effect
- await token.document.update({
- "effects": []
- });
- }
- await actor.deleteEmbeddedDocuments("ActiveEffect", [effectId]);
- // Inform the user
- ChatMessage.create({
- content: `${actor.name} is no longer hallucinating.`,
- speaker: ChatMessage.getSpeaker({ actor: actor })
- });
- }
- // Toggle hallucination effect
- async function toggleHallucinationEffect() {
- const token = canvas.tokens.controlled[0];
- if (!token) {
- ui.notifications.warn("Please select a token first.");
- return;
- }
- const actor = token.actor;
- // Check if the hallucination effect is already applied
- let existingEffect = actor.effects.find(e => e.name === hallucinationEffect.name);
- if (existingEffect) {
- await removeHallucinationEffect(actor, existingEffect.id);
- // Remove the combat hook
- const hookId = existingEffect.flags.custom?.hallucinationHookId;
- if (hookId) {
- Hooks.off("updateCombat", hookId);
- console.log(`Removed combat hook with ID: ${hookId}`);
- }
- } else {
- await applyHallucinationEffect(token, actor);
- }
- }
- // Run the toggle function
- toggleHallucinationEffect();
Add Comment
Please, Sign In to add comment