Files
forgotten-friends/src/app/logic/chibi-behaviour/brains/aperio.brain.ts
2026-03-15 15:01:50 +01:00

118 lines
4.4 KiB
TypeScript

import { Chibi } from "../../../types/chibi/chibi";
import { EChibiInteraction } from "../../../types/chibi/chibi-interaction";
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 { RandomOutcome, RandomService } from "../../random.service";
import { IBrain } from "../brain";
type ResolutionFunction = (interaction: EChibiInteraction) => ChibiState;
type InteractionRecord = Partial<Record<EChibiInteraction, ResolutionFunction>>;
const MAX_TIME_AWAKE: number = 3;
export class Aperio implements IBrain {
private randomService!: RandomService;
private chibi!: Chibi;
private hasBeenAwakeFor: number = 0;
private state!: ChibiState;
private interactionMap: { [key in EChibiStateName]: InteractionRecord | undefined } = {
[EChibiStateName.Sleeping]: {
[EChibiInteraction.WakeUp]: (interaction: EChibiInteraction): ChibiState => this.wakeUp(interaction),
},
[EChibiStateName.Awake]: undefined,
[EChibiStateName.Snoring]: undefined,
[EChibiStateName.Nightmare]: undefined,
[EChibiStateName.Grumbling]: undefined,
[EChibiStateName.Bored]: undefined,
[EChibiStateName.Idle]: undefined,
[EChibiStateName.Exited]: undefined,
[EChibiStateName.Fighting]: undefined,
[EChibiStateName.Eating]: undefined,
[EChibiStateName.Hungry]: undefined,
[EChibiStateName.Depressed]: undefined,
[EChibiStateName.Tired]: undefined
}
public readonly defaultState: EChibiStateName = EChibiStateName.Sleeping;
//needed: some sort of legal interactions map
//uses stateNames as keys
//then submap the interactions
//behind every interaction is a dedicated function
init(chibi: Chibi, randomService: RandomService): void {
this.randomService = randomService;
this.chibi = chibi;
this.state = STATES[chibi.state];
console.log(this.wakeUp);
}
exist(): void {
console.log('Brain is braining');
if (isState(this.state, EChibiStateName.Awake)) {
console.log('is awake');
this.hasBeenAwakeFor++;
if (this.hasBeenAwakeFor > MAX_TIME_AWAKE) {
this.state = STATE_SLEEPING;
this.chibi.state = EChibiStateName.Sleeping;
this.hasBeenAwakeFor = 0;
}
}
this.chibi.state = this.state.name;
}
resolveInteraction(interaction: EChibiInteraction, item?: Food): void {
// 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;
}
/*
pear mostly sleeps
visiting caethya will wake her up and go into lovey-dovey mode
she has a 1/2048 chance of waking up on her own
pears happyness goes up when she sleeps
pear never gets hungry
but she accepts foods she likes, which raise her happyness
as long as the same food is not offered thrice in a row (except banana split)
pears energy never goes down
pear will accept fights and enjoys them
pear will accept walks and enjoys them
pear will get sad if she is awake and you do not interact with her
pear gets offended if you suggest she go to sleep
after any interaction she has a 1/4 chance of going to sleep
*/
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;
}
private resolveAwakeInteraction(interaction: EChibiInteraction, item?: Food): ChibiState {
if (EChibiInteraction.Feed) {
return STATE_EATING;
}
return STATE_IDLE;
}
};