From 2b26ea6058d9a7834f4d35c9aeea5f7c25dab79c Mon Sep 17 00:00:00 2001 From: Paulo Roger Date: Mon, 26 Sep 2022 17:51:06 -0300 Subject: [PATCH] added comments --- scripts/dynamic-shadows.js | 25 ++++++---- scripts/old.js | 97 ++------------------------------------ 2 files changed, 21 insertions(+), 101 deletions(-) diff --git a/scripts/dynamic-shadows.js b/scripts/dynamic-shadows.js index 26e24ff..5fcf9e0 100644 --- a/scripts/dynamic-shadows.js +++ b/scripts/dynamic-shadows.js @@ -1,6 +1,5 @@ import { MODULE, MODULE_DIR } from "./const.js"; //import the const variables -// import { registerSettings, cacheSettings, enableFT, enableForAll, scaleFT, enableZoom, chatOutput, notificationOutput } from "./settings.js" //import settings -// import { FlyingHud } from "./flying-hud.js" +import { registerSettings, cacheSettings } from "./settings.js" //import settings //Compatibility with v9 let fvttVersion @@ -12,12 +11,14 @@ Hooks.once("init", () => { // cacheSettings(); }); +// hook to check the foundry version and to show the module is loaded. Hooks.once('ready', async function () { fvttVersion = parseInt(game.version) console.log(" ====================================== 🕵 Shadows ======================================= ") console.log(" ==================================== FoundryVTT Version:", fvttVersion, " ==================================== ") }); +// hook that triggers every time the light refresh, check light collision for every token in the ground level (for now lets keep it simple) and that is visible. Hooks.on("lightingRefresh", async () => { let tokens = canvas.scene.tokens.contents.filter(i => i.elevation === 0 && i.hidden === false) for (var i = 0; i < tokens.length; i++) { @@ -25,30 +26,34 @@ Hooks.on("lightingRefresh", async () => { } }); +// convert radians to degrees function radToDeg(rad) { return rad / (Math.PI / 180); } +// calc the sides of the triangle function cathetus(tokenXY, lightXY) { return tokenXY - lightXY + (canvas.scene.grid.size / 2) } +// hypothenuse + adjust using canvas grid properties to use the center point of the square. Using the grid properties makes the code adjustable to any grid size or unit function calcDistance (cathetusA, cathetusB){ return (Math.hypot(cathetusA, cathetusB) / (canvas.scene.grid.size / canvas.scene.grid.distance)) + (canvas.scene.grid.distance / 2) } +// checks light collision with the token async function lightCollision(token, tokenX, tokenY) { let canvasToken = canvas.tokens.get(token.id) // console.log(" ====================================== 🕵 DEBUG: Shadows ======================================= ") - let lightsInScene = canvas.scene.lights.filter(i => i.hidden === false) - let tokensLightsInScene = canvas.tokens.objects.children.filter(i => i.light.active === true) + let lightsInScene = canvas.scene.lights.filter(i => i.hidden === false) // array of lights in the scene that are not faded. + let tokensLightsInScene = canvas.tokens.objects.children.filter(i => i.light.active === true) // array of tokens that are emitting light. for (var i = 0; i < lightsInScene.length; i++) { // console.log(" ====================================== 🕵 DEBUG: lightingRefresh - LIGHTS 💡 ======================================= ") let lightX = lightsInScene[i].x let lightY = lightsInScene[i].y let OpoCathetus = cathetus(tokenY, lightY) let adjCathetus = cathetus(tokenX, lightX) - let distance = calcDistance(OpoCathetus, adjCathetus) + let distance = calcDistance(OpoCathetus, adjCathetus) //distance of the center of the token square to the center of the light square let lightRadius = lightsInScene[i].config.dim // console.log(` ====================================== sombra n.${i} ======================================= `) @@ -77,6 +82,7 @@ async function lightCollision(token, tokenX, tokenY) { // console.log(`light X: ${lightX}, light Y: ${lightY}, token X: ${tokenX}, token Y: ${tokenY}, raio da luz:${lightRadius}`) // console.log(`cat oposto: ${OpoCathetus}, cat adjacente: ${adjCathetus}, distancia da Luz: ${distance}`) + //this if is for the shadow of the torch holder, creates a shadow below her/him. if (token.id == tokensLightsInScene[i].id) { if (!canvasToken.TMFXhasFilterId(token.id)) { await castSelfShadow(canvasToken, tokensLightsInScene[i].id) @@ -95,9 +101,10 @@ async function lightCollision(token, tokenX, tokenY) { } } +// creates the shadow with the parameters params below. async function castShadow(canvasToken, adjCathetus, OpoCathetus, id) { - let shadowAngle = Math.round(radToDeg(Math.atan(OpoCathetus / adjCathetus))) - if (adjCathetus < 0) shadowAngle += 180 + let shadowAngle = Math.round(radToDeg(Math.atan(OpoCathetus / adjCathetus))) // angle of the shadow around the token. + if (adjCathetus < 0) shadowAngle += 180 // adjust the angle in case of the adjacent cathetus is negative console.log(` ====================================== SHADOW ANGLE: ${shadowAngle} ======================================= `) let params = [{ @@ -122,7 +129,7 @@ async function castShadow(canvasToken, adjCathetus, OpoCathetus, id) { val1: 2, val2: 3 }, - rotation: + rotation: //makes the shadow flickers { active: true, loopDuration: 300, @@ -132,7 +139,7 @@ async function castShadow(canvasToken, adjCathetus, OpoCathetus, id) { } } }]; - if (canvasToken.document.getFlag('tokenmagic', 'filters') !== undefined) { + if (canvasToken.document.getFlag('tokenmagic', 'filters') !== undefined) { // this part is to optmize the code, if the shadow already exists and the angle didn't change, do nothing. else apply the shadow filter. if (canvasToken.document.getFlag('tokenmagic', 'filters').find(i => i.tmFilters.tmParams.rotation === shadowAngle) === undefined) await TokenMagic.addUpdateFilters(canvasToken, params); } else await TokenMagic.addUpdateFilters(canvasToken, params); diff --git a/scripts/old.js b/scripts/old.js index 70669ee..4ca9239 100644 --- a/scripts/old.js +++ b/scripts/old.js @@ -1,41 +1,5 @@ import { MODULE, MODULE_DIR } from "./const.js"; //import the const variables -import { chatMessage } from "./util.js" -// import { registerSettings, cacheSettings, enableFT, enableForAll, scaleFT, enableZoom, chatOutput, notificationOutput } from "./settings.js" //import settings -// import { FlyingHud } from "./flying-hud.js" - -//Compatibility with v9 -let fvttVersion - -// Hook that trigger once when the game is initiated. Register and cache settings. -Hooks.once("init", () => { - // registerWrappers(); - // registerSettings(); - // cacheSettings(); -}); - -Hooks.once('ready', async function () { - fvttVersion = parseInt(game.version) - console.log(" ====================================== 🕵 Shadows ======================================= ") - console.log(" ==================================== FoundryVTT Version:", fvttVersion, " ==================================== ") - //compatibility with v9 - // if (fvttVersion < 10) { - // } -}); - -// Hooks.once("refreshToken", async (token) => { -// console.log(" ====================================== 🕵 Hook Refresh token ======================================= ", token) -// if (token !== undefined) -// /* await */ createManyShadows(token.document, token.x, token.y); - -// }); - -// Hooks.on("refreshToken", async (token) => { -// console.log(" ====================================== 🕵 Hook Refresh token ======================================= ", CanvasAnimation.animations.isPrototypeOf()) -// console.log(CanvasAnimation.getAnimation('Token.soPFQpa4y60ybpLt.animate')) -// // console.log("*********************Time:",CanvasAnimation.getAnimation(CanvasAnimation.animations.valueOf()).duration) -// console.log(`token X: ${token.x}, token Y: ${token.y}`) -// }); - +import { registerSettings, cacheSettings } from "./settings.js" //import settings Hooks.on("preUpdateToken", async (token, updateData) => { let tokenX = getProperty(updateData, "x"); @@ -66,20 +30,11 @@ Hooks.on("preUpdateToken", async (token, updateData) => { // }); }); - function radToDeg(rad) { return rad / (Math.PI / 180); } async function createManyShadows(token, tokenX, tokenY) { - // let adistance - // if (atokenX || atokenY === 0) - // adistance = Math.abs(atokenX + atokenY) - // else adistance = Math.hypot(atokenX, atokenY) - // console.log(` ====================================== DISTANCE : ${adistance} ======================================= `) - // Hooks.on("refreshToken", async (tokenRef) => { - // let tokenX = tokenRef.x - // let tokenY = tokenRef.y let canvasToken = canvas.tokens.get(token.id) // console.log(" ====================================== 🕵 Shadows ======================================= ") let lightsInScene = canvas.scene.lights.filter(i => i.hidden === false) @@ -115,9 +70,9 @@ async function createManyShadows(token, tokenX, tokenY) { let lightRadius = tokensLightsInScene[i].light.radius arctan = radToDeg(arctan) - console.log(` ====================================== sombra n.${i} ======================================= `) - console.log(`light X: ${lightX}, light Y: ${lightY}, token X: ${tokenX}, token Y: ${tokenY}, raio da luz:${lightRadius}`) - console.log(`cat oposto: ${catop}, cat adjacente: ${catad}, arc tangente: ${arctan}, distancia da Luz: ${distance}`) + // console.log(` ====================================== sombra n.${i} ======================================= `) + // console.log(`light X: ${lightX}, light Y: ${lightY}, token X: ${tokenX}, token Y: ${tokenY}, raio da luz:${lightRadius}`) + // console.log(`cat oposto: ${catop}, cat adjacente: ${catad}, arc tangente: ${arctan}, distancia da Luz: ${distance}`) if ((distance + (canvas.scene.grid.distance / 2)) <= lightRadius / (canvas.scene.grid.size / canvas.scene.grid.distance)) { await castShadow(canvasToken, catad, arctan, tokensLightsInScene[i].id) @@ -126,8 +81,6 @@ async function createManyShadows(token, tokenX, tokenY) { await canvasToken.TMFXdeleteFilters(tokensLightsInScene[i].id) } } - // }); - } async function castShadow(canvasToken, catad, arctan, id) { @@ -167,44 +120,4 @@ async function castShadow(canvasToken, catad, arctan, id) { } }]; await TokenMagic.addUpdateFilters(canvasToken, params); -} - -//canvas.scene.lights (é uma embedded collection) -//canvas.lighting.placeables[0].x -// canvas.lighting.objects.children[0].document.x -// let lightY = 525 -// let lightX = 875 - - -// function shadow() { -// let lightsInScene = canvas.scene.lights.filter(i => i.hidden === false) -// let tokensLightsInScene = canvas.tokens.objects.children.filter(i => i.light.active === true) -// let light -// if (lightsInScene.length >= 0) { -// light = new Light(lightsInScene[i].x, lightsInScene[i].y, lightsInScene[i].config.dim, lightsInScene[i].id) -// } -// if (tokensLightsInScene.length >= 0) { -// light = new Light(tokensLightsInScene[i].x, tokensLightsInScene[i].y, tokensLightsInScene[i].config.dim, tokensLightsInScene[i].id) -// } -// } - -// class Light { -// constructor(x, y, radius, id) { -// this.x = x, -// this.y = y, -// this.radius = radius, -// this.id = id - -// } -// } - -// function calcShadow(light) { -// let lightX = light.x -// let lightY = light.y -// let catop = tokenY - lightY + (canvas.scene.grid.size / 2) -// let catad = tokenX - lightX + (canvas.scene.grid.size / 2) //precisa retornar -// let arctan = Math.atan(catop / catad) -// let distance = Math.hypot(catop, catad) / (canvas.scene.grid.size / canvas.scene.grid.distance) //precisa retornar -// let lightRadius = light.config.dim //precisa retornar -// arctan = radToDeg(arctan) //precisa retornar -// } +} \ No newline at end of file