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 { 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<Record<EChibiInteraction, ((interaction: EChibiInteraction) => ChibiState)>>;
type ResolutionFunction = (interaction: EChibiInteraction) => ChibiState;
type InteractionRecord = Partial<Record<EChibiInteraction, ResolutionFunction>>;
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;
}

View File

@@ -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);
}
}