Skip to content

Commit

Permalink
Make single token update call in PartyClownCar##retrieve (foundryvt…
Browse files Browse the repository at this point in the history
  • Loading branch information
stwlam committed Sep 3, 2023
1 parent 44586da commit 225511c
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 29 deletions.
7 changes: 4 additions & 3 deletions src/module/actor/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,13 @@ import { extractEphemeralEffects, processPreUpdateActorHooks } from "@module/rul
import { RuleElementSynthetics } from "@module/rules/index.ts";
import { RuleElementPF2e } from "@module/rules/rule-element/base.ts";
import { RollOptionRuleElement } from "@module/rules/rule-element/roll-option.ts";
import { type ScenePF2e, TokenDocumentPF2e } from "@scene";
import type { ScenePF2e } from "@scene/document.ts";
import { TokenDocumentPF2e } from "@scene/token-document/document.ts";
import { IWRApplicationData, applyIWR } from "@system/damage/iwr.ts";
import { DamageType } from "@system/damage/types.ts";
import { CheckDC } from "@system/degree-of-success.ts";
import { ArmorStatistic } from "@system/statistic/armor-class.ts";
import { Statistic, StatisticCheck, StatisticDifficultyClass } from "@system/statistic/index.ts";
import type { ArmorStatistic } from "@system/statistic/armor-class.ts";
import type { Statistic, StatisticCheck, StatisticDifficultyClass } from "@system/statistic/index.ts";
import { EnrichmentOptionsPF2e, TextEditorPF2e } from "@system/text-editor.ts";
import { ErrorPF2e, localizer, objectHasKey, setHasElement, sluggify, traitSlugToObject, tupleHasValue } from "@util";
import * as R from "remeda";
Expand Down
3 changes: 2 additions & 1 deletion src/module/apps/effects-panel.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { AbstractEffectPF2e, AfflictionPF2e, ConditionPF2e, EffectPF2e } from "@item";
import { EffectExpiryType } from "@item/effect/data.ts";
import { ActorPF2e, TokenDocumentPF2e } from "@module/documents.ts";
import { ActorPF2e } from "@actor";
import { InlineRollLinks } from "@scripts/ui/inline-roll-links.ts";
import { htmlQuery, htmlQueryAll } from "@util";
import type { TokenDocumentPF2e } from "@scene/token-document/document.ts";

export class EffectsPanel extends Application {
private get token(): TokenDocumentPF2e | null {
Expand Down
9 changes: 5 additions & 4 deletions src/module/canvas/token/object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -343,14 +343,15 @@ class TokenPF2e<TDocument extends TokenDocumentPF2e = TokenDocumentPF2e> extends
if (options?.spin) {
let attributeAdded = false;
const currentRotation = this.document.rotation;
const rotationAngle = this.x <= this.document.x ? 360 : -360;
options.ontick = (_frame, data) => {
if (!attributeAdded && data.attributes.length > 0) {
const duration = options.duration ?? 6;
const duration = (data.duration ?? 1000) / 1000;
data.attributes.push({
attribute: "rotation",
parent: data.attributes[0].parent,
from: currentRotation,
to: currentRotation + duration * (options.spin === "right" ? 360 : -360),
to: currentRotation + duration * rotationAngle,
delta: data.attributes[0].delta,
});
attributeAdded = true;
Expand Down Expand Up @@ -469,8 +470,8 @@ type ShowFloatyEffectParams =
| { update: NumericFloatyEffect }
| { delete: NumericFloatyEffect };

interface TokenAnimationOptionsPF2e<TObject extends TokenPF2e> extends TokenAnimationOptions<TObject> {
spin?: "left" | "right";
interface TokenAnimationOptionsPF2e<TObject extends TokenPF2e = TokenPF2e> extends TokenAnimationOptions<TObject> {
spin?: boolean;
}

export { ShowFloatyEffectParams, TokenAnimationOptionsPF2e, TokenPF2e };
2 changes: 1 addition & 1 deletion src/module/documents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ export { UserPF2e } from "./user/document.ts";
export { ActiveEffectPF2e } from "./active-effect.ts";

// Canvas documents
export { AmbientLightDocumentPF2e, MeasuredTemplateDocumentPF2e, ScenePF2e, TokenDocumentPF2e } from "@scene";
export { ScenePF2e } from "@scene";
13 changes: 4 additions & 9 deletions src/module/scene/token-document/clown-car.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { PartyPF2e } from "@actor";
import { getAreaSquares } from "@module/canvas/token/aura/util.ts";
import { TokenAnimationOptionsPF2e } from "@module/canvas/token/object.ts";
import type { ScenePF2e, TokenDocumentPF2e } from "@scene";
import { SceneTokenModificationContextPF2e } from "@scene/token-document/document.ts";
import { ErrorPF2e } from "@util";
import * as R from "remeda";

Expand Down Expand Up @@ -39,14 +39,9 @@ class PartyClownCar {
/** Retrieve all party-member tokens, animating their movement before finally deleting them. */
async #retrieve(): Promise<void> {
const tokens = this.memberTokens;

await Promise.all(
tokens.map((token) => {
const spin = token.x > this.token.x ? "left" : "right";
const context: SceneTokenModificationContextPF2e = token.object?.mesh ? { animation: { spin } } : {};
return token.update({ x: this.token.x, y: this.token.y }, context);
})
);
const updates = tokens.map((t) => ({ _id: t.id, ...R.pick(this.token, ["x", "y"]) }));
const animation: TokenAnimationOptionsPF2e = { spin: true };
await this.scene.updateEmbeddedDocuments("Token", updates, { animation });

await Promise.all(
tokens.map(async (token) => {
Expand Down
11 changes: 3 additions & 8 deletions src/module/scene/token-document/document.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { ActorPF2e } from "@actor";
import { PrototypeTokenPF2e } from "@actor/data/base.ts";
import type { TokenPF2e } from "@module/canvas/index.ts";
import { TokenAnimationOptionsPF2e } from "@module/canvas/token/object.ts";
import { ChatMessagePF2e } from "@module/chat-message/document.ts";
import type { CombatantPF2e, EncounterPF2e } from "@module/encounter/index.ts";
import { LightLevels } from "@scene/data.ts";
import type { ScenePF2e } from "@scene/index.ts";
import { LightLevels } from "../data.ts";
import type { ScenePF2e } from "../document.ts";
import { objectHasKey, sluggify } from "@util";
import type { ActorDeltaPF2e } from "./actor-delta.ts";
import { TokenAura } from "./aura/index.ts";
Expand Down Expand Up @@ -480,8 +479,4 @@ interface TokenDocumentPF2e<TParent extends ScenePF2e | null = ScenePF2e | null>
delta: ActorDeltaPF2e<this> | null;
}

interface SceneTokenModificationContextPF2e extends SceneTokenModificationContext<ScenePF2e> {
animation?: TokenAnimationOptionsPF2e<TokenPF2e>;
}

export { SceneTokenModificationContextPF2e, TokenDocumentPF2e };
export { TokenDocumentPF2e };
5 changes: 3 additions & 2 deletions tests/mocks/actor.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import type { ActiveEffectPF2e, ActorPF2e, ItemPF2e, ScenePF2e, TokenDocumentPF2e } from "@module/documents.ts";
import { ActorSourcePF2e } from "@actor/data/index.ts";
import { ActorSystemSource } from "@actor/data/base.ts";
import { ActorSourcePF2e } from "@actor/data/index.ts";
import { ItemSourcePF2e } from "@item/data/index.ts";
import type { ActiveEffectPF2e, ActorPF2e, ItemPF2e, ScenePF2e } from "@module/documents.ts";
import type { TokenDocumentPF2e } from "@scene/token-document/document.ts";
import { MockCollection } from "./collection.ts";
import { MockItem } from "./item.ts";

Expand Down
3 changes: 2 additions & 1 deletion tests/mocks/token.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { ActorPF2e, ScenePF2e, TokenDocumentPF2e } from "@module/documents.js";
import type { ActorPF2e, ScenePF2e } from "@module/documents.ts";
import type { TokenDocumentPF2e } from "@scene/token-document/document.ts";

export class MockToken {
actor: ActorPF2e | null;
Expand Down

0 comments on commit 225511c

Please sign in to comment.