Skip to content

Instantly share code, notes, and snippets.

@thomasmckay
Last active May 5, 2020 15:25
Show Gist options
  • Save thomasmckay/7e062adce34c0390523496806a7dfc3e to your computer and use it in GitHub Desktop.
Save thomasmckay/7e062adce34c0390523496806a7dfc3e to your computer and use it in GitHub Desktop.
fvtt arrow example
/*
To run:
createArrows(wasd)
clearArrows()
*/
let name = "PintAndPie";
let user = game.users.entities.find(u => u.name.toLowerCase() === name.toLowerCase());
let token = canvas.tokens.placeables.find(t => t.name.toLowerCase() === name.toLowerCase());
let arrows = {};
arrows[name] = [];
let createArrow = async function(user, startX, startY, endX, endY) {
let g = await Drawing.create({
author: user._id,
type: CONST.DRAWING_TYPES.FREEHAND,
x: 0,
y: 0,
points: [[startX, startY], [endX, endY]],
strokeAlpha: 1.0,
strokeWidth: 8,
strokeColor: user.data.color,
fillAlpha: 0.5,
fillType: 0,
fillColor: user.data.color
});
arrows[name] = arrows[name].concat(g);
return g;
}
let wasd = ["aaa", "www", "ddd", "sss", "ddd"];
let createArrows = function(wasd) {
let prevX = token.data.x + canvas.grid.w / 2,
prevY = token.data.y + canvas.grid.w / 2;
for (let i = 0; i < wasd.length; i++) {
let deltaX = 0,
deltaY = 0;
for (let c = 0; c < wasd[i].length; c++) {
if (wasd[i][c] === "w") {
deltaY = deltaY - 1;
} else if (wasd[i][c] === "s") {
deltaY = deltaY + 1;
} else if (wasd[i][c] === "a") {
deltaX = deltaX - 1;
} else if (wasd[i][c] === "d") {
deltaX = deltaX + 1;
}
}
let nextX = prevX + deltaX * canvas.grid.w,
nextY = prevY + deltaY * canvas.grid.h;
createArrow(user, prevX, prevY, nextX, nextY);
prevX = nextX;
prevY = nextY;
}
}
let clearArrows = async function() {
if (arrows[name] && arrows[name].length > 0) {
await canvas.scene.deleteEmbeddedEntity("Drawing", arrows[name].map(a => a.id));
// arrows[name].forEach(async function(arrow) {
// await canvas.scene.deleteEmbeddedEntity("Drawing", arrow.id);
// });
}
return;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment