Files
esengine/source/src/ECS/Component.ts

156 lines
3.6 KiB
TypeScript
Raw Normal View History

/**
*
*
* 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 {
2020-07-22 20:07:14 +08:00
/**
* ID生成器
*
* ID
2020-07-22 20:07:14 +08:00
*/
public static _idGenerator: number = 0;
/**
*
*
* ID
*/
public readonly id: number;
/**
*
*
*
*/
public entity!: Entity;
/**
*
*
*
*/
private _enabled: boolean = true;
/**
*
*
*
*/
private _updateOrder: number = 0;
2020-06-08 23:04:57 +08:00
/**
*
*
* ID给组件
*/
constructor() {
this.id = Component._idGenerator++;
}
/**
*
*
*
*
* @returns true
*/
public get enabled(): boolean {
return this.entity ? this.entity.enabled && this._enabled : this._enabled;
}
2020-06-08 23:04:57 +08:00
/**
*
*
*
*
* @param value -
*/
public set enabled(value: boolean) {
if (this._enabled !== value) {
this._enabled = value;
if (this._enabled) {
this.onEnabled();
} else {
this.onDisabled();
2020-07-22 20:07:14 +08:00
}
}
}
/**
*
*
* @returns
*/
public get updateOrder(): number {
return this._updateOrder;
}
2020-07-08 18:12:17 +08:00
/**
*
*
* @param value -
*/
public set updateOrder(value: number) {
this._updateOrder = value;
}
2021-08-20 19:05:40 +08:00
/**
*
*
*
*/
public onAddedToEntity(): void {
}
2021-08-20 19:05:40 +08:00
/**
*
*
*
*/
public onRemovedFromEntity(): void {
}
2021-08-20 19:05:40 +08:00
/**
*
*
*
*/
public onEnabled(): void {
}
2021-08-20 19:05:40 +08:00
/**
*
*
*
*/
public onDisabled(): void {
}
2021-08-20 19:05:40 +08:00
/**
*
*
*
*
*/
public update(): void {
2020-06-08 20:11:58 +08:00
}
2020-07-22 20:07:14 +08:00
}
// 避免循环引用在文件末尾导入Entity
import type { Entity } from './Entity';