Skip to content

Commit

Permalink
Add mergeExisting property to Aura RE (foundryvtt#9247)
Browse files Browse the repository at this point in the history
So there aren't a dozen kinetic auras on a single actor: if an existing aura has the same slug as a new one on the
actor, setting `mergeExisting` to true will merge the second aura into the first, combining effects and traits.
  • Loading branch information
stwlam committed Aug 11, 2023
1 parent 984380a commit 68f4da5
Showing 1 changed file with 23 additions and 12 deletions.
35 changes: 23 additions & 12 deletions src/module/rules/rule-element/aura.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { SAVE_TYPES } from "@actor/values.ts";
import { EffectTrait } from "@item/abstract-effect/data.ts";
import { PredicateField } from "@system/schema-data-fields.ts";
import { sluggify } from "@util";
import * as R from "remeda";
import type {
ArrayField,
BooleanField,
Expand Down Expand Up @@ -84,6 +85,7 @@ class AuraRuleElement extends RuleElementPF2e<AuraSchema> {
},
{ required: false, nullable: true, initial: null }
),
mergeExisting: new fields.BooleanField({ required: false, nullable: false, initial: true }),
};
}

Expand Down Expand Up @@ -120,7 +122,21 @@ class AuraRuleElement extends RuleElementPF2e<AuraSchema> {
}
}

this.actor.auras.set(this.slug, data);
const existing = this.actor.auras.get(this.slug);
if (existing && this.mergeExisting) {
existing.traits = R.uniq([...existing.traits, ...data.traits]).sort();
if (data.colors) existing.colors = mergeObject(existing.colors ?? {}, data.colors);
for (const effect of data.effects) {
const existingIndex = existing.effects.findIndex((e) => e.uuid === effect.uuid);
if (existingIndex !== -1) {
existing.effects.splice(existingIndex, 1, effect);
} else {
existing.effects.push(effect);
}
}
} else {
this.actor.auras.set(this.slug, data);
}
}
}

Expand Down Expand Up @@ -166,17 +182,12 @@ type AuraSchema = RuleElementSchema & {
* Custom border and fill colors for the aura: if omitted, the border color will be black, and the fill color the
* user's assigned color
*/
colors: SchemaField<
{
border: ColorField<false, true, true>;
fill: ColorField<false, true, true>;
},
AuraColors,
AuraColors,
false,
true,
true
>;
colors: SchemaField<{ border: ColorField; fill: ColorField }, AuraColors, AuraColors, false, true, true>;
/**
* If another aura with the same slug is already being emitted, merge this aura's data in with the other's,
* combining traits and effects as well as merging `colors` data.
*/
mergeExisting: BooleanField<boolean, boolean, false, false, true>;
};

type AuraEffectSchema = {
Expand Down

0 comments on commit 68f4da5

Please sign in to comment.