Guest User

Untitled

a guest
Aug 16th, 2024
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.19 KB | None | 0 0
  1. // Define the hallucination effect
  2. const hallucinationEffect = {
  3. name: "Hallucination",
  4. icon: "icons/svg/daze.svg", // Update icon if needed
  5. origin: null,
  6. disabled: false,
  7. duration: { rounds: 9999, startRound: game.combat?.round, startTime: game.time.worldTime },
  8. flags: {
  9. custom: { hallucinationHookId: null }
  10. },
  11. changes: []
  12. };
  13.  
  14. // Function to apply the hallucination effect and visual effect
  15. async function applyHallucinationEffect(token, actor) {
  16. console.log(`Applying ongoing effect: ${hallucinationEffect.name}`);
  17. hallucinationEffect.origin = actor.uuid; // Set the origin to the actor's UUID
  18.  
  19. // Create the effect on the actor
  20. const createdEffect = await actor.createEmbeddedDocuments("ActiveEffect", [hallucinationEffect]);
  21. const effectId = createdEffect[0].id;
  22.  
  23. // Apply visual effect to the token directly
  24. if (token) {
  25. await token.document.update({
  26. "light.bright": 0, // Bright radius
  27. "light.dim": 6, // Dim radius
  28. "light.color": "#000000", // White color
  29. "light.animation.type": "radialrainbow", // Rainbow pulse animation
  30. "light.animation.speed": 1,
  31. "light.animation.intensity": 0.5,
  32. "light.animation.range": 2, // Adjust as needed
  33. });
  34.  
  35. // Add swirling rainbow effect as a background overlay
  36. await token.document.update({
  37. "effects": [{
  38. "changes": [{
  39. "key": "flags.core.animation.type",
  40. "value": "swirling-rainbow",
  41. "mode": 2
  42. }, {
  43. "key": "flags.core.animation.speed",
  44. "value": 1,
  45. "mode": 2
  46. }, {
  47. "key": "flags.core.animation.intensity",
  48. "value": 1,
  49. "mode": 2
  50. }],
  51. "icon": "icons/svg/poison.svg" // Use the icon for effect
  52. }]
  53. });
  54. }
  55.  
  56. // Set up a hook to remind the player to make Deduction checks
  57. const hookId = Hooks.on("updateCombat", (combat, update, options, userId) => {
  58. const combatant = combat.combatants.find(c => c.tokenId === token.id);
  59.  
  60. // If it's the start of the token's turn
  61. if (combatant && combat.current.tokenId === token.id) {
  62. console.log(`${token.name} is hallucinating.`);
  63. ChatMessage.create({
  64. content: `${actor.name} is hallucinating. Make a DC:15 Deduction check to recognize false images.`,
  65. speaker: ChatMessage.getSpeaker({ actor: actor })
  66. });
  67. }
  68. });
  69.  
  70. console.log(`Created combat hook with ID: ${hookId}`);
  71.  
  72. // Store the hook ID in the effect so we can remove it later
  73. try {
  74. await actor.updateEmbeddedDocuments("ActiveEffect", [{
  75. _id: effectId,
  76. "flags.custom.hallucinationHookId": hookId
  77. }]);
  78. } catch (error) {
  79. console.error("Failed to update flag 'custom' with hookId.", error);
  80. }
  81.  
  82. // Notify about the applied effect
  83. ChatMessage.create({
  84. content: `${actor.name} is hallucinating. Visual distortions are affecting their perception.`,
  85. speaker: ChatMessage.getSpeaker({ actor: actor })
  86. });
  87. }
  88.  
  89. // Function to remove the hallucination effect and visual effect
  90. async function removeHallucinationEffect(actor, effectId) {
  91. console.log(`Removing ongoing effect: ${hallucinationEffect.name}`);
  92.  
  93. // Find the token associated with the actor
  94. const token = canvas.tokens.placeables.find(t => t.actor.id === actor.id);
  95. if (token) {
  96. // Remove lighting effect from the token's document
  97. await token.document.update({
  98. "light.bright": 0,
  99. "light.dim": 0,
  100. "light.color": null,
  101. "light.animation.type": null
  102. });
  103.  
  104. // Remove the swirling rainbow effect
  105. await token.document.update({
  106. "effects": []
  107. });
  108. }
  109.  
  110. await actor.deleteEmbeddedDocuments("ActiveEffect", [effectId]);
  111.  
  112. // Inform the user
  113. ChatMessage.create({
  114. content: `${actor.name} is no longer hallucinating.`,
  115. speaker: ChatMessage.getSpeaker({ actor: actor })
  116. });
  117. }
  118.  
  119. // Toggle hallucination effect
  120. async function toggleHallucinationEffect() {
  121. const token = canvas.tokens.controlled[0];
  122. if (!token) {
  123. ui.notifications.warn("Please select a token first.");
  124. return;
  125. }
  126.  
  127. const actor = token.actor;
  128.  
  129. // Check if the hallucination effect is already applied
  130. let existingEffect = actor.effects.find(e => e.name === hallucinationEffect.name);
  131.  
  132. if (existingEffect) {
  133. await removeHallucinationEffect(actor, existingEffect.id);
  134.  
  135. // Remove the combat hook
  136. const hookId = existingEffect.flags.custom?.hallucinationHookId;
  137. if (hookId) {
  138. Hooks.off("updateCombat", hookId);
  139. console.log(`Removed combat hook with ID: ${hookId}`);
  140. }
  141. } else {
  142. await applyHallucinationEffect(token, actor);
  143. }
  144. }
  145.  
  146. // Run the toggle function
  147. toggleHallucinationEffect();
Add Comment
Please, Sign In to add comment