From ca11f0f92fe1e4a488402617911eff26fc0904f1 Mon Sep 17 00:00:00 2001 From: Dustlint Date: Sun, 15 Mar 2026 15:01:50 +0100 Subject: [PATCH] interaction map works --- .../chibi-behaviour/brains/aperio.brain.ts | 42 ++++++++++--------- src/app/logic/random.service.ts | 24 ++++++++--- 2 files changed, 41 insertions(+), 25 deletions(-) diff --git a/src/app/logic/chibi-behaviour/brains/aperio.brain.ts b/src/app/logic/chibi-behaviour/brains/aperio.brain.ts index f1f63f8..2b83ebb 100644 --- a/src/app/logic/chibi-behaviour/brains/aperio.brain.ts +++ b/src/app/logic/chibi-behaviour/brains/aperio.brain.ts @@ -1,12 +1,13 @@ import { Chibi } from "../../../types/chibi/chibi"; import { EChibiInteraction } from "../../../types/chibi/chibi-interaction"; -import { ChibiState, isState, STATE_AWAKE, STATE_EATING, STATE_IDLE, STATE_SLEEPING, STATES } from "../../../types/chibi/chibi-state"; +import { ChibiState, isState, STATE_EATING, STATE_GRUMBLING, STATE_IDLE, STATE_SLEEPING, STATES } from "../../../types/chibi/chibi-state"; import { EChibiStateName } from "../../../types/chibi/chibi-state-name"; import { Food } from "../../../types/food"; -import { RandomService } from "../../random.service"; +import { RandomOutcome, RandomService } from "../../random.service"; import { IBrain } from "../brain"; -type InteractionRecord = Partial ChibiState)>>; +type ResolutionFunction = (interaction: EChibiInteraction) => ChibiState; +type InteractionRecord = Partial>; const MAX_TIME_AWAKE: number = 3; @@ -16,11 +17,9 @@ export class Aperio implements IBrain { private chibi!: Chibi; private hasBeenAwakeFor: number = 0; private state!: ChibiState; - - public readonly defaultState: EChibiStateName = EChibiStateName.Sleeping; - public readonly interactionMap: { [key in EChibiStateName]: InteractionRecord | undefined } = { + private interactionMap: { [key in EChibiStateName]: InteractionRecord | undefined } = { [EChibiStateName.Sleeping]: { - [EChibiInteraction.WakeUp]: this.resolveSleepingInteraction, + [EChibiInteraction.WakeUp]: (interaction: EChibiInteraction): ChibiState => this.wakeUp(interaction), }, [EChibiStateName.Awake]: undefined, [EChibiStateName.Snoring]: undefined, @@ -36,6 +35,8 @@ export class Aperio implements IBrain { [EChibiStateName.Tired]: undefined } + public readonly defaultState: EChibiStateName = EChibiStateName.Sleeping; + //needed: some sort of legal interactions map //uses stateNames as keys //then submap the interactions @@ -45,6 +46,7 @@ export class Aperio implements IBrain { this.randomService = randomService; this.chibi = chibi; this.state = STATES[chibi.state]; + console.log(this.wakeUp); } exist(): void { @@ -62,10 +64,10 @@ export class Aperio implements IBrain { } resolveInteraction(interaction: EChibiInteraction, item?: Food): void { - if (isState(this.state, EChibiStateName.Sleeping)) { - this.state = this.resolveSleepingInteraction(interaction); - } else { - this.state = this.resolveAwakeInteraction(interaction, item); + // reassign state based on resolving map + const resolution: ResolutionFunction | undefined = this.interactionMap[this.state.name]?.[interaction]; + if (resolution) { + this.state = resolution(interaction); } this.chibi.state = this.state.name; } @@ -92,15 +94,17 @@ export class Aperio implements IBrain { after any interaction she has a 1/4 chance of going to sleep */ - private resolveSleepingInteraction(interaction: EChibiInteraction): ChibiState { - const willwakeUp = this.randomService.matchesChance(1, 128); - /* - 1/128 chance of waking her - 12/128 chance of her doing a grumble and going back to sleep - */ - console.log('will wake up', willwakeUp); - if (EChibiInteraction.WakeUp && willwakeUp) { + private wakeUp(interaction: EChibiInteraction): ChibiState { + const wakeUpOutcome: RandomOutcome = this.randomService.obtainRandom(128); + const willWakeUp: boolean = wakeUpOutcome.matchesChance(1); + const willGrumble: boolean = wakeUpOutcome.matchesChance(12, 1); + console.log(wakeUpOutcome.random); + console.log(willWakeUp); + console.log(willGrumble); + if (willWakeUp) { return STATE_IDLE; + } else if (willGrumble) { + return STATE_GRUMBLING; } return STATE_SLEEPING; } diff --git a/src/app/logic/random.service.ts b/src/app/logic/random.service.ts index 2205385..2dbfcc0 100644 --- a/src/app/logic/random.service.ts +++ b/src/app/logic/random.service.ts @@ -1,5 +1,21 @@ import { Injectable } from "@angular/core"; +export class RandomOutcome { + public readonly random: number; + + constructor(random: number) { + this.random = random; + } + + public matchesChance(chance: number, min: number = 0): boolean { + if (this.random >= min && this.random < min + chance) { + return true; + } + return false; + } +} + + @Injectable({ providedIn: 'root' }) export class RandomService { @@ -7,12 +23,8 @@ export class RandomService { return Math.floor(Math.random() * (max - min) + min); } - public matchesChance(chance: number, max: number): boolean { + public obtainRandom(max: number): RandomOutcome { const random = this.rollRandom(0, max); - console.log(random, chance); - if (random < chance) { - return true; - } - return false; + return new RandomOutcome(random); } } \ No newline at end of file