interaction map works

This commit is contained in:
2026-03-15 15:01:50 +01:00
parent 57b753a6b5
commit ca11f0f92f
2 changed files with 41 additions and 25 deletions

View File

@@ -1,12 +1,13 @@
import { Chibi } from "../../../types/chibi/chibi"; import { Chibi } from "../../../types/chibi/chibi";
import { EChibiInteraction } from "../../../types/chibi/chibi-interaction"; 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 { EChibiStateName } from "../../../types/chibi/chibi-state-name";
import { Food } from "../../../types/food"; import { Food } from "../../../types/food";
import { RandomService } from "../../random.service"; import { RandomOutcome, RandomService } from "../../random.service";
import { IBrain } from "../brain"; import { IBrain } from "../brain";
type InteractionRecord = Partial<Record<EChibiInteraction, ((interaction: EChibiInteraction) => ChibiState)>>; type ResolutionFunction = (interaction: EChibiInteraction) => ChibiState;
type InteractionRecord = Partial<Record<EChibiInteraction, ResolutionFunction>>;
const MAX_TIME_AWAKE: number = 3; const MAX_TIME_AWAKE: number = 3;
@@ -16,11 +17,9 @@ export class Aperio implements IBrain {
private chibi!: Chibi; private chibi!: Chibi;
private hasBeenAwakeFor: number = 0; private hasBeenAwakeFor: number = 0;
private state!: ChibiState; private state!: ChibiState;
private interactionMap: { [key in EChibiStateName]: InteractionRecord | undefined } = {
public readonly defaultState: EChibiStateName = EChibiStateName.Sleeping;
public readonly interactionMap: { [key in EChibiStateName]: InteractionRecord | undefined } = {
[EChibiStateName.Sleeping]: { [EChibiStateName.Sleeping]: {
[EChibiInteraction.WakeUp]: this.resolveSleepingInteraction, [EChibiInteraction.WakeUp]: (interaction: EChibiInteraction): ChibiState => this.wakeUp(interaction),
}, },
[EChibiStateName.Awake]: undefined, [EChibiStateName.Awake]: undefined,
[EChibiStateName.Snoring]: undefined, [EChibiStateName.Snoring]: undefined,
@@ -36,6 +35,8 @@ export class Aperio implements IBrain {
[EChibiStateName.Tired]: undefined [EChibiStateName.Tired]: undefined
} }
public readonly defaultState: EChibiStateName = EChibiStateName.Sleeping;
//needed: some sort of legal interactions map //needed: some sort of legal interactions map
//uses stateNames as keys //uses stateNames as keys
//then submap the interactions //then submap the interactions
@@ -45,6 +46,7 @@ export class Aperio implements IBrain {
this.randomService = randomService; this.randomService = randomService;
this.chibi = chibi; this.chibi = chibi;
this.state = STATES[chibi.state]; this.state = STATES[chibi.state];
console.log(this.wakeUp);
} }
exist(): void { exist(): void {
@@ -62,10 +64,10 @@ export class Aperio implements IBrain {
} }
resolveInteraction(interaction: EChibiInteraction, item?: Food): void { resolveInteraction(interaction: EChibiInteraction, item?: Food): void {
if (isState(this.state, EChibiStateName.Sleeping)) { // reassign state based on resolving map
this.state = this.resolveSleepingInteraction(interaction); const resolution: ResolutionFunction | undefined = this.interactionMap[this.state.name]?.[interaction];
} else { if (resolution) {
this.state = this.resolveAwakeInteraction(interaction, item); this.state = resolution(interaction);
} }
this.chibi.state = this.state.name; 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 after any interaction she has a 1/4 chance of going to sleep
*/ */
private resolveSleepingInteraction(interaction: EChibiInteraction): ChibiState { private wakeUp(interaction: EChibiInteraction): ChibiState {
const willwakeUp = this.randomService.matchesChance(1, 128); const wakeUpOutcome: RandomOutcome = this.randomService.obtainRandom(128);
/* const willWakeUp: boolean = wakeUpOutcome.matchesChance(1);
1/128 chance of waking her const willGrumble: boolean = wakeUpOutcome.matchesChance(12, 1);
12/128 chance of her doing a grumble and going back to sleep console.log(wakeUpOutcome.random);
*/ console.log(willWakeUp);
console.log('will wake up', willwakeUp); console.log(willGrumble);
if (EChibiInteraction.WakeUp && willwakeUp) { if (willWakeUp) {
return STATE_IDLE; return STATE_IDLE;
} else if (willGrumble) {
return STATE_GRUMBLING;
} }
return STATE_SLEEPING; return STATE_SLEEPING;
} }

View File

@@ -1,5 +1,21 @@
import { Injectable } from "@angular/core"; 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' }) @Injectable({ providedIn: 'root' })
export class RandomService { export class RandomService {
@@ -7,12 +23,8 @@ export class RandomService {
return Math.floor(Math.random() * (max - min) + min); 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); const random = this.rollRandom(0, max);
console.log(random, chance); return new RandomOutcome(random);
if (random < chance) {
return true;
}
return false;
} }
} }