扩展ECS框架可通过@ecs.register('Initialize')方式注册系统组件

This commit is contained in:
donggang
2024-02-18 16:30:00 +08:00
parent 3818503e58
commit f22403eef2
4 changed files with 21 additions and 2 deletions

View File

@@ -100,7 +100,6 @@ export class Root extends Component {
oops.game = new GameManager(this.game);
oops.gui = new LayerManager(this.gui);
this.initGui();
this.initEcsSystem();
oops.ecs.init();

View File

@@ -116,8 +116,17 @@ export module ecs {
*/
export function register<T>(name: string, canNew: boolean = true) {
return function (ctor: any) {
// 注册系统
if (ctor.s) {
var system: ecs.System = ECSModel.systems.get(name);
if (system == null) {
system = new ecs.System();
ECSModel.systems.set(name, system);
}
system.add(new ctor);
}
// 注册实体
if (ctor.tid == undefined) {
else if (ctor.tid == undefined) {
ECSModel.entityCtors.set(ctor as EntityCtor<T>, name);
}
// 注册组件

View File

@@ -76,4 +76,7 @@ export class ECSModel {
}
return group as unknown as ECSGroup<E>;
}
/** 系统组件 */
static systems: Map<string, ecs.System> = new Map<string, ecs.System>();
}

View File

@@ -5,6 +5,8 @@ import { ECSModel } from "./ECSModel";
/** 继承此类实现具体业务逻辑的系统 */
export abstract class ECSComblockSystem<E extends ECSEntity = ECSEntity> {
static s: boolean = true;
protected group: ECSGroup<E>;
protected dt: number = 0;
@@ -179,6 +181,12 @@ export class ECSRootSystem {
}
init() {
// 自动注册系统组件
ECSModel.systems.forEach(s => {
this.add(s);
});
// 初始化组件
this.executeSystemFlows.forEach(sys => sys.init());
}