Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move static methods in CombatantPF2e to top of class #15995

Merged
merged 1 commit into from
Aug 11, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 47 additions & 47 deletions src/module/encounter/combatant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,33 +11,32 @@ class CombatantPF2e<
/** Has this document completed `DataModel` initialization? */
declare initialized: boolean;

get encounter(): TParent {
return this.parent;
}

/** The round this combatant last had a turn */
get roundOfLastTurn(): number | null {
return this.flags.pf2e.roundOfLastTurn;
}

/** Can the user see this combatant's name? */
get playersCanSeeName(): boolean {
return !!this.token?.playersCanSeeName;
}

overridePriority(initiative: number): number | null {
return this.flags.pf2e.overridePriority[initiative] ?? null;
}
static override async createDocuments<TDocument extends foundry.abstract.Document>(
this: ConstructorOf<TDocument>,
data?: (TDocument | PreCreate<TDocument["_source"]>)[],
operation?: Partial<DatabaseCreateOperation<TDocument["parent"]>>,
): Promise<TDocument[]>;
static override async createDocuments(
data: (CombatantPF2e | PreCreate<foundry.documents.CombatantSource>)[] = [],
operation: Partial<DatabaseCreateOperation<EncounterPF2e>> = {},
): Promise<Combatant<EncounterPF2e, TokenDocument<Scene | null> | null>[]> {
type DataType = (typeof data)[number];
const entries: { token: TokenDocumentPF2e | null; data: DataType }[] = data.map((d) => {
const scene = d.sceneId ? game.scenes.get(d.sceneId) : operation.parent?.scene;
const token = scene?.tokens.get(d.tokenId ?? "") || null;
return { token, data: d };
});

hasHigherInitiative(
this: RolledCombatant<NonNullable<TParent>>,
{ than }: { than: RolledCombatant<NonNullable<TParent>> },
): boolean {
if (this.parent.id !== than.parent.id) {
throw ErrorPF2e("The initiative of Combatants from different combats cannot be compared");
// Party actors add their members to initiative instead of themselves
const tokens = entries.map((e) => e.token);
for (const token of tokens) {
if (token?.actor?.isOfType("party")) {
await token?.actor.addToCombat({ combat: operation.parent });
}
}

return this.parent.getCombatantWithHigherInit(this, than) === this;
const nonPartyData = entries.filter((e) => !e.token?.actor?.isOfType("party")).map((e) => e.data);
return super.createDocuments<Combatant<EncounterPF2e, TokenDocument<Scene | null>>>(nonPartyData, operation);
}

/** Get the active Combatant for the given actor, creating one if necessary */
Expand Down Expand Up @@ -74,32 +73,33 @@ class CombatantPF2e<
return null;
}

static override async createDocuments<TDocument extends foundry.abstract.Document>(
this: ConstructorOf<TDocument>,
data?: (TDocument | PreCreate<TDocument["_source"]>)[],
operation?: Partial<DatabaseCreateOperation<TDocument["parent"]>>,
): Promise<TDocument[]>;
static override async createDocuments(
data: (CombatantPF2e | PreCreate<foundry.documents.CombatantSource>)[] = [],
operation: Partial<DatabaseCreateOperation<EncounterPF2e>> = {},
): Promise<Combatant<EncounterPF2e, TokenDocument<Scene | null> | null>[]> {
type DataType = (typeof data)[number];
const entries: { token: TokenDocumentPF2e | null; data: DataType }[] = data.map((d) => {
const scene = d.sceneId ? game.scenes.get(d.sceneId) : operation.parent?.scene;
const token = scene?.tokens.get(d.tokenId ?? "") || null;
return { token, data: d };
});
get encounter(): TParent {
return this.parent;
}

// Party actors add their members to initiative instead of themselves
const tokens = entries.map((e) => e.token);
for (const token of tokens) {
if (token?.actor?.isOfType("party")) {
await token?.actor.addToCombat({ combat: operation.parent });
}
/** The round this combatant last had a turn */
get roundOfLastTurn(): number | null {
return this.flags.pf2e.roundOfLastTurn;
}

/** Can the user see this combatant's name? */
get playersCanSeeName(): boolean {
return !!this.token?.playersCanSeeName;
}

overridePriority(initiative: number): number | null {
return this.flags.pf2e.overridePriority[initiative] ?? null;
}

hasHigherInitiative(
this: RolledCombatant<NonNullable<TParent>>,
{ than }: { than: RolledCombatant<NonNullable<TParent>> },
): boolean {
if (this.parent.id !== than.parent.id) {
throw ErrorPF2e("The initiative of Combatants from different combats cannot be compared");
}

const nonPartyData = entries.filter((e) => !e.token?.actor?.isOfType("party")).map((e) => e.data);
return super.createDocuments<Combatant<EncounterPF2e, TokenDocument<Scene | null>>>(nonPartyData, operation);
return this.parent.getCombatantWithHigherInit(this, than) === this;
}

async startTurn(): Promise<void> {
Expand Down
Loading