更新network库及core库优化

This commit is contained in:
YHH
2025-08-12 09:39:07 +08:00
parent c178e2fbcc
commit 9f76d37a82
117 changed files with 17988 additions and 4099 deletions

View File

@@ -1,6 +1,6 @@
import { Entity } from '../../Entity';
import { Component } from '../../Component';
import { Scene } from '../../Scene';
import { IScene } from '../../IScene';
import { ComponentType } from '../ComponentStorage';
import { QuerySystem, QueryBuilder } from '../QuerySystem';
import { TypeSafeEventSystem } from '../EventSystem';
@@ -14,11 +14,11 @@ import { EntityBatchOperator } from './EntityBatchOperator';
* 提供统一的流式接口
*/
export class ECSFluentAPI {
private scene: Scene;
private scene: IScene;
private querySystem: QuerySystem;
private eventSystem: TypeSafeEventSystem;
constructor(scene: Scene, querySystem: QuerySystem, eventSystem: TypeSafeEventSystem) {
constructor(scene: IScene, querySystem: QuerySystem, eventSystem: TypeSafeEventSystem) {
this.scene = scene;
this.querySystem = querySystem;
this.eventSystem = eventSystem;
@@ -86,7 +86,7 @@ export class ECSFluentAPI {
* @returns 实体或null
*/
public findByName(name: string): Entity | null {
return this.scene.getEntityByName(name);
return this.scene.findEntity(name);
}
/**
@@ -95,7 +95,7 @@ export class ECSFluentAPI {
* @returns 实体数组
*/
public findByTag(tag: number): Entity[] {
return this.scene.getEntitiesByTag(tag);
return this.scene.findEntitiesByTag(tag);
}
/**
@@ -161,16 +161,16 @@ export class ECSFluentAPI {
public getStats(): {
entityCount: number;
systemCount: number;
componentStats: Map<string, any>;
componentStats: Map<string, unknown>;
queryStats: unknown;
eventStats: Map<string, any>;
eventStats: Map<string, unknown>;
} {
return {
entityCount: this.scene.entities.count,
systemCount: this.scene.systems.length,
componentStats: this.scene.componentStorageManager.getAllStats(),
queryStats: this.querySystem.getStats(),
eventStats: this.eventSystem.getStats() as Map<string, any>
eventStats: this.eventSystem.getStats() as Map<string, unknown>
};
}
}
@@ -183,7 +183,7 @@ export class ECSFluentAPI {
* @returns ECS流式API实例
*/
export function createECSAPI(
scene: Scene,
scene: IScene,
querySystem: QuerySystem,
eventSystem: TypeSafeEventSystem
): ECSFluentAPI {
@@ -202,7 +202,7 @@ export let ECS: ECSFluentAPI;
* @param eventSystem 事件系统
*/
export function initializeECS(
scene: Scene,
scene: IScene,
querySystem: QuerySystem,
eventSystem: TypeSafeEventSystem
): void {

View File

@@ -1,6 +1,6 @@
import { Entity } from '../../Entity';
import { Component } from '../../Component';
import { Scene } from '../../Scene';
import { IScene } from '../../IScene';
import { ComponentType, ComponentStorageManager } from '../ComponentStorage';
/**
@@ -8,10 +8,10 @@ import { ComponentType, ComponentStorageManager } from '../ComponentStorage';
*/
export class EntityBuilder {
private entity: Entity;
private scene: Scene;
private scene: IScene;
private storageManager: ComponentStorageManager;
constructor(scene: Scene, storageManager: ComponentStorageManager) {
constructor(scene: IScene, storageManager: ComponentStorageManager) {
this.scene = scene;
this.storageManager = storageManager;
this.entity = new Entity("", scene.identifierPool.checkOut());

View File

@@ -0,0 +1,158 @@
import { Entity } from './Entity';
import { EntityList } from './Utils/EntityList';
import { EntityProcessorList } from './Utils/EntityProcessorList';
import { IdentifierPool } from './Utils/IdentifierPool';
import { EntitySystem } from './Systems/EntitySystem';
import { ComponentStorageManager } from './Core/ComponentStorage';
import { QuerySystem } from './Core/QuerySystem';
import { TypeSafeEventSystem } from './Core/EventSystem';
/**
* 场景接口定义
*
* 定义场景应该实现的核心功能和属性,使用接口而非继承提供更灵活的实现方式。
*/
export interface IScene {
/**
* 场景名称
*/
readonly name: string;
/**
* 场景中的实体集合
*/
readonly entities: EntityList;
/**
* 实体系统处理器集合
*/
readonly entityProcessors: EntityProcessorList;
/**
* 标识符池
*/
readonly identifierPool: IdentifierPool;
/**
* 组件存储管理器
*/
readonly componentStorageManager: ComponentStorageManager;
/**
* 查询系统
*/
readonly querySystem: QuerySystem;
/**
* 事件系统
*/
readonly eventSystem: TypeSafeEventSystem;
/**
* 获取系统列表
*/
readonly systems: EntitySystem[];
/**
* 初始化场景
*/
initialize(): void;
/**
* 场景开始运行时的回调
*/
onStart(): void;
/**
* 场景卸载时的回调
*/
unload(): void;
/**
* 开始场景
*/
begin(): void;
/**
* 结束场景
*/
end(): void;
/**
* 更新场景
*/
update(): void;
/**
* 创建实体
*/
createEntity(name: string): Entity;
/**
* 添加实体
*/
addEntity(entity: Entity, deferCacheClear?: boolean): Entity;
/**
* 批量创建实体
*/
createEntities(count: number, namePrefix?: string): Entity[];
/**
* 销毁所有实体
*/
destroyAllEntities(): void;
/**
* 查找实体
*/
findEntity(name: string): Entity | null;
/**
* 根据标签查找实体
*/
findEntitiesByTag(tag: number): Entity[];
/**
* 添加实体处理器
*/
addEntityProcessor(processor: EntitySystem): EntitySystem;
/**
* 移除实体处理器
*/
removeEntityProcessor(processor: EntitySystem): void;
/**
* 获取实体处理器
*/
getEntityProcessor<T extends EntitySystem>(type: new (...args: any[]) => T): T | null;
}
/**
* 场景工厂接口
*/
export interface ISceneFactory<T extends IScene> {
/**
* 创建场景实例
*/
createScene(): T;
}
/**
* 场景配置接口
*/
export interface ISceneConfig {
/**
* 场景名称
*/
name?: string;
/**
* 是否自动开始
*/
autoStart?: boolean;
/**
* 调试配置
*/
debug?: boolean;
}

View File

@@ -7,27 +7,36 @@ import { ComponentStorageManager } from './Core/ComponentStorage';
import { QuerySystem } from './Core/QuerySystem';
import { TypeSafeEventSystem } from './Core/EventSystem';
import { EventBus } from './Core/EventBus';
import { IScene, ISceneConfig } from './IScene';
/**
* 游戏场景类
* 游戏场景默认实现
*
* 管理游戏场景中的所有实体和系统,提供场景生命周期管理
* 场景是游戏世界的容器,负责协调实体和系统的运行
* 实现IScene接口提供场景的基础功能
* 推荐使用组合而非继承的方式来构建自定义场景
*
* @example
* ```typescript
* // 推荐的组合方式
* class GameScene implements IScene {
* private scene = new Scene();
*
* public initialize(): void {
* this.scene.initialize();
* // 自定义初始化逻辑
* }
* }
*
* // 仍然支持继承方式
* class GameScene extends Scene {
* public initialize(): void {
* // 创建游戏实体
* const player = this.createEntity("Player");
*
* // 添加系统
* this.addEntityProcessor(new MovementSystem());
* super.initialize();
* // 自定义逻辑
* }
* }
* ```
*/
export class Scene {
export class Scene implements IScene {
/**
* 场景名称
*
@@ -89,10 +98,15 @@ export class Scene {
return this.entityProcessors.processors;
}
/**
* 是否已完成基础初始化
*/
private _isBaseInitialized = false;
/**
* 创建场景实例
*/
constructor() {
constructor(config?: ISceneConfig) {
this.entities = new EntityList(this);
this.entityProcessors = new EntityProcessorList();
this.identifierPool = new IdentifierPool();
@@ -100,17 +114,30 @@ export class Scene {
this.querySystem = new QuerySystem();
this.eventSystem = new TypeSafeEventSystem();
// 应用配置
if (config?.name) {
this.name = config.name;
}
if (!Entity.eventBus) {
Entity.eventBus = new EventBus(false);
}
if (Entity.eventBus) {
Entity.eventBus.onComponentAdded((data: any) => {
Entity.eventBus.onComponentAdded((data: unknown) => {
this.eventSystem.emitSync('component:added', data);
});
}
// 标记基础初始化完成
this._isBaseInitialized = true;
// 立即调用初始化,但确保在基础设施就绪后
this.initialize();
if (config?.autoStart) {
this.begin();
}
}
/**

View File

@@ -4,6 +4,7 @@ export { ECSEventType, EventPriority, EVENT_TYPES, EventTypeValidator } from './
export * from './Systems';
export * from './Utils';
export { Scene } from './Scene';
export { IScene, ISceneFactory, ISceneConfig } from './IScene';
export { EntityManager, EntityQueryBuilder } from './Core/EntityManager';
export * from './Core/Events';
export * from './Core/Query';