Integrate role asset studio into custom world agent flow
This commit is contained in:
@@ -1,4 +1,15 @@
|
||||
import type { GameState, StoryMoment } from '../types';
|
||||
import {
|
||||
buildInitialNpcState,
|
||||
createNpcBattleMonster,
|
||||
} from '../data/npcInteractions';
|
||||
import type {
|
||||
Encounter,
|
||||
GameState,
|
||||
NpcPersistentState,
|
||||
SceneHostileNpc,
|
||||
StoryMoment,
|
||||
} from '../types';
|
||||
import { WorldType } from '../types';
|
||||
import type { BottomTab } from '../types/navigation';
|
||||
import type {
|
||||
HydratableGameState,
|
||||
@@ -37,6 +48,199 @@ function createEmptyEquipmentLoadout() {
|
||||
} satisfies GameState['playerEquipment'];
|
||||
}
|
||||
|
||||
function resolveHydrationWorldType(worldType: GameState['worldType']) {
|
||||
const normalizedWorldType =
|
||||
typeof worldType === 'string' ? worldType.toUpperCase() : worldType;
|
||||
|
||||
if (normalizedWorldType === WorldType.WUXIA) {
|
||||
return WorldType.WUXIA;
|
||||
}
|
||||
|
||||
if (normalizedWorldType === WorldType.XIANXIA) {
|
||||
return WorldType.XIANXIA;
|
||||
}
|
||||
|
||||
if (normalizedWorldType === WorldType.CUSTOM) {
|
||||
return WorldType.CUSTOM;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
function hasRenderableRuntimeNpcBattleFields(hostileNpc: SceneHostileNpc) {
|
||||
const candidate = hostileNpc as Partial<SceneHostileNpc>;
|
||||
|
||||
return Boolean(
|
||||
candidate.encounter &&
|
||||
typeof candidate.animation === 'string' &&
|
||||
typeof candidate.xMeters === 'number' &&
|
||||
typeof candidate.yOffset === 'number' &&
|
||||
typeof candidate.facing === 'string' &&
|
||||
typeof candidate.attackRange === 'number' &&
|
||||
typeof candidate.speed === 'number',
|
||||
);
|
||||
}
|
||||
|
||||
function normalizeRuntimeBattleEncounter(
|
||||
encounter: GameState['currentEncounter'],
|
||||
): Encounter | null {
|
||||
if (!encounter || encounter.kind !== 'npc') {
|
||||
return null;
|
||||
}
|
||||
|
||||
const npcName =
|
||||
typeof encounter.npcName === 'string' ? encounter.npcName.trim() : '';
|
||||
if (!npcName) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return {
|
||||
...encounter,
|
||||
kind: 'npc',
|
||||
npcName,
|
||||
npcDescription:
|
||||
typeof encounter.npcDescription === 'string'
|
||||
? encounter.npcDescription
|
||||
: '',
|
||||
npcAvatar:
|
||||
typeof encounter.npcAvatar === 'string' ? encounter.npcAvatar : '',
|
||||
context: typeof encounter.context === 'string' ? encounter.context : '',
|
||||
hostile: true,
|
||||
} satisfies Encounter;
|
||||
}
|
||||
|
||||
function resolveRuntimeNpcBattleState(
|
||||
gameState: Pick<
|
||||
GameState,
|
||||
| 'currentBattleNpcId'
|
||||
| 'currentEncounter'
|
||||
| 'customWorldProfile'
|
||||
| 'npcStates'
|
||||
| 'sceneHostileNpcs'
|
||||
| 'worldType'
|
||||
>,
|
||||
) {
|
||||
const encounter = normalizeRuntimeBattleEncounter(gameState.currentEncounter);
|
||||
if (!encounter || gameState.sceneHostileNpcs.length === 0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const npcStateKey =
|
||||
gameState.currentBattleNpcId ??
|
||||
encounter.id ??
|
||||
encounter.npcName;
|
||||
const npcState =
|
||||
gameState.npcStates[npcStateKey] ??
|
||||
buildInitialNpcState(
|
||||
encounter,
|
||||
resolveHydrationWorldType(gameState.worldType),
|
||||
gameState as GameState,
|
||||
);
|
||||
|
||||
return {
|
||||
encounter,
|
||||
npcState,
|
||||
};
|
||||
}
|
||||
|
||||
function hydrateRuntimeNpcBattleMonster(params: {
|
||||
hostileNpc: SceneHostileNpc;
|
||||
encounter: Encounter;
|
||||
npcState: NpcPersistentState;
|
||||
gameState: Pick<GameState, 'customWorldProfile' | 'worldType'>;
|
||||
battleMode: NonNullable<GameState['currentNpcBattleMode']>;
|
||||
}) {
|
||||
const template = createNpcBattleMonster(
|
||||
params.encounter,
|
||||
params.npcState,
|
||||
params.battleMode,
|
||||
{
|
||||
worldType: resolveHydrationWorldType(params.gameState.worldType),
|
||||
customWorldProfile: params.gameState.customWorldProfile,
|
||||
},
|
||||
);
|
||||
const candidate = params.hostileNpc as Partial<SceneHostileNpc>;
|
||||
const xMeters =
|
||||
typeof candidate.xMeters === 'number' ? candidate.xMeters : template.xMeters;
|
||||
const yOffset =
|
||||
typeof candidate.yOffset === 'number' ? candidate.yOffset : template.yOffset;
|
||||
|
||||
return {
|
||||
...template,
|
||||
id:
|
||||
typeof candidate.id === 'string' && candidate.id.trim()
|
||||
? candidate.id
|
||||
: template.id,
|
||||
name:
|
||||
typeof candidate.name === 'string' && candidate.name.trim()
|
||||
? candidate.name
|
||||
: template.name,
|
||||
description:
|
||||
typeof candidate.description === 'string'
|
||||
? candidate.description
|
||||
: template.description,
|
||||
hp: typeof candidate.hp === 'number' ? candidate.hp : template.hp,
|
||||
maxHp:
|
||||
typeof candidate.maxHp === 'number' ? candidate.maxHp : template.maxHp,
|
||||
animation:
|
||||
typeof candidate.animation === 'string'
|
||||
? candidate.animation
|
||||
: template.animation,
|
||||
xMeters,
|
||||
yOffset,
|
||||
facing:
|
||||
candidate.facing === 'left' || candidate.facing === 'right'
|
||||
? candidate.facing
|
||||
: template.facing,
|
||||
attackRange:
|
||||
typeof candidate.attackRange === 'number'
|
||||
? candidate.attackRange
|
||||
: template.attackRange,
|
||||
speed:
|
||||
typeof candidate.speed === 'number' ? candidate.speed : template.speed,
|
||||
encounter: {
|
||||
...template.encounter,
|
||||
xMeters,
|
||||
},
|
||||
} satisfies SceneHostileNpc;
|
||||
}
|
||||
|
||||
export function hydrateRuntimeNpcBattleGameState(
|
||||
gameState: HydratedGameState,
|
||||
): HydratedGameState {
|
||||
const battleMode = gameState.currentNpcBattleMode;
|
||||
|
||||
if (
|
||||
gameState.inBattle !== true ||
|
||||
(battleMode !== 'fight' && battleMode !== 'spar') ||
|
||||
gameState.currentEncounter?.kind !== 'npc' ||
|
||||
gameState.sceneHostileNpcs.length === 0 ||
|
||||
gameState.sceneHostileNpcs.every(hasRenderableRuntimeNpcBattleFields)
|
||||
) {
|
||||
return gameState;
|
||||
}
|
||||
|
||||
const resolvedState = resolveRuntimeNpcBattleState(gameState);
|
||||
if (!resolvedState) {
|
||||
return gameState;
|
||||
}
|
||||
|
||||
return {
|
||||
...gameState,
|
||||
sceneHostileNpcs: gameState.sceneHostileNpcs.map((hostileNpc) =>
|
||||
hasRenderableRuntimeNpcBattleFields(hostileNpc)
|
||||
? hostileNpc
|
||||
: hydrateRuntimeNpcBattleMonster({
|
||||
hostileNpc,
|
||||
encounter: resolvedState.encounter,
|
||||
npcState: resolvedState.npcState,
|
||||
gameState,
|
||||
battleMode,
|
||||
}),
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
export function normalizeSavedStory(story: StoryMoment | null) {
|
||||
if (!story) {
|
||||
return null;
|
||||
@@ -57,7 +261,7 @@ export function normalizeSavedGameState(gameState: GameState) {
|
||||
const playerMaxHp = Math.max(1, hydratableState.playerMaxHp);
|
||||
const playerMaxMana = Math.max(1, hydratableState.playerMaxMana);
|
||||
|
||||
return {
|
||||
return hydrateRuntimeNpcBattleGameState({
|
||||
...hydratableState,
|
||||
playerMaxHp,
|
||||
playerHp: Math.min(hydratableState.playerHp, playerMaxHp),
|
||||
@@ -72,7 +276,7 @@ export function normalizeSavedGameState(gameState: GameState) {
|
||||
typeof hydratableState.runtimeSessionId === 'string'
|
||||
? hydratableState.runtimeSessionId
|
||||
: null,
|
||||
} satisfies HydratedGameState;
|
||||
} satisfies HydratedGameState);
|
||||
}
|
||||
|
||||
export function hydrateSnapshotState(snapshot: {
|
||||
@@ -105,8 +309,23 @@ export function isHydratedSnapshotState(
|
||||
);
|
||||
}
|
||||
|
||||
export function rehydrateSavedSnapshot<T extends HydratedSnapshotState>(
|
||||
snapshot: T,
|
||||
): T {
|
||||
const hydratedGameState = hydrateRuntimeNpcBattleGameState(snapshot.gameState);
|
||||
|
||||
if (hydratedGameState === snapshot.gameState) {
|
||||
return snapshot;
|
||||
}
|
||||
|
||||
return {
|
||||
...snapshot,
|
||||
gameState: hydratedGameState,
|
||||
};
|
||||
}
|
||||
|
||||
export function resolveHydratedSnapshotState(snapshot: SnapshotState) {
|
||||
return isHydratedSnapshotState(snapshot)
|
||||
? snapshot
|
||||
? rehydrateSavedSnapshot(snapshot)
|
||||
: hydrateSnapshotState(snapshot);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user