use interaction resolve matching
This commit is contained in:
@@ -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<T = null> = {
|
||||
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;
|
||||
|
||||
Reference in New Issue
Block a user