Advertisement
Guest User

Untitled

a guest
Aug 16th, 2024
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.50 KB | None | 0 0
  1. // Define the suffocation effect
  2. const suffocationEffect = {
  3. label: "Suffocation",
  4. icon: "icons/svg/silenced.svg", // Adjust the icon path if needed
  5. origin: null,
  6. disabled: false,
  7. duration: { rounds: 9999, startRound: game.combat?.round, startTime: game.time.worldTime },
  8. flags: {
  9. custom: { suffocationDamageHookId: null }
  10. },
  11. changes: []
  12. };
  13.  
  14. // Function to apply suffocation damage
  15. async function applySuffocationDamage(actor) {
  16. const suffocationDamage = 3;
  17.  
  18. // Reduce the actor's HP by suffocation damage
  19. const newHP = Math.max(actor.system.derivedStats.hp.value - suffocationDamage, 0);
  20. await actor.update({
  21. 'system.derivedStats.hp.value': newHP
  22. });
  23.  
  24. // Inform the user
  25. ChatMessage.create({
  26. content: `${actor.name} takes ${suffocationDamage} suffocation damage!`,
  27. speaker: ChatMessage.getSpeaker({ actor: actor })
  28. });
  29. }
  30.  
  31. // Function to apply or remove suffocation effect
  32. async function toggleSuffocationEffect() {
  33. const token = canvas.tokens.controlled[0];
  34. if (!token) {
  35. ui.notifications.warn("Please select a token first.");
  36. return;
  37. }
  38.  
  39. const actor = token.actor;
  40.  
  41. // Check if the suffocation effect is already applied
  42. let existingEffect = actor.effects.find(e => e.label === suffocationEffect.label);
  43.  
  44. if (existingEffect) {
  45. console.log(`Removing ongoing effect: ${suffocationEffect.label}`);
  46.  
  47. // Remove the combat hook
  48. try {
  49. const hookId = existingEffect.flags.custom?.suffocationDamageHookId;
  50. if (hookId) {
  51. Hooks.off("updateCombat", hookId);
  52. console.log(`Removed combat hook with ID: ${hookId}`);
  53. }
  54. } catch (error) {
  55. console.error("Failed to get flag 'custom' or flag does not exist.", error);
  56. }
  57.  
  58. // Remove the effect from the actor
  59. await actor.deleteEmbeddedDocuments("ActiveEffect", [existingEffect.id]);
  60. } else {
  61. console.log(`Applying ongoing effect: ${suffocationEffect.label}`);
  62. suffocationEffect.origin = actor.uuid; // Set the origin to the actor's UUID
  63.  
  64. // Create the effect on the actor
  65. const createdEffect = await actor.createEmbeddedDocuments("ActiveEffect", [suffocationEffect]);
  66. const effectId = createdEffect[0].id;
  67.  
  68. // Set up a hook to apply suffocation damage at the start of the affected token's turn
  69. const hookId = Hooks.on("updateCombat", (combat, update, options, userId) => {
  70. const combatant = combat.combatants.find(c => c.tokenId === token.id);
  71.  
  72. // If it's the start of the token's turn
  73. if (combatant && combatant.tokenId === combat.current.tokenId && combat.turns[combat.turn].tokenId === token.id) {
  74. console.log(`Applying suffocation damage to ${token.name}`);
  75.  
  76. // Apply suffocation damage
  77. applySuffocationDamage(actor);
  78. }
  79. });
  80.  
  81. console.log(`Created combat hook with ID: ${hookId}`);
  82.  
  83. // Store the hook ID in the effect so we can remove it later
  84. try {
  85. await actor.updateEmbeddedDocuments("ActiveEffect", [{
  86. _id: effectId,
  87. "flags.custom.suffocationDamageHookId": hookId
  88. }]);
  89. } catch (error) {
  90. console.error("Failed to update flag 'custom' with hookId.", error);
  91. }
  92. }
  93. }
  94.  
  95. // Run the toggle function
  96. toggleSuffocationEffect();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement