From 5858e2808d2c9aef8ff35be83992388b7dee5549 Mon Sep 17 00:00:00 2001 From: Dustlint Date: Fri, 27 Mar 2026 20:03:50 +0100 Subject: [PATCH] use interaction resolve matching --- src/app/dexie/entities/chibis.ts | 38 +++++++++-- .../chibi-behaviour/brains/aperio.brain.ts | 46 ++++++++----- .../interactions/interactions.component.html | 68 ++++++++++--------- .../interactions/interactions.component.ts | 1 + src/app/types/chibi/chibi.ts | 19 ++++-- 5 files changed, 110 insertions(+), 62 deletions(-) diff --git a/src/app/dexie/entities/chibis.ts b/src/app/dexie/entities/chibis.ts index 39e8ddd..1034dba 100644 --- a/src/app/dexie/entities/chibis.ts +++ b/src/app/dexie/entities/chibis.ts @@ -1,7 +1,14 @@ -import { Chibi, ChibiId } from "../../types/chibi/chibi"; +import { Chibi, ChibiId, ChibiStatBar } from "../../types/chibi/chibi"; import { EChibiName } from "../../types/chibi/chibi-name"; import { EChibiStateName } from "../../types/chibi/chibi-state-name"; +export const statBar = (current: number, max: number): ChibiStatBar => { + return { + current, + max + } +} + export const MAX_STAT_VALUE: number = 100000; export const CHIBIS: Chibi[] = [ @@ -12,11 +19,11 @@ export const CHIBIS: Chibi[] = [ spritePath: '', experience: 0, unlocked: true, - happyness: MAX_STAT_VALUE, - health: MAX_STAT_VALUE, - hunger: MAX_STAT_VALUE, - boredom: MAX_STAT_VALUE, - energy: MAX_STAT_VALUE, + happyness: statBar(MAX_STAT_VALUE, MAX_STAT_VALUE), + health: statBar(MAX_STAT_VALUE, MAX_STAT_VALUE), + hunger: statBar(MAX_STAT_VALUE, MAX_STAT_VALUE), + boredom: statBar(MAX_STAT_VALUE, MAX_STAT_VALUE), + energy: statBar(MAX_STAT_VALUE, MAX_STAT_VALUE), attentionSpan: 1000, recoveryRate: 1000, constitution: 1000, @@ -24,4 +31,23 @@ export const CHIBIS: Chibi[] = [ maintenanceCals: 0, state: EChibiStateName.Sleeping }, + { + id: '238df3e7-3003-4471-91f5-a7ecf3151e18' as ChibiId, + name: EChibiName.Caethya, + iconPath: '', + spritePath: '', + experience: 0, + unlocked: true, + happyness: statBar(MAX_STAT_VALUE, MAX_STAT_VALUE), + health: statBar(MAX_STAT_VALUE, MAX_STAT_VALUE), + hunger: statBar(MAX_STAT_VALUE, MAX_STAT_VALUE), + boredom: statBar(MAX_STAT_VALUE, MAX_STAT_VALUE), + energy: statBar(MAX_STAT_VALUE, MAX_STAT_VALUE), + attentionSpan: 1000, + recoveryRate: 1000, + constitution: 1000, + power: 1000, + maintenanceCals: 0, + state: EChibiStateName.Idle + }, ]; \ No newline at end of file diff --git a/src/app/logic/chibi-behaviour/brains/aperio.brain.ts b/src/app/logic/chibi-behaviour/brains/aperio.brain.ts index 3151c7e..b6b5349 100644 --- a/src/app/logic/chibi-behaviour/brains/aperio.brain.ts +++ b/src/app/logic/chibi-behaviour/brains/aperio.brain.ts @@ -6,6 +6,8 @@ import { Food } from "../../../types/food"; import { RandomOutcome, RandomService } from "../../random.service"; import { IBrain } from "../brain"; +export type InteractionMap = { [key in EChibiStateName]: InteractionRecord | undefined } + export type ChibiInteraction = { name: EChibiInteraction; payload: T; @@ -37,7 +39,7 @@ export class Aperio implements IBrain { private preferredFoods: string[] = []; private state: ChibiState = STATES[this.defaultState]; - private interactionMap: { [key in EChibiStateName]: InteractionRecord | undefined } = { + private interactionMap: InteractionMap = { [EChibiStateName.Sleeping]: { [EChibiInteraction.WakeUp]: (interaction: ChibiInteraction): ChibiState => this.wakeUp(interaction), }, @@ -65,6 +67,9 @@ export class Aperio implements IBrain { exist(): void { this.timeInCurrentState++; + //this sucks, resolve should be generic + //stuff into an existMap which contains the resolve functions + //then find correct function to execute the same way we do for interactions this.resolveAwake(); this.resolveSleeping(); this.resolveGrumbling(); @@ -73,11 +78,8 @@ export class Aperio implements IBrain { resolveInteraction(interaction: ChibiInteraction): void { this.timeSinceLastInteraction = 0; - const specificMatch: ResolutionFunction | undefined = this.interactionMap[this.state.name]?.[interaction.name]; - const awakeMatch: ResolutionFunction | undefined = this.interactionMap[EChibiStateName.Awake]?.[interaction.name]; - const asleepMatch: ResolutionFunction | undefined = this.interactionMap[EChibiStateName.Sleeping]?.[interaction.name]; - const resolution: ResolutionFunction | undefined = specificMatch || (this.isAwake() ? awakeMatch : asleepMatch); - console.log(`specific match: ${specificMatch}`); + const resolution = this.matchInteraction(this.interactionMap, interaction.name, STATES[this.chibi.state]); + console.log('resolution', resolution); console.log(`${this.chibi.name} should ${interaction.name} with payload: ${interaction.payload}`); if (resolution) { this.changeState(resolution(interaction)); @@ -88,6 +90,17 @@ export class Aperio implements IBrain { return isState(this.state, EChibiStateName.Awake); } + matchInteraction(map: InteractionMap, interaction: EChibiInteraction, state: ChibiState): ResolutionFunction | undefined { + const directMatch = map?.[state.name]?.[interaction]; + if (directMatch) { + return directMatch; + } + if (state.parent) { + return this.matchInteraction(map, interaction, state.parent); + } + return undefined; + } + // resolvers // functions which auitomatically resolve based on the existence cycle @@ -107,16 +120,6 @@ export class Aperio implements IBrain { } } - increaseHappyness(increase: number): void { - console.log(`${this.chibi.name} happyness increases.`); - this.chibi.happyness = this.chibi.happyness + increase; - } - - saddenedByNeglect(neglectTime: number, sadness: number): void { - console.log(`${this.chibi.name} happyness decreases.`); - this.chibi.happyness = this.chibi.happyness - (neglectTime * sadness); - } - resolveSleeping(): void { if (isState(this.state, EChibiStateName.Sleeping)) { console.log(`${this.chibi.name} is sleeping.`); @@ -147,6 +150,17 @@ export class Aperio implements IBrain { } } + + private increaseHappyness(increase: number): void { + console.log(`${this.chibi.name} happyness increases.`); + this.chibi.happyness.current = this.chibi.happyness.current + increase; + } + + private saddenedByNeglect(neglectTime: number, sadness: number): void { + console.log(`${this.chibi.name} happyness decreases.`); + this.chibi.happyness.current = this.chibi.happyness.current - (neglectTime * sadness); + } + private changeState(state: ChibiState): void { this.previousState = this.state.name; this.state = state; diff --git a/src/app/pages/interactions/interactions.component.html b/src/app/pages/interactions/interactions.component.html index 8f65de9..a85aaf6 100644 --- a/src/app/pages/interactions/interactions.component.html +++ b/src/app/pages/interactions/interactions.component.html @@ -14,43 +14,45 @@ }
- + @if (chibi()) { + - + - + -
- {{ - lang.game.actions.giveItem - }} - {{ - lang.game.actions.feed - }} - {{ - lang.game.actions.inviteVisitor - }} - @if (isAwake()) { - {{ - lang.game.actions.wakeUp +
+ {{ + lang.game.actions.giveItem }} - } @else { - {{ - lang.game.actions.putToSleep + {{ + lang.game.actions.feed }} - } -
+ {{ + lang.game.actions.inviteVisitor + }} + @if (isAwake()) { + {{ + lang.game.actions.putToSleep + }} + } @else { + {{ + lang.game.actions.wakeUp + }} + } +
+ }
diff --git a/src/app/pages/interactions/interactions.component.ts b/src/app/pages/interactions/interactions.component.ts index 047d2d2..99d17de 100644 --- a/src/app/pages/interactions/interactions.component.ts +++ b/src/app/pages/interactions/interactions.component.ts @@ -78,6 +78,7 @@ export class InteractionsComponent extends TranslateableComponent implements OnD this.brain.resolveInteraction({ name: interaction, payload: item } as ChibiInteraction); } + public openFoodPantry(): void { this.interactionMenuState.set(EInteractionMenuState.Pantry); } diff --git a/src/app/types/chibi/chibi.ts b/src/app/types/chibi/chibi.ts index 04e4922..a237bb4 100644 --- a/src/app/types/chibi/chibi.ts +++ b/src/app/types/chibi/chibi.ts @@ -8,7 +8,7 @@ export type ChibiPreview = { id: ChibiId; name: EChibiName; iconPath: string; - happyness: number; + happyness: ChibiStatBar; level: number; }; @@ -23,11 +23,11 @@ export type Chibi = { } & ChibiWellnessStats & ChibiTraits; export type ChibiWellnessStats = { - happyness: number; - health: number; - hunger: number; //feed them - boredom: number; //do activities to alleviate boredom - energy: number; //let them rest + happyness: ChibiStatBar; + health: ChibiStatBar; + hunger: ChibiStatBar; //feed them + boredom: ChibiStatBar; //do activities to alleviate boredom + energy: ChibiStatBar; //let them rest }; export type ChibiTraits = { @@ -36,4 +36,9 @@ export type ChibiTraits = { constitution: number; //how resistant they are to damage power: number; //how much damage they deal maintenanceCals: number; //how quickly they get hungry -}; \ No newline at end of file +}; + +export type ChibiStatBar = { + current: number; + max: number; +} \ No newline at end of file