Files
esengine/src/ECS/Component.ts

158 lines
3.5 KiB
TypeScript
Raw Normal View History

import type { IComponent } from '../types';
/**
*
*
* ECS架构中的组件Component
*
*
* @example
* ```typescript
* class HealthComponent extends Component {
* public health: number = 100;
*
* public takeDamage(damage: number): void {
* this.health -= damage;
* if (this.health <= 0) {
* this.entity.destroy();
* }
* }
* }
* ```
*/
export abstract class Component implements IComponent {
/**
* ID生成器
*
* ID
*/
public static _idGenerator: number = 0;
/**
*
*
* ID
*/
public readonly id: number;
/**
*
*
*
*/
public entity!: Entity;
/**
*
*
*
*/
private _enabled: boolean = true;
/**
*
*
*
*/
private _updateOrder: number = 0;
/**
*
*
* ID给组件
*/
constructor() {
this.id = Component._idGenerator++;
}
/**
*
*
*
*
* @returns true
*/
public get enabled(): boolean {
return this.entity ? this.entity.enabled && this._enabled : this._enabled;
}
/**
*
*
*
*
* @param value -
*/
public set enabled(value: boolean) {
if (this._enabled !== value) {
this._enabled = value;
if (this._enabled) {
this.onEnabled();
} else {
this.onDisabled();
}
}
}
/**
*
*
* @returns
*/
public get updateOrder(): number {
return this._updateOrder;
}
/**
*
*
* @param value -
*/
public set updateOrder(value: number) {
this._updateOrder = value;
}
/**
*
*
*
*/
public onAddedToEntity(): void {
}
/**
*
*
*
*/
public onRemovedFromEntity(): void {
}
/**
*
*
*
*/
public onEnabled(): void {
}
/**
*
*
*
*/
public onDisabled(): void {
}
/**
*
*
*
*
*/
public update(): void {
}
}
// 避免循环引用在文件末尾导入Entity
import type { Entity } from './Entity';