Advertisement
ms_63f9f5eaabed15e

Untitled

Aug 16th, 2024
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.73 KB | None | 0 0
  1. // Define the bleed effect
  2. const bleedEffect = {
  3. label: "Bleed",
  4. icon: "icons/svg/blood.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: { bleedDamageHookId: null }
  10. },
  11. changes: []
  12. };
  13.  
  14. // Function to apply bleed damage
  15. async function applyBleedDamage(actor) {
  16. const bleedDamage = 2;
  17.  
  18. // Reduce the actor's HP by bleed damage
  19. const newHP = Math.max(actor.system.derivedStats.hp.value - bleedDamage, 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 ${bleedDamage} bleed damage!`,
  27. speaker: ChatMessage.getSpeaker({ actor: actor })
  28. });
  29. }
  30.  
  31. // Function to prompt First Aid check
  32. function promptFirstAidCheck(actor) {
  33. ChatMessage.create({
  34. content: `${actor.name}, you must make a DC 15 First Aid check to stop the bleeding. This will take a full turn.`,
  35. speaker: ChatMessage.getSpeaker({ actor: actor })
  36. });
  37. }
  38.  
  39. // Function to apply or remove bleed effect
  40. async function toggleBleedEffect() {
  41. const token = canvas.tokens.controlled[0];
  42. if (!token) {
  43. ui.notifications.warn("Please select a token first.");
  44. return;
  45. }
  46.  
  47. const actor = token.actor;
  48.  
  49. // Check if the bleed effect is already applied
  50. let existingEffect = actor.effects.find(e => e.label === bleedEffect.label);
  51.  
  52. if (existingEffect) {
  53. console.log(`Removing ongoing effect: ${bleedEffect.label}`);
  54.  
  55. // Remove the combat hook
  56. try {
  57. const hookId = existingEffect.flags.custom?.bleedDamageHookId;
  58. if (hookId) {
  59. Hooks.off("updateCombat", hookId);
  60. console.log(`Removed combat hook with ID: ${hookId}`);
  61. }
  62. } catch (error) {
  63. console.error("Failed to get flag 'custom' or flag does not exist.", error);
  64. }
  65.  
  66. // Remove the effect from the actor
  67. await actor.deleteEmbeddedDocuments("ActiveEffect", [existingEffect.id]);
  68. } else {
  69. console.log(`Applying ongoing effect: ${bleedEffect.label}`);
  70. bleedEffect.origin = actor.uuid; // Set the origin to the actor's UUID
  71.  
  72. // Create the effect on the actor
  73. const createdEffect = await actor.createEmbeddedDocuments("ActiveEffect", [bleedEffect]);
  74. const effectId = createdEffect[0].id;
  75.  
  76. // Set up a hook to apply bleed damage at the start of the affected token's turn
  77. const hookId = Hooks.on("updateCombat", (combat, update, options, userId) => {
  78. const combatant = combat.combatants.find(c => c.tokenId === token.id);
  79.  
  80. // If it's the start of the token's turn
  81. if (combatant && combatant.tokenId === combat.current.tokenId && combat.turns[combat.turn].tokenId === token.id) {
  82.  
  83. // Apply bleed damage
  84. applyBleedDamage(actor);
  85.  
  86. // Prompt for First Aid check
  87. promptFirstAidCheck(actor);
  88. console.log(`Applying bleed damage to ${token.name}`);
  89. }
  90. });
  91.  
  92. console.log(`Created combat hook with ID: ${hookId}`);
  93.  
  94. // Store the hook ID in the effect so we can remove it later
  95. try {
  96. await actor.updateEmbeddedDocuments("ActiveEffect", [{
  97. _id: effectId,
  98. "flags.custom.bleedDamageHookId": hookId
  99. }]);
  100. } catch (error) {
  101. console.error("Failed to update flag 'custom' with hookId.", error);
  102. }
  103. }
  104. }
  105.  
  106. // Run the toggle function
  107. toggleBleedEffect();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement