Skip to content

Commit

Permalink
Merge branch 'feat/max-hp-damage' of https://github.com/SondreElg/obs…
Browse files Browse the repository at this point in the history
…idian-initiative-tracker into SondreElg-feat/max-hp-damage
  • Loading branch information
valentine195 committed Apr 25, 2023
2 parents 4af8363 + c7997d8 commit 38ebd91
Show file tree
Hide file tree
Showing 9 changed files with 89 additions and 50 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,8 @@ Once all of the creatures in a given combat have been added, initiatives can be

### HP

Creatures can take damage, be healed or gain temporary HP by clicking on their HP.
Creatures can take damage, be healed, gain temporary HP or take max HP damage by clicking on their HP.
The temp HP and max HP modifiers also support the "-" symbol to remove temp HP and restore/gain max HP.

#### Rolling HP

Expand Down
2 changes: 2 additions & 0 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ export interface InitiativeViewState {
export interface CreatureState extends HomebrewCreature {
status: string[];
enabled: boolean;
currentMaxHP: number;
currentHP: number;
tempHP: number;
initiative: number;
Expand Down Expand Up @@ -202,6 +203,7 @@ export interface UpdateLogMessage {
hp: number | null;
temp: boolean;
status: string[] | null;
max: boolean;
saved: boolean;
unc: boolean;
}
Expand Down
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"author": "",
"license": "MIT",
"devDependencies": {
"@popperjs/core": "^2.9.2",
"@popperjs/core": "^2.11.7",
"@tsconfig/svelte": "^2.0.1",
"@types/jest": "^29.5.0",
"@types/node": "^14.14.37",
Expand Down
103 changes: 68 additions & 35 deletions src/tracker/stores/tracker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,20 +134,20 @@ function createTracker() {
// Handle overflow healing according to settings
if (
change.hp > 0 &&
change.hp + creature.hp > creature.max
change.hp + creature.hp > creature.current_max
) {
switch (_settings.hpOverflow) {
case OVERFLOW_TYPE.ignore:
change.hp = Math.max(
creature.max - creature.hp,
creature.current_max - creature.hp,
0
);
break;
case OVERFLOW_TYPE.temp:
// Gives temp a value, such that it will be set later
change.temp =
change.hp -
Math.min(creature.max - creature.hp, 0);
Math.min(creature.current_max - creature.hp, 0);
change.hp -= change.temp;
break;
case OVERFLOW_TYPE.current:
Expand All @@ -163,10 +163,10 @@ function createTracker() {
}
}
if (change.max) {
if (creature.hp == creature.max) {
creature.hp = Number(change.max);
creature.current_max = Math.max(0, creature.current_max + change.max);
if (creature.hp >= creature.current_max && _settings.hpOverflow !== OVERFLOW_TYPE.current) {
creature.hp = creature.current_max;
}
creature.max = Number(change.max);
}
if (change.ac) {
creature.ac = change.ac;
Expand All @@ -176,10 +176,18 @@ function createTracker() {
if (_settings.additiveTemp) {
baseline = creature.temp;
}
creature.temp = Math.max(
creature.temp,
baseline + change.temp
);
if (change.temp > 0) {
creature.temp = Math.max(
creature.temp,
baseline + change.temp
);
}
else {
creature.temp = Math.max(
0,
creature.temp + change.temp
);
};
}
if (change.marker) {
creature.marker = change.marker;
Expand Down Expand Up @@ -320,6 +328,7 @@ function createTracker() {
name: name.join(" "),
hp: null,
temp: false,
max: false,
status: null,
saved: false,
unc: false
Expand All @@ -331,20 +340,22 @@ function createTracker() {
message.temp = true;
change.temp = toAdd;
} else {
let toAdd = Number(toAddString);
const maxHpDamage = toAddString.charAt(0) === "m";
let toAdd = Number(toAddString.slice(+maxHpDamage));
toAdd =
-1 *
Math.sign(toAdd) *
Math.max(Math.abs(toAdd) * modifier, 1);
toAdd = roundHalf ? Math.trunc(toAdd) : toAdd;
message.hp = toAdd;
if (
toAdd < 0 &&
creature.hp + creature.temp + toAdd <= 0
) {
message.unc = true;
if (maxHpDamage) {
message.max = true;
change.max = toAdd;
}
change.hp = toAdd;
if (creature.hp <= 0) {
message.unc = true;
}
}
if (statuses.length) {
message.status = statuses.map((s) => s.name);
Expand Down Expand Up @@ -489,7 +500,7 @@ function createTracker() {
let roller = plugin.getRoller(
creature.hit_dice
) as StackRoller;
creature.hp = creature.max = roller.rollSync();
creature.hp = creature.max = creature.current_max = roller.rollSync();
}
}
creatures.push(...items);
Expand Down Expand Up @@ -554,7 +565,7 @@ function createTracker() {
let roller = plugin.getRoller(
creature.hit_dice
) as StackRoller;
creature.hp = creature.max = roller.rollSync();
creature.hp = creature.max = creature.current_max = roller.rollSync();
}
}
}
Expand All @@ -573,7 +584,7 @@ function createTracker() {
reset: () =>
updateAndSave((creatures) => {
for (let creature of creatures) {
creature.hp = creature.max;
creature.hp = creature.current_max = creature.max;
creature.enabled = true;
creature.hidden = false;
creature.status.clear();
Expand All @@ -595,22 +606,44 @@ function createTracker() {
message.name
} gained ${message.hp.toString()} temporary HP`
);
} else if (message.hp < 0) {
perCreature.push(
`${message.name} took ${(
-1 * message.hp
).toString()} damage${
message.unc
? " and was knocked unconscious"
: ""
}`
);
} else if (message.hp > 0) {
perCreature.push(
`${
message.name
} was healed for ${message.hp.toString()} HP`
);
} else if (!message.max) {
if (message.hp < 0) {
perCreature.push(
`${message.name} took ${(
-1 * message.hp
).toString()} damage${
message.unc
? " and was knocked unconscious"
: ""
}`
);
} else if (message.hp > 0) {
perCreature.push(
`${
message.name
} was healed for ${message.hp.toString()} HP`
);
}
}
else if (message.max) {
if (message.hp < 0) {
perCreature.push(
`${message.name} took ${(
-1 * message.hp
).toString()} max HP damage${
message.unc
? " and died"
: ""
}`
);
}
else {
perCreature.push(
`${message.name} gained ${(
-1 * message.hp
).toString()} max HP`
);
}
}
}
if (message.status) {
Expand Down
6 changes: 3 additions & 3 deletions src/tracker/ui/Updating.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -91,13 +91,13 @@
<div class="hp-status">
{#if plugin.data.beginnerTips}
<small class="label">
Apply damage, healing(-) or temp HP(t)
Apply damage, (m)max HP damage, (-)healing or (t)temp HP
</small>
{/if}
<div class="input">
<tag
use:hpIcon
aria-label="Apply damage, healing(-) or temp HP(t)"
aria-label="Apply damage, (m)max HP damage, (-)healing or (t)temp HP"
style="margin: 0 0.2rem 0 0.7rem"
/>
<input
Expand All @@ -112,7 +112,7 @@
return;
}
if (
!/^(t?-?\d*\.?\d*(Backspace|Delete|Arrow\w+)?)$/.test(
!/^((t|m)?-?\d*\.?\d*(Backspace|Delete|Arrow\w+)?)$/.test(
this.value + evt.key
)
) {
Expand Down
2 changes: 1 addition & 1 deletion src/tracker/ui/create/Creator.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
if (!$adding.length && !isEditing) return;
if (isEditing) {
if ($editing.hp != creature.max) {
creature.max = $editing.hp;
creature.max = creature.current_max = $editing.hp;
}
tracker.replace(creature, $editing);
} else {
Expand Down
2 changes: 1 addition & 1 deletion src/tracker/ui/creatures/Table.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@
class:friendly={creature.friendly}
animate:flip={{ duration: flipDurationMs }}
data-hp={creature.hp}
data-hp-max={creature.max}
data-hp-max={creature.current_max}
data-hp-percent={Math.round(
((creature.hp ?? 0) / creature.max) * 100 ?? 0
)}
Expand Down
11 changes: 7 additions & 4 deletions src/utils/creature.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export class Creature {
enabled: boolean = true;
hidden: boolean = false;
max: number;
current_max: number;
level: number;
player: boolean;
status: Set<Condition> = new Set();
Expand Down Expand Up @@ -62,7 +63,7 @@ export class Creature {
: Number(initiative ?? 0);
this.modifier = Number(creature.modifier ?? 0);

this.max = creature.hp ? Number(creature.hp) : undefined;
this.max = this.current_max = creature.hp ? Number(creature.hp) : 0;
this.ac = creature.ac ?? undefined;
this.note = creature.note;
this.level = creature.level;
Expand Down Expand Up @@ -96,14 +97,14 @@ export class Creature {
}
}
get hpDisplay() {
if (this.max) {
if (this.current_max) {
const tempMods =
this.temp > 0
? `aria-label="Temp HP: ${this.temp}" style="font-weight:bold"`
: "";
return `
<span ${tempMods}>${this.hp + this.temp}</span><span>/${
this.max
this.current_max
}</span>
`;
}
Expand Down Expand Up @@ -181,7 +182,7 @@ export class Creature {
this.name = creature.name;
this.modifier = Number(creature.modifier ?? 0);

this.max = creature.hp ? Number(creature.hp) : undefined;
this.current_max = this.max = creature.hp ? Number(creature.hp) : 0;

if (this.hp > this.max) this.hp = this.max;

Expand All @@ -206,6 +207,7 @@ export class Creature {
initiative: this.initiative - this.modifier,
modifier: this.modifier,
hp: this.max,
currentMaxHP: this.current_max,
ac: this.ac,
note: this.note,
id: this.id,
Expand All @@ -230,6 +232,7 @@ export class Creature {
creature.enabled = state.enabled;

creature.temp = state.tempHP ? state.tempHP : 0;
creature.current_max = state.currentMaxHP;
creature.hp = state.currentHP;
let statuses: Condition[] = [];
for (const status of state.status) {
Expand Down

0 comments on commit 38ebd91

Please sign in to comment.