Files
esengine/packages/asset-system/src/interfaces/IResourceComponent.ts

63 lines
2.4 KiB
TypeScript
Raw Normal View History

/**
* -
* Interface for components that depend on runtime resources (textures, audio, etc.)
*
* SceneResourceManager
* Components implementing this interface can participate in centralized resource loading managed by SceneResourceManager
*/
/**
* - ID
* Resource reference with path and runtime ID
*/
export interface ResourceReference {
/** 资源路径(例如 "assets/sprites/player.png"/ Asset path (e.g., "assets/sprites/player.png") */
path: string;
/** 引擎分配的运行时资源 ID例如 GPU 上的纹理 ID/ Runtime resource ID assigned by engine (e.g., texture ID on GPU) */
runtimeId?: number;
/** 资源类型标识符 / Resource type identifier */
type: 'texture' | 'audio' | 'font' | 'data';
}
/**
*
* Resource component interface
*
* SceneResourceManager
* Components implementing this interface can have their resources loaded centrally by SceneResourceManager before the scene starts
*/
export interface IResourceComponent {
/**
*
* Get all resource references needed by this component
*
*
* Called during scene loading to collect resource paths
*/
getResourceReferences(): ResourceReference[];
/**
* ID
* Set runtime IDs for loaded resources
*
* SceneResourceManager
* Called after resources are loaded by SceneResourceManager
*
* @param pathToId ID / Map of resource paths to runtime IDs
*/
setResourceIds(pathToId: Map<string, number>): void;
}
/**
* - IResourceComponent
* Type guard to check if a component implements IResourceComponent
*/
export function isResourceComponent(component: any): component is IResourceComponent {
return (
component !== null &&
typeof component === 'object' &&
typeof component.getResourceReferences === 'function' &&
typeof component.setResourceIds === 'function'
);
}