Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Define the freeze effect
- const freezeEffect = {
- label: "Freeze",
- icon: "icons/svg/frozen.svg",
- origin: null,
- disabled: false,
- duration: { rounds: 9999, startRound: game.combat?.round, startTime: game.time.worldTime },
- flags: {
- custom: { freezeHookId: null }
- },
- changes: []
- };
- // Function to apply the freeze penalties immediately
- async function applyFreezePenalties(actor) {
- const spdPenalty = -3;
- const refPenalty = -1;
- // Update SPD and Reflex stats
- const currentSpd = actor.system.stats.spd.current;
- const currentRef = actor.system.stats.ref.current;
- console.log(`Current SPD: ${currentSpd}, Current Reflex: ${currentRef}`);
- const newSpd = Math.max(currentSpd + spdPenalty, 0); // Ensure value does not go negative
- const newRef = Math.max(currentRef + refPenalty, 0); // Ensure value does not go negative
- await actor.update({
- 'system.stats.spd.current': newSpd,
- 'system.stats.ref.current': newRef
- });
- console.log(`Updated SPD: ${newSpd}, Updated Reflex: ${newRef}`);
- // Inform the user
- ChatMessage.create({
- content: `${actor.name} has -3 SPD and -1 Reflex penalty from being frozen!`,
- speaker: ChatMessage.getSpeaker({ actor: actor })
- });
- }
- // Function to remove the freeze effect
- async function removeFreezeEffect(actor, effectId) {
- console.log(`Removing ongoing effect: ${freezeEffect.label}`);
- // Restore the SPD and Reflex stats to their original values
- await actor.update({
- 'system.stats.spd.current': actor.system.stats.spd.current - (-3),
- 'system.stats.ref.current': actor.system.stats.ref.current - (-1)
- });
- // Remove the effect from the actor
- await actor.deleteEmbeddedDocuments("ActiveEffect", [effectId]);
- console.log(`Effect removed and stats restored.`);
- }
- // Function to send a reminder message
- function sendReminderMessage(actor) {
- ChatMessage.create({
- content: `${actor.name}, you are frozen! To break free, make a DC:16 Physique check which takes 1 action.`,
- speaker: ChatMessage.getSpeaker({ actor: actor })
- });
- }
- // Function to apply or remove the freeze effect
- async function toggleFreezeEffect() {
- const token = canvas.tokens.controlled[0];
- if (!token) {
- ui.notifications.warn("Please select a token first.");
- return;
- }
- const actor = token.actor;
- // Check if the freeze effect is already applied
- let existingEffect = actor.effects.find(e => e.label === freezeEffect.label);
- if (existingEffect) {
- await removeFreezeEffect(actor, existingEffect.id);
- // Remove the combat hook
- const hookId = existingEffect.flags.custom?.freezeHookId;
- if (hookId) {
- Hooks.off("updateCombat", hookId);
- console.log(`Removed combat hook with ID: ${hookId}`);
- }
- } else {
- console.log(`Applying ongoing effect: ${freezeEffect.label}`);
- freezeEffect.origin = actor.uuid; // Set the origin to the actor's UUID
- // Create the effect on the actor
- const createdEffect = await actor.createEmbeddedDocuments("ActiveEffect", [freezeEffect]);
- const effectId = createdEffect[0].id;
- // Apply the penalties immediately
- await applyFreezePenalties(actor);
- // Set up a hook to send a reminder message 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) {
- console.log(`Sending freeze reminder message for ${token.name}`);
- sendReminderMessage(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.freezeHookId": hookId
- }]);
- } catch (error) {
- console.error("Failed to update flag 'custom' with hookId.", error);
- }
- }
- }
- // Run the toggle function
- toggleFreezeEffect();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement