Files
esengine/packages/editor-app/src/application/commands/component/AddComponentCommand.ts
yhh ad96edfad0 fix: 恢复 @esengine/ecs-framework 包名
上一个提交错误地将 npm 包名也改了,这里恢复正确的包名。
只更新 GitHub 仓库 URL,不改变 npm 包名。
2025-12-08 21:26:35 +08:00

55 lines
1.4 KiB
TypeScript

import { Entity, Component } from '@esengine/ecs-framework';
import { MessageHub } from '@esengine/editor-core';
import { BaseCommand } from '../BaseCommand';
/**
* 添加组件命令
*/
export class AddComponentCommand extends BaseCommand {
private component: Component | null = null;
constructor(
private messageHub: MessageHub,
private entity: Entity,
private ComponentClass: new () => Component,
private initialData?: Record<string, unknown>
) {
super();
}
execute(): void {
this.component = new this.ComponentClass();
// 应用初始数据
if (this.initialData) {
for (const [key, value] of Object.entries(this.initialData)) {
(this.component as any)[key] = value;
}
}
this.entity.addComponent(this.component);
this.messageHub.publish('component:added', {
entity: this.entity,
component: this.component
});
}
undo(): void {
if (!this.component) return;
this.entity.removeComponent(this.component);
this.messageHub.publish('component:removed', {
entity: this.entity,
componentType: this.ComponentClass.name
});
this.component = null;
}
getDescription(): string {
return `添加组件: ${this.ComponentClass.name}`;
}
}