Files
esengine/packages/core/src/ECS/Component.ts

88 lines
2.2 KiB
TypeScript
Raw Normal View History

2025-06-30 20:43:11 +08:00
import type { IComponent } from '../Types';
/**
*
*
* ECS架构中的组件Component
* EntitySystem
*
* @example
*
* ```typescript
* class HealthComponent extends Component {
* public health: number = 100;
* public maxHealth: number = 100;
* }
* ```
*
* @example
* System
* ```typescript
* class HealthSystem extends EntitySystem {
* process(entities: Entity[]): void {
* for (const entity of entities) {
* const health = entity.getComponent(HealthComponent);
* if (health && health.health <= 0) {
* entity.destroy();
* }
* }
* }
* }
* ```
*/
export abstract class Component implements IComponent {
/**
* ID生成器
*
* ID
*/
public static _idGenerator: number = 0;
/**
*
*
* ID
*/
public readonly id: number;
/**
* ID
*
* ID而非引用ECS数据导向设计
*/
public entityId: number | null = null;
/**
*
*
* ID给组件
*/
constructor() {
this.id = Component._idGenerator++;
}
/**
*
*
*
*
* @remarks
*
* System
*/
public onAddedToEntity(): void {
}
/**
*
*
*
*
* @remarks
*
* System
*/
public onRemovedFromEntity(): void {
}
}