-
Notifications
You must be signed in to change notification settings - Fork 0
/
portrait.js
168 lines (153 loc) · 6.06 KB
/
portrait.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/* eslint-disable no-await-in-loop */
/* eslint no-param-reassign: ["error", { "props": false }] */
/* eslint-disable no-restricted-syntax */
/* eslint-disable no-underscore-dangle */
/* eslint-disable no-undef */
/*
Requires the following:
- Any number of users, each with a selected character assigned in User Configuration.
- Each of these characters must have an assigned avatar (character art).
- On your landing page, one token per user with the name 'portrait (x)' where x is a
number from 1 to the number of users you have. You can import 'fvtt-Actor-portrait.json'.
- The script supports any size and proportion (in grid squares) of token, as well as rotation.
- Somewhere suitable near the tokens, one text drawing per user with the text 'Player x' where
x is the number mentioned above.
*/
Hooks.on('canvasReady', async (canvas) => {
const size = 1;
const party = game.actors.party.members.filter((c) => c.type === 'character' && !['Animal Companion', 'Eidolon', 'Construct Companion'].includes(c.class?.name));
let maxPNum = 0;
async function setNum(u, fi, ai) {
maxPNum = fi * 6 + ai + 1;
await u.setFlag('world', 'userManager.playerNum', maxPNum);
}
const portraitTokens = canvas.tokens.placeables.filter((t) => t.name.substring(0, 8).toLowerCase() === 'portrait');
const users = [];
if (portraitTokens.length <= 6) {
for (const [ai, a] of party.entries()) {
await setNum(a, 0, ai);
users.push(a);
}
} else {
const folders = game.folders.filter((f) => f.type === 'Actor' && f.name.match('^[0-9]{4}..*'))
.sort((a, b) => {
if (a.name < b.name) {
return -1;
}
if (a.name > b.name) {
return 1;
}
return 0;
});
for (const [fi, f] of folders.entries()) {
if (f.contents.length === 0) {
for (const [ai, a] of party.entries()) {
await setNum(a, fi, ai);
users.push(a);
}
} else {
const chars = f.contents.filter((c) => c.type === 'character' && !['Animal Companion', 'Eidolon', 'Construct Companion'].includes(c.class?.name));
for (const [ai, a] of chars.entries()) {
await setNum(a, fi, ai);
users.push(a);
}
}
}
}
const pTMatch = portraitTokens;
let cnum = 1;
for (t of portraitTokens) {
const playerNo = t.name.match(/[0-9]+(?=\)$)/)[0];
t.id2 = `token${playerNo}`;
const matches = pTMatch.filter((to) => to.name === t.name).length;
if (matches > 1) {
t.id2 = `token${playerNo}.${cnum}`;
cnum += 1;
} else {
cnum = 1;
}
}
const playerLabels = game.canvas.drawings.objects.children
.filter((d) => d.text?.text.substring(0, 6).toLowerCase() === 'player')
.sort((a, b) => a.text.text.match(/[0-9]+$/)[0] - b.text.text.match(/[0-9]+$/)[0]);
playerLabels.forEach((d) => {
const drawing = d;
const playerNo = drawing.text.text.match(/[0-9]+$/)[0];
drawing.id2 = `player-label${playerNo}`;
});
if (!portraitTokens) return;
async function getOwner() {
const chars = [];
users.forEach(async (u) => {
u.owner = { id: u.id, name: u.name, playerNum: await u.getFlag('world', 'userManager.playerNum') };
chars.push(u);
});
return chars;
}
const characters = await getOwner();
async function cPrep(c) {
const playerLabel = playerLabels.find((l) => l.id2 === `player-label${c.owner.playerNum}`);
if (!playerLabel) return;
[playerLabel.text.text] = c.prototypeToken.name.match(/[^\s]+/);
const portraitToken = portraitTokens.find((t) => t.id2 === `token${c.owner.playerNum}`);
let markerTexture;
if (c.img === 'systems/pf2e/icons/default-icons/character.svg') {
markerTexture = await loadTexture('modules/pfs2/assets/pfs-tan.svg');
await portraitToken.mesh.document.update({ alpha: 1 });
portraitToken.refresh();
} else {
markerTexture = await loadTexture(c.img);
await portraitToken.mesh.document.update({ alpha: 0 });
portraitToken.refresh();
}
if (!markerTexture.baseTexture) return;
const textureAR = markerTexture.baseTexture.width / markerTexture.baseTexture.height;
const textureARinv = markerTexture.baseTexture.height / markerTexture.baseTexture.width;
const tokenAR = portraitToken.document.width / portraitToken.document.height;
let textureHeight = 0;
let textureWidth = 0;
if (textureAR < tokenAR) {
textureHeight = canvas.grid.size * portraitToken.document.height;
textureWidth = canvas.grid.size * portraitToken.document.width * tokenAR;
} else {
textureHeight = canvas.grid.size * portraitToken.document.width * textureARinv;
textureWidth = canvas.grid.size * portraitToken.document.width;
}
markerTexture.orig = {
height: size * textureHeight,
width: size * textureWidth,
x: textureWidth * size * 0.5,
y: textureHeight * size * 0.5,
};
const sprite = new PIXI.Sprite(markerTexture);
sprite.angle = portraitToken.document.rotation;
if (VideoHelper.hasVideoExtension(c.img)) {
sprite.texture.baseTexture.resource.source.loop = true;
sprite.texture.baseTexture.resource.source.play();
}
sprite.anchor.set(0.5, 0.5);
const markerToken = portraitToken.addChild(sprite);
portraitToken.sortableChildren = true;
markerToken.zIndex = 100;
markerToken.transform.position.set(
canvas.grid.size * portraitToken.document.width * 0.5,
canvas.grid.size * portraitToken.document.height * 0.5,
);
markerToken.id = `player-portrait${c.owner.playerNum}`;
markerToken.alpha = 1;
}
async function cloop(chars) {
chars.forEach(async (c) => {
await cPrep(c);
});
}
cloop(characters);
await new Promise((r) => { setTimeout(r, 1000); });
users.forEach((u) => {
const chars = game.actors.filter((a) => a.getUserLevel(u) === 3 && a.name !== 'Party Loot');
chars.forEach((ch) => {
const label = game.canvas.drawings.objects.children.find((d) => d.text?.text === ch.prototypeToken.name.match(/[^\s]+/)[0]);
if (label) { label.refresh(); }
});
});
});