Track the movement of all tokens that are moved simultaneously in one batch (huge performance bump if many tokens are moved)
This commit is contained in:
@@ -64,8 +64,8 @@ async function animateTokens(tokens, draggedToken, draggedRays, wasPaused) {
|
|||||||
return {token, rays: offsetRays, dx, dy};
|
return {token, rays: offsetRays, dx, dy};
|
||||||
});
|
});
|
||||||
|
|
||||||
|
trackRays(tokenAnimationData.map(({token}) => token), tokenAnimationData.map(({rays}) => rays));
|
||||||
for (const {token, rays} of tokenAnimationData) {
|
for (const {token, rays} of tokenAnimationData) {
|
||||||
trackRays(token, rays);
|
|
||||||
token._noAnimate = true;
|
token._noAnimate = true;
|
||||||
}
|
}
|
||||||
for (let i = 0;i < tokenAnimationData[0].rays.length; i++) {
|
for (let i = 0;i < tokenAnimationData[0].rays.length; i++) {
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import {measureDistances} from "./compatibility.js";
|
import {measureDistances} from "./compatibility.js";
|
||||||
import {updateCombatantDragRulerFlags} from "./socket.js";
|
import {updateCombatantDragRulerFlags} from "./socket.js";
|
||||||
import {getTokenShape} from "./util.js";
|
import {getTokenShape, zip} from "./util.js";
|
||||||
|
|
||||||
function initTrackingFlag(combatant) {
|
function initTrackingFlag(combatant) {
|
||||||
const initialFlag = {passedWaypoints: [], trackedRound: 0};
|
const initialFlag = {passedWaypoints: [], trackedRound: 0};
|
||||||
@@ -23,13 +23,21 @@ function getInitializedCombatant(token, combat) {
|
|||||||
return combatant;
|
return combatant;
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function trackRays(token, rays) {
|
export async function trackRays(tokens, tokenRays) {
|
||||||
// Only track movement if the current token is participating in the active combat
|
|
||||||
const combat = game.combat;
|
const combat = game.combat;
|
||||||
if (!combat)
|
if (!combat)
|
||||||
return;
|
return;
|
||||||
if (!combat.started)
|
if (!combat.started)
|
||||||
return;
|
return;
|
||||||
|
if (!(tokens instanceof Array)) {
|
||||||
|
tokens = [tokens];
|
||||||
|
tokenRays = [tokenRays];
|
||||||
|
}
|
||||||
|
const updates = Array.from(zip(tokens, tokenRays)).map(([token, rays]) => calculateUpdate(combat, token, rays)).filter(Boolean);
|
||||||
|
await updateCombatantDragRulerFlags(combat, updates);
|
||||||
|
}
|
||||||
|
|
||||||
|
function calculateUpdate(combat, token, rays) {
|
||||||
const combatant = getInitializedCombatant(token, combat);
|
const combatant = getInitializedCombatant(token, combat);
|
||||||
if (!combatant)
|
if (!combatant)
|
||||||
return;
|
return;
|
||||||
@@ -55,7 +63,7 @@ export async function trackRays(token, rays) {
|
|||||||
waypoints.push(ray.A);
|
waypoints.push(ray.A);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
await updateCombatantDragRulerFlags(combat, combatant, dragRulerFlags);
|
return {_id: combatant._id, dragRulerFlags};
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getMovementHistory(token) {
|
export function getMovementHistory(token) {
|
||||||
@@ -81,5 +89,5 @@ export async function resetMovementHistory(combat, combatantId) {
|
|||||||
dragRulerFlags.passedWaypoints = undefined;
|
dragRulerFlags.passedWaypoints = undefined;
|
||||||
dragRulerFlags.trackedRound = undefined;
|
dragRulerFlags.trackedRound = undefined;
|
||||||
dragRulerFlags.rulerState = undefined;
|
dragRulerFlags.rulerState = undefined;
|
||||||
await updateCombatantDragRulerFlags(combat, combatant, dragRulerFlags);
|
await updateCombatantDragRulerFlags(combat, [{_id: combatantId, dragRulerFlags}]);
|
||||||
}
|
}
|
||||||
|
|||||||
+7
-5
@@ -5,13 +5,15 @@ Hooks.once("socketlib.ready", () => {
|
|||||||
socket.register("updateCombatantDragRulerFlags", _socketUpdateCombatantDragRulerFlags);
|
socket.register("updateCombatantDragRulerFlags", _socketUpdateCombatantDragRulerFlags);
|
||||||
});
|
});
|
||||||
|
|
||||||
export function updateCombatantDragRulerFlags(combat, combatant, flags) {
|
export function updateCombatantDragRulerFlags(combat, updates) {
|
||||||
const combatId = combat.id;
|
const combatId = combat.id;
|
||||||
const combatantId = combatant._id;
|
return socket.executeAsGM(_socketUpdateCombatantDragRulerFlags, combatId, updates);
|
||||||
return socket.executeAsGM(_socketUpdateCombatantDragRulerFlags, combatId, combatantId, flags);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async function _socketUpdateCombatantDragRulerFlags(combatId, combatantId, flags) {
|
async function _socketUpdateCombatantDragRulerFlags(combatId, updates) {
|
||||||
const combat = game.combats.get(combatId);
|
const combat = game.combats.get(combatId);
|
||||||
await combat.updateEmbeddedEntity("Combatant", {_id: combatantId, flags: {dragRuler: flags}}, {diff: false});
|
updates = updates.map(update => {
|
||||||
|
return {_id: update._id, flags: {dragRuler: update.dragRulerFlags}};
|
||||||
|
});
|
||||||
|
await combat.updateEmbeddedEntity("Combatant", updates, {diff: false});
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user