auto resolve existance
This commit is contained in:
@@ -13,8 +13,11 @@ export type ChibiInteraction<T = null> = {
|
||||
payload: T;
|
||||
}
|
||||
|
||||
type ResolutionFunction = (interaction: ChibiInteraction<any>) => ChibiState;
|
||||
type InteractionRecord = Partial<Record<EChibiInteraction, ResolutionFunction>>;
|
||||
type InteractionResolutionFunction = (interaction: ChibiInteraction<any>) => ChibiState;
|
||||
type InteractionRecord = Partial<Record<EChibiInteraction, InteractionResolutionFunction>>;
|
||||
|
||||
type ResolutionFunction = () => ChibiState | undefined;
|
||||
type ResolutionMap = { [key in EChibiStateName]: ResolutionFunction | undefined }
|
||||
|
||||
const MAX_TIME_AWAKE: number = 10;
|
||||
|
||||
@@ -38,7 +41,6 @@ export class Aperio implements IBrain {
|
||||
private lastConsumedFoods: string[] = [];
|
||||
private preferredFoods: string[] = [];
|
||||
|
||||
private state: ChibiState = STATES[this.defaultState];
|
||||
private interactionMap: InteractionMap = {
|
||||
[EChibiStateName.Sleeping]: {
|
||||
[EChibiInteraction.WakeUp]: (interaction: ChibiInteraction): ChibiState => this.wakeUp(interaction),
|
||||
@@ -58,27 +60,45 @@ export class Aperio implements IBrain {
|
||||
[EChibiStateName.Depressed]: undefined,
|
||||
[EChibiStateName.Tired]: undefined
|
||||
}
|
||||
private resolutionMap: ResolutionMap = {
|
||||
[EChibiStateName.Sleeping]: (): ChibiState | undefined => this.resolveSleeping(),
|
||||
[EChibiStateName.Awake]: (): ChibiState | undefined => this.resolveAwake(),
|
||||
[EChibiStateName.Snoring]: undefined,
|
||||
[EChibiStateName.Nightmare]: undefined,
|
||||
[EChibiStateName.Grumbling]: (): ChibiState | undefined => this.resolveGrumbling(),
|
||||
[EChibiStateName.Bored]: undefined,
|
||||
[EChibiStateName.Idle]: undefined,
|
||||
[EChibiStateName.Exited]: undefined,
|
||||
[EChibiStateName.Fighting]: undefined,
|
||||
[EChibiStateName.Eating]: (): ChibiState | undefined => this.resolveEating(),
|
||||
[EChibiStateName.Hungry]: undefined,
|
||||
[EChibiStateName.Depressed]: undefined,
|
||||
[EChibiStateName.Tired]: undefined
|
||||
}
|
||||
|
||||
get state(): ChibiState {
|
||||
return STATES[this.chibi.currentStateName];
|
||||
}
|
||||
|
||||
init(chibi: Chibi, randomService: RandomService): void {
|
||||
this.randomService = randomService;
|
||||
this.chibi = chibi;
|
||||
this.state = STATES[chibi.state];
|
||||
}
|
||||
|
||||
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();
|
||||
this.resolveEating();
|
||||
const resolution = this.matchResolution(this.resolutionMap, STATES[this.chibi.currentStateName]);
|
||||
if (resolution) {
|
||||
const newState = resolution();
|
||||
if (newState && newState != STATES[this.chibi.currentStateName]) {
|
||||
this.changeState(newState);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
resolveInteraction(interaction: ChibiInteraction): void {
|
||||
this.timeSinceLastInteraction = 0;
|
||||
const resolution = this.matchInteraction(this.interactionMap, interaction.name, STATES[this.chibi.state]);
|
||||
const resolution = this.matchInteraction(this.interactionMap, interaction.name, STATES[this.chibi.currentStateName]);
|
||||
console.log('resolution', resolution);
|
||||
console.log(`${this.chibi.name} should ${interaction.name} with payload: ${interaction.payload}`);
|
||||
if (resolution) {
|
||||
@@ -90,7 +110,7 @@ export class Aperio implements IBrain {
|
||||
return isState(this.state, EChibiStateName.Awake);
|
||||
}
|
||||
|
||||
matchInteraction(map: InteractionMap, interaction: EChibiInteraction, state: ChibiState): ResolutionFunction | undefined {
|
||||
matchInteraction(map: InteractionMap, interaction: EChibiInteraction, state: ChibiState): InteractionResolutionFunction | undefined {
|
||||
const directMatch = map?.[state.name]?.[interaction];
|
||||
if (directMatch) {
|
||||
return directMatch;
|
||||
@@ -101,53 +121,61 @@ export class Aperio implements IBrain {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
matchResolution(map: ResolutionMap, state: ChibiState): ResolutionFunction | undefined {
|
||||
const directMatch = map?.[state.name];
|
||||
if (directMatch) {
|
||||
return directMatch;
|
||||
}
|
||||
if (state.parent) {
|
||||
return this.matchResolution(map, state.parent);
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
|
||||
// resolvers
|
||||
// functions which auitomatically resolve based on the existence cycle
|
||||
// functions which automatically resolve based on the existence cycle
|
||||
|
||||
resolveAwake(): void {
|
||||
if (this.isAwake()) {
|
||||
console.log(`${this.chibi.name} is awake.`);
|
||||
this.timeSinceLastInteraction++;
|
||||
this.hasBeenAwakeFor++;
|
||||
resolveAwake(): ChibiState | undefined {
|
||||
console.log(`${this.chibi.name} is awake.`);
|
||||
this.timeSinceLastInteraction++;
|
||||
this.hasBeenAwakeFor++;
|
||||
|
||||
if (this.timeInCurrentState % 10 === 0) {
|
||||
this.saddenedByNeglect(this.timeSinceLastInteraction, SADNESS_FROM_NEGLECT);
|
||||
}
|
||||
|
||||
if (this.hasBeenAwakeFor > MAX_TIME_AWAKE) {
|
||||
this.changeState(STATE_SLEEPING);
|
||||
}
|
||||
if (this.timeInCurrentState % 10 === 0) {
|
||||
this.saddenedByNeglect(this.timeSinceLastInteraction, SADNESS_FROM_NEGLECT);
|
||||
}
|
||||
|
||||
if (this.hasBeenAwakeFor > MAX_TIME_AWAKE) {
|
||||
return STATE_SLEEPING;
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
|
||||
resolveSleeping(): void {
|
||||
if (isState(this.state, EChibiStateName.Sleeping)) {
|
||||
console.log(`${this.chibi.name} is sleeping.`);
|
||||
this.hasBeenAwakeFor = 0;
|
||||
this.increaseHappyness(HAPPINESS_FROM_SLEEP);
|
||||
const awakeChance = this.randomService.obtainRandom(2048);
|
||||
if (awakeChance.matchesChance(1)) {
|
||||
this.changeState(STATE_AWAKE);
|
||||
}
|
||||
resolveSleeping(): ChibiState | undefined {
|
||||
console.log(`${this.chibi.name} is sleeping.`);
|
||||
this.hasBeenAwakeFor = 0;
|
||||
this.increaseHappyness(HAPPINESS_FROM_SLEEP);
|
||||
const awakeChance = this.randomService.obtainRandom(2048);
|
||||
if (awakeChance.matchesChance(1)) {
|
||||
return STATE_AWAKE;
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
|
||||
resolveGrumbling(): void {
|
||||
if (isState(this.state, EChibiStateName.Grumbling)) {
|
||||
console.log(`${this.chibi.name} is grumbling.`);
|
||||
if (this.timeInCurrentState > MAX_DURATION_GRUMBLE) {
|
||||
this.changeState(STATE_SLEEPING);
|
||||
}
|
||||
resolveGrumbling(): ChibiState | undefined {
|
||||
console.log(`${this.chibi.name} is grumbling. ${this.timeInCurrentState}`);
|
||||
if (this.timeInCurrentState > MAX_DURATION_GRUMBLE) {
|
||||
return STATE_SLEEPING;
|
||||
}
|
||||
|
||||
return undefined;
|
||||
}
|
||||
|
||||
resolveEating(): void {
|
||||
if (isState(this.state, EChibiStateName.Eating)) {
|
||||
console.log(`${this.chibi.name} is eating.`);
|
||||
if (this.timeInCurrentState > MAX_DURATION_EATING) {
|
||||
this.changeState(STATES[this.previousState]);
|
||||
}
|
||||
resolveEating(): ChibiState | undefined {
|
||||
console.log(`${this.chibi.name} is eating. ${this.timeInCurrentState}`);
|
||||
if (this.timeInCurrentState > MAX_DURATION_EATING) {
|
||||
return STATE_IDLE;
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
|
||||
|
||||
@@ -163,8 +191,7 @@ export class Aperio implements IBrain {
|
||||
|
||||
private changeState(state: ChibiState): void {
|
||||
this.previousState = this.state.name;
|
||||
this.state = state;
|
||||
this.chibi.state = this.state.name;
|
||||
this.chibi.currentStateName = state.name;
|
||||
this.timeInCurrentState = 0;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user