Files
forgotten-friends/src/app/pages/interactions/interactions.component.ts
2026-03-30 16:31:19 +02:00

100 lines
3.9 KiB
TypeScript

import { Component, computed, effect, inject, input, InputSignal, OnDestroy, signal, Signal, WritableSignal } from '@angular/core';
import { HeaderComponent } from "../../components/ui-elements/header/header.component";
import { ERouteKey } from '../../types/route-key';
import { Chibi, ChibiId } from '../../types/chibi/chibi';
import { InteractionCanvasComponent } from "../../components/interaction-canvas/interaction-canvas.component";
import { ChibiStore } from '../../api/chibi.store';
import { FoodPantryComponent } from "../../components/interaction-menus/food-pantry/food-pantry.component";
import { InventoryComponent } from "../../components/interaction-menus/inventory/inventory.component";
import { ButtonComponent } from "../../components/ui-elements/button/button.component";
import { EChibiInteraction } from '../../types/chibi/chibi-interaction';
import { IBrain } from '../../logic/chibi-behaviour/brain';
import { BRAIN_MAP } from '../../logic/chibi-behaviour/brain-map';
import { TranslateableComponent } from '../../components/translateable.component';
import { Food } from '../../types/food';
import { interval, Subscription } from 'rxjs';
import { RandomService } from '../../logic/random.service';
import { isState, STATES } from '../../types/chibi/chibi-state';
import { EChibiStateName } from '../../types/chibi/chibi-state-name';
import { VisitorListComponent } from "../../components/interaction-menus/visitor-list/visitor-list.component";
import { ChibiInteraction } from '../../logic/chibi-behaviour/basic.brain';
enum EInteractionMenuState {
Neutral,
Pantry,
Inventory,
VisitorList
}
const THINKING_INTERVAL: number = 2000;
@Component({
selector: 'ff-interactions',
imports: [HeaderComponent, InteractionCanvasComponent, FoodPantryComponent, InventoryComponent, ButtonComponent, VisitorListComponent],
templateUrl: './interactions.component.html',
})
export class InteractionsComponent extends TranslateableComponent implements OnDestroy {
public readonly chibiId: InputSignal<ChibiId> = input.required<ChibiId>();
private readonly chibiStore: ChibiStore = inject(ChibiStore);
private readonly randomService: RandomService = inject(RandomService);
protected readonly chibi: Signal<Chibi> = computed(() => {
return this.chibiStore.chibis().find((chibi: Chibi) => chibi.id === this.chibiId()) as Chibi;
});
protected readonly interactionMenuState: WritableSignal<EInteractionMenuState> = signal(EInteractionMenuState.Neutral);
protected readonly EInteractionMenuState: typeof EInteractionMenuState = EInteractionMenuState;
protected readonly ERouteKey: typeof ERouteKey = ERouteKey;
protected readonly EChibiInteraction: typeof EChibiInteraction = EChibiInteraction;
private brain!: IBrain;
private sub: Subscription = new Subscription();
constructor() {
super();
effect(() => {
if (this.chibi()) {
this.brain = BRAIN_MAP[this.chibi().name]!;
this.brain.init(this.chibi(), this.randomService);
}
})
this.sub.add(interval(THINKING_INTERVAL).subscribe(() => {
this.brain.exist();
}));
}
ngOnDestroy() {
this.sub.unsubscribe();
}
public isAwake(): boolean {
return isState(STATES[this.chibi().currentStateName], EChibiStateName.Awake);
}
public interact(interaction: EChibiInteraction, item?: Food): void {
console.log(`${interaction} ${this.chibi().name} and item ${item}`);
this.brain.resolveInteraction({ name: interaction, payload: item } as ChibiInteraction<Food>);
}
public openFoodPantry(): void {
this.interactionMenuState.set(EInteractionMenuState.Pantry);
}
public openInventory(): void {
this.interactionMenuState.set(EInteractionMenuState.Inventory);
}
public openVisitorList(): void {
this.interactionMenuState.set(EInteractionMenuState.VisitorList);
}
public closeMenu(): void {
this.interactionMenuState.set(EInteractionMenuState.Neutral);
}
}