Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Define the bleed effect
- const bleedEffect = {
- label: "Bleed",
- icon: "icons/svg/blood.svg", // Adjust the icon path if needed
- origin: null,
- disabled: false,
- duration: { rounds: 9999, startRound: game.combat?.round, startTime: game.time.worldTime },
- flags: {
- custom: { bleedDamageHookId: null }
- },
- changes: []
- };
- // Function to apply bleed damage
- async function applyBleedDamage(actor) {
- const bleedDamage = 2;
- // Reduce the actor's HP by bleed damage
- const newHP = Math.max(actor.system.derivedStats.hp.value - bleedDamage, 0);
- await actor.update({
- 'system.derivedStats.hp.value': newHP
- });
- // Inform the user
- ChatMessage.create({
- content: `${actor.name} takes ${bleedDamage} bleed damage!`,
- speaker: ChatMessage.getSpeaker({ actor: actor })
- });
- }
- // Function to prompt First Aid check
- function promptFirstAidCheck(actor) {
- ChatMessage.create({
- content: `${actor.name}, you must make a DC 15 First Aid check to stop the bleeding. This will take a full turn.`,
- speaker: ChatMessage.getSpeaker({ actor: actor })
- });
- }
- // Function to apply or remove bleed effect
- async function toggleBleedEffect() {
- const token = canvas.tokens.controlled[0];
- if (!token) {
- ui.notifications.warn("Please select a token first.");
- return;
- }
- const actor = token.actor;
- // Check if the bleed effect is already applied
- let existingEffect = actor.effects.find(e => e.label === bleedEffect.label);
- if (existingEffect) {
- console.log(`Removing ongoing effect: ${bleedEffect.label}`);
- // Remove the combat hook
- try {
- const hookId = existingEffect.flags.custom?.bleedDamageHookId;
- 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: ${bleedEffect.label}`);
- bleedEffect.origin = actor.uuid; // Set the origin to the actor's UUID
- // Create the effect on the actor
- const createdEffect = await actor.createEmbeddedDocuments("ActiveEffect", [bleedEffect]);
- const effectId = createdEffect[0].id;
- // Set up a hook to apply bleed 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) {
- // Apply bleed damage
- applyBleedDamage(actor);
- // Prompt for First Aid check
- promptFirstAidCheck(actor);
- console.log(`Applying bleed damage to ${token.name}`);
- }
- });
- 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.bleedDamageHookId": hookId
- }]);
- } catch (error) {
- console.error("Failed to update flag 'custom' with hookId.", error);
- }
- }
- }
- // Run the toggle function
- toggleBleedEffect();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement