Skip to content

Commit

Permalink
added comments
Browse files Browse the repository at this point in the history
  • Loading branch information
paulo-roger committed Sep 26, 2022
1 parent 94bab7e commit 2b26ea6
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 101 deletions.
25 changes: 16 additions & 9 deletions scripts/dynamic-shadows.js
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -12,43 +11,49 @@ 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++) {
await lightCollision(tokens[i], tokens[i].x, tokens[i].y)
}
});

// 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} ======================================= `)
Expand Down Expand Up @@ -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)
Expand All @@ -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 =
[{
Expand All @@ -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,
Expand All @@ -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);
Expand Down
97 changes: 5 additions & 92 deletions scripts/old.js
Original file line number Diff line number Diff line change
@@ -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");
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -126,8 +81,6 @@ async function createManyShadows(token, tokenX, tokenY) {
await canvasToken.TMFXdeleteFilters(tokensLightsInScene[i].id)
}
}
// });

}

async function castShadow(canvasToken, catad, arctan, id) {
Expand Down Expand Up @@ -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
// }
}

0 comments on commit 2b26ea6

Please sign in to comment.