forgot to mention the story
This commit is contained in:
parent
4cdb048dfc
commit
39983a30c9
@ -5,7 +5,7 @@
|
||||
* State is encapsulated in a CharacterState record type.
|
||||
*/
|
||||
import { Health } from './Health.ts';
|
||||
import type { Level } from './Level.ts';
|
||||
import { Level } from './Level.ts';
|
||||
import type { Status } from './Status.ts';
|
||||
import { StatusAlive, StatusDead } from './Status.ts';
|
||||
import { CharacterState } from './CharacterState.ts';
|
||||
@ -81,4 +81,21 @@ export class Character {
|
||||
new CharacterState(target.name, newHealth, newStatus, target.level, target.factions),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Heal self by the given amount. Returns a new Character with updated state.
|
||||
* Dead characters cannot heal. Healing is capped at max health for level.
|
||||
*/
|
||||
healSelf(amount: number): Character {
|
||||
// Dead characters cannot heal
|
||||
if (this.status.kind === 'dead') return this;
|
||||
// Negative heal is invalid
|
||||
if (amount < 0) throw new Error(`Heal amount must be non-negative, got ${amount}`);
|
||||
// Increase health, capped at level-based maximum
|
||||
const maxHealth = Level.maxHealthForLevel(this.level.value);
|
||||
const newHealth = this.health.add(amount, maxHealth);
|
||||
return new Character(
|
||||
new CharacterState(this.name, newHealth, this.status, this.level, this.factions),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@ -26,4 +26,9 @@ export class Health {
|
||||
sub(amount: number): Health {
|
||||
return Health.create(Math.max(0, this.#value - amount));
|
||||
}
|
||||
|
||||
/** Add healing — capped at the given maximum. */
|
||||
add(amount: number, max: number): Health {
|
||||
return Health.create(Math.min(this.#value + amount, max));
|
||||
}
|
||||
}
|
||||
|
||||
File diff suppressed because one or more lines are too long
Loading…
x
Reference in New Issue
Block a user