Skip to content

Commit

Permalink
feat: Can now specify initiative as static (close #74)
Browse files Browse the repository at this point in the history
  • Loading branch information
valentine195 committed May 9, 2023
1 parent c3ff665 commit b602f5c
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 4 deletions.
2 changes: 2 additions & 0 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ export interface CreatureState extends HomebrewCreature {
tempHP: number;
currentAC: number | string;
initiative: number;
static: boolean;
player: boolean;
xp: number;
active: boolean;
Expand Down Expand Up @@ -215,6 +216,7 @@ export interface HomebrewCreature {
hidden?: boolean;
friendly?: boolean;
active?: boolean;
static?: boolean;
"statblock-link"?: string;
}

Expand Down
4 changes: 3 additions & 1 deletion src/encounter/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ interface CreatureStats {
display?: string;
hidden: boolean;
friendly?: boolean;
static?: boolean;
}

export const equivalent = (
Expand All @@ -45,7 +46,8 @@ export const equivalent = (
equivalentModifiers(creature.modifier, existing.modifier) &&
creature.xp == existing.xp &&
creature.hidden == existing.hidden &&
creature.friendly == existing.friendly
creature.friendly == existing.friendly &&
creature.static == existing.static
);
};

Expand Down
3 changes: 3 additions & 0 deletions src/tracker/stores/tracker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ function createTracker() {
values.forEach((creature, _, arr) => {
const equiv = arr.filter((c) => equivalent(c, creature));
equiv.forEach((eq) => {
if (eq.static) return;
eq.initiative = Math.max(...equiv.map((i) => i.initiative));
});
});
Expand Down Expand Up @@ -572,6 +573,7 @@ function createTracker() {
roll: (plugin: InitiativeTracker) =>
updateAndSave((creatures) => {
for (let creature of creatures) {
if (creature.static) continue;
creature.initiative = plugin.getInitiativeValue(
creature.modifier
);
Expand All @@ -589,6 +591,7 @@ function createTracker() {
: creatures.filter((c) => c.player);
if (!state || state?.roll) {
for (let creature of creatures) {
if (creature.static && creature.initiative) continue;
creature.initiative = plugin.getInitiativeValue(
creature.modifier
);
Expand Down
9 changes: 9 additions & 0 deletions src/tracker/ui/create/Create.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,11 @@
.setValue(creature.hidden)
.onChange((v) => (creature.hidden = v));
};
const staticToggle = (div: HTMLDivElement) => {
new ToggleComponent(div)
.setValue(creature.static)
.onChange((v) => (creature.static = v));
};
const friendToggle = (div: HTMLDivElement) => {
new ToggleComponent(div)
.setValue(creature.friendly)
Expand Down Expand Up @@ -247,6 +252,10 @@
</div>

{#key creature}
<div>
<label for="add-mod">Static Initiative</label>
<div use:staticToggle />
</div>
<div>
<label for="add-mod">Hidden</label>
<div use:hideToggle />
Expand Down
3 changes: 1 addition & 2 deletions src/tracker/ui/create/Creator.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,7 @@
tracker.add(plugin, rollHP, ...creatures);
}
console.log(creature.player && creature.note);
if (creature.player && creature.path) {
if (creature?.player && creature?.path) {
const file = await plugin.app.vault.getAbstractFileByPath(
creature.path
);
Expand Down
6 changes: 5 additions & 1 deletion src/utils/creature.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export class Creature {
status: Set<Condition> = new Set();
marker: string;
initiative: number;
static: boolean = false;
source: string | string[];
id: string;
xp: number;
Expand Down Expand Up @@ -65,6 +66,7 @@ export class Creature {
"initiative" in creature
? (creature as Creature).initiative
: Number(initiative ?? 0);
this.static = creature.static ?? false;
this.modifier = Number(creature.modifier ?? 0);

this.current_ac = this.ac = creature.ac ?? undefined;
Expand Down Expand Up @@ -142,6 +144,7 @@ export class Creature {
*[Symbol.iterator]() {
yield this.name;
yield this.initiative;
yield this.static;
yield this.modifier;
yield this.max;
yield this.ac;
Expand Down Expand Up @@ -209,6 +212,7 @@ export class Creature {
name: this.name,
display: this.display,
initiative: this.initiative,
static: this.static,
modifier: this.modifier,
hp: this.max,
currentMaxHP: this.current_max,
Expand Down Expand Up @@ -240,7 +244,7 @@ export class Creature {
creature =
plugin.getPlayerByName(state.name) ??
new Creature(state, state.initiative);
creature.initiative = state.initiative;
creature.initiative = state.initiative;
} else {
creature = new Creature(state, state.initiative);
}
Expand Down

0 comments on commit b602f5c

Please sign in to comment.