the pear goes back to sleep
This commit is contained in:
@@ -4,8 +4,9 @@ import { Food } from "../../types/food";
|
||||
|
||||
export interface IBrain {
|
||||
|
||||
init(chibi: Chibi): void;
|
||||
//function for time passegs + autonomous state changes
|
||||
exist(chibi: Chibi): void;
|
||||
exist(): void;
|
||||
|
||||
resolveInteraction(chibi: Chibi, interaction: EChibiInteraction, item?: Food): void
|
||||
resolveInteraction(interaction: EChibiInteraction, item?: Food): void
|
||||
}
|
||||
@@ -1,30 +1,44 @@
|
||||
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 { EChibiStateName } from "../../../types/chibi/chibi-state-name";
|
||||
import { Food } from "../../../types/food";
|
||||
import { IBrain } from "../brain";
|
||||
|
||||
const MAX_TIME_AWAKE: number = 3;
|
||||
|
||||
export class Aperio implements IBrain {
|
||||
|
||||
private chibi!: Chibi;
|
||||
private hasBeenAwakeFor: number = 0;
|
||||
private state!: ChibiState;
|
||||
|
||||
exist(chibi: Chibi): void {
|
||||
console.log('Brain is braining');
|
||||
if (!EChibiStateName.Sleeping) {
|
||||
this.hasBeenAwakeFor++;
|
||||
if (this.hasBeenAwakeFor > 60) {
|
||||
chibi.state = EChibiStateName.Sleeping;
|
||||
}
|
||||
}
|
||||
init(chibi: Chibi): void {
|
||||
this.chibi = chibi;
|
||||
this.state = STATES[chibi.state];
|
||||
}
|
||||
|
||||
resolveInteraction(chibi: Chibi, interaction: EChibiInteraction, item?: Food): void {
|
||||
switch (chibi.state) {
|
||||
case EChibiStateName.Sleeping: chibi.state = this.resolveSleepingInteraction(interaction); break;
|
||||
case EChibiStateName.Awake: chibi.state = this.resolveAwakeInteraction(interaction, item); break;
|
||||
default:
|
||||
chibi.state = EChibiStateName.Sleeping; break;
|
||||
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 {
|
||||
if (isState(this.state, EChibiStateName.Sleeping)) {
|
||||
this.state = this.resolveSleepingInteraction(interaction);
|
||||
} else {
|
||||
this.state = this.resolveAwakeInteraction(interaction, item);
|
||||
}
|
||||
this.chibi.state = this.state.name;
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -50,17 +64,17 @@ export class Aperio implements IBrain {
|
||||
after any interaction she has a 1/4 chance of going to sleep
|
||||
*/
|
||||
|
||||
private resolveSleepingInteraction(interaction: EChibiInteraction): EChibiStateName {
|
||||
private resolveSleepingInteraction(interaction: EChibiInteraction): ChibiState {
|
||||
if (EChibiInteraction.WakeUp) {
|
||||
return EChibiStateName.Awake;
|
||||
return STATE_AWAKE;
|
||||
}
|
||||
return EChibiStateName.Sleeping;
|
||||
return STATE_SLEEPING;
|
||||
}
|
||||
|
||||
private resolveAwakeInteraction(interaction: EChibiInteraction, item?: Food): EChibiStateName {
|
||||
private resolveAwakeInteraction(interaction: EChibiInteraction, item?: Food): ChibiState {
|
||||
if (EChibiInteraction.Feed) {
|
||||
return EChibiStateName.Eating;
|
||||
return STATE_EATING;
|
||||
}
|
||||
return EChibiStateName.Idle;
|
||||
return STATE_IDLE;
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user