diff --git a/src/Character.ts b/src/Character.ts index 77b1000..c97ace8 100644 --- a/src/Character.ts +++ b/src/Character.ts @@ -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), + ); + } } diff --git a/src/Health.ts b/src/Health.ts index 6b6dd3d..5dbeda1 100644 --- a/src/Health.ts +++ b/src/Health.ts @@ -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)); + } } diff --git a/transcripts/yagni-in-agents-md.html b/transcripts/yagni-in-agents-md.html index 0280fa8..62e1981 100644 --- a/transcripts/yagni-in-agents-md.html +++ b/transcripts/yagni-in-agents-md.html @@ -1,3595 +1,12415 @@ - + -
- - -