the pear goes back to sleep

This commit is contained in:
2026-02-26 18:35:48 +01:00
parent 7835a4df02
commit 621659a352
5 changed files with 137 additions and 26 deletions

View File

@@ -4,8 +4,9 @@ import { Food } from "../../types/food";
export interface IBrain { export interface IBrain {
init(chibi: Chibi): void;
//function for time passegs + autonomous state changes //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
} }

View File

@@ -1,30 +1,44 @@
import { Chibi } from "../../../types/chibi/chibi"; import { Chibi } from "../../../types/chibi/chibi";
import { EChibiInteraction } from "../../../types/chibi/chibi-interaction"; 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 { EChibiStateName } from "../../../types/chibi/chibi-state-name";
import { Food } from "../../../types/food"; import { Food } from "../../../types/food";
import { IBrain } from "../brain"; import { IBrain } from "../brain";
const MAX_TIME_AWAKE: number = 3;
export class Aperio implements IBrain { export class Aperio implements IBrain {
private chibi!: Chibi;
private hasBeenAwakeFor: number = 0; private hasBeenAwakeFor: number = 0;
private state!: ChibiState;
exist(chibi: Chibi): void { init(chibi: Chibi): void {
this.chibi = chibi;
this.state = STATES[chibi.state];
}
exist(): void {
console.log('Brain is braining'); console.log('Brain is braining');
if (!EChibiStateName.Sleeping) { if (isState(this.state, EChibiStateName.Awake)) {
console.log('is awake');
this.hasBeenAwakeFor++; this.hasBeenAwakeFor++;
if (this.hasBeenAwakeFor > 60) { if (this.hasBeenAwakeFor > MAX_TIME_AWAKE) {
chibi.state = EChibiStateName.Sleeping; this.state = STATE_SLEEPING;
this.chibi.state = EChibiStateName.Sleeping;
this.hasBeenAwakeFor = 0;
} }
} }
this.chibi.state = this.state.name;
} }
resolveInteraction(chibi: Chibi, interaction: EChibiInteraction, item?: Food): void { resolveInteraction(interaction: EChibiInteraction, item?: Food): void {
switch (chibi.state) { if (isState(this.state, EChibiStateName.Sleeping)) {
case EChibiStateName.Sleeping: chibi.state = this.resolveSleepingInteraction(interaction); break; this.state = this.resolveSleepingInteraction(interaction);
case EChibiStateName.Awake: chibi.state = this.resolveAwakeInteraction(interaction, item); break; } else {
default: this.state = this.resolveAwakeInteraction(interaction, item);
chibi.state = EChibiStateName.Sleeping; break;
} }
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 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) { 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) { if (EChibiInteraction.Feed) {
return EChibiStateName.Eating; return STATE_EATING;
} }
return EChibiStateName.Idle; return STATE_IDLE;
} }
}; };

View File

@@ -13,6 +13,7 @@ import { BRAIN_MAP } from '../../logic/chibi-behaviour/brain-map';
import { TranslateableComponent } from '../../components/translateable.component'; import { TranslateableComponent } from '../../components/translateable.component';
import { Food } from '../../types/food'; import { Food } from '../../types/food';
import { interval } from 'rxjs'; import { interval } from 'rxjs';
import { ChibiState, STATES } from '../../types/chibi/chibi-state';
enum EInteractionMenuState { enum EInteractionMenuState {
Neutral, Neutral,
@@ -20,6 +21,8 @@ enum EInteractionMenuState {
Inventory Inventory
} }
const THINKING_INTERVAL: number = 10000;
@Component({ @Component({
selector: 'ff-interactions', selector: 'ff-interactions',
imports: [HeaderComponent, InteractionCanvasComponent, FoodPantryComponent, InventoryComponent, ButtonComponent], imports: [HeaderComponent, InteractionCanvasComponent, FoodPantryComponent, InventoryComponent, ButtonComponent],
@@ -46,16 +49,17 @@ export class InteractionsComponent extends TranslateableComponent {
effect(() => { effect(() => {
if (this.chibi()) { if (this.chibi()) {
this.brain = BRAIN_MAP[this.chibi().name]!; this.brain = BRAIN_MAP[this.chibi().name]!;
this.brain.init(this.chibi());
} }
}) })
interval(1000).subscribe(() => { interval(THINKING_INTERVAL).subscribe(() => {
this.brain.exist(this.chibi()); this.brain.exist();
}) })
} }
public interact(interaction: EChibiInteraction, item?: Food): void { public interact(interaction: EChibiInteraction, item?: Food): void {
console.log(`${interaction} ${this.chibi().name} and item ${item}`); console.log(`${interaction} ${this.chibi().name} and item ${item}`);
this.brain.resolveInteraction(this.chibi(), interaction, item); this.brain.resolveInteraction(interaction, item);
} }
public openFoodPantry(): void { public openFoodPantry(): void {

View File

@@ -1,11 +1,13 @@
export enum EChibiStateName { export enum EChibiStateName {
Sleeping = 'Sleeping', Sleeping = 'Sleeping',
Snoring = 'Snoring',
Nightmare = 'Nightmare',
Grumbling = 'Grumbling',
Awake = 'Awake', Awake = 'Awake',
Bored = 'Bored', Bored = 'Bored',
Idle = 'Idle', Idle = 'Idle',
Exited = 'Exited', Exited = 'Exited',
Fighting = 'Fighting', Fighting = 'Fighting',
Walking = 'Walking',
Eating = 'Eating', Eating = 'Eating',
Hungry = 'Hunry', Hungry = 'Hunry',
Depressed = 'Depressed', Depressed = 'Depressed',

View File

@@ -0,0 +1,90 @@
import { EChibiStateName } from "./chibi-state-name"
export type ChibiState = {
name: EChibiStateName;
parent?: ChibiState;
}
export const isState = (state: ChibiState, stateName: EChibiStateName): boolean => {
if (state.parent) {
return state.name === stateName || isState(state.parent, stateName);
}
return state.name === stateName;
}
export const STATE_SLEEPING: ChibiState = {
name: EChibiStateName.Sleeping
}
export const STATE_GRUMBLING: ChibiState = {
name: EChibiStateName.Grumbling,
parent: STATE_SLEEPING
}
export const STATE_SNORING: ChibiState = {
name: EChibiStateName.Snoring,
parent: STATE_SLEEPING
}
export const STATE_NIGHTMARE: ChibiState = {
name: EChibiStateName.Nightmare,
parent: STATE_SLEEPING
}
export const STATE_AWAKE: ChibiState = {
name: EChibiStateName.Awake
}
export const STATE_EATING: ChibiState = {
name: EChibiStateName.Eating,
parent: STATE_AWAKE
}
export const STATE_BORED: ChibiState = {
name: EChibiStateName.Bored,
parent: STATE_AWAKE
}
export const STATE_IDLE: ChibiState = {
name: EChibiStateName.Idle,
parent: STATE_AWAKE
}
export const STATE_EXITED: ChibiState = {
name: EChibiStateName.Exited,
parent: STATE_AWAKE
}
export const STATE_FIGHTING: ChibiState = {
name: EChibiStateName.Fighting,
parent: STATE_AWAKE
}
export const STATE_HUNGRY: ChibiState = {
name: EChibiStateName.Hungry,
parent: STATE_AWAKE
}
export const STATE_DEPRESSED: ChibiState = {
name: EChibiStateName.Depressed,
parent: STATE_AWAKE
}
export const STATES: Record<EChibiStateName, ChibiState> = {
[EChibiStateName.Sleeping]: STATE_SLEEPING,
[EChibiStateName.Snoring]: STATE_SNORING,
[EChibiStateName.Nightmare]: STATE_NIGHTMARE,
[EChibiStateName.Grumbling]: STATE_GRUMBLING,
[EChibiStateName.Awake]: STATE_AWAKE,
[EChibiStateName.Bored]: STATE_BORED,
[EChibiStateName.Idle]: STATE_IDLE,
[EChibiStateName.Exited]: STATE_EXITED,
[EChibiStateName.Fighting]: STATE_FIGHTING,
[EChibiStateName.Eating]: STATE_EATING,
[EChibiStateName.Hungry]: STATE_HUNGRY,
[EChibiStateName.Depressed]: STATE_DEPRESSED
}