Skip to content

Home > @esengine/ecs-framework-monorepo > Component

Component class

游戏组件基类

ECS架构中的组件(Component),用于实现具体的游戏功能。 组件包含数据和行为,可以被添加到实体上以扩展实体的功能。

Signature:

typescript
export declare abstract class Component implements IComponent

Implements: IComponent

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();
        }
    }
}

Constructors

Constructor

Modifiers

Description

(constructor)()

创建组件实例

自动分配唯一ID给组件。

Properties

Property

Modifiers

Type

Description

_idGenerator

static

number

组件ID生成器

用于为每个组件分配唯一的ID。

enabled

boolean

获取或设置组件启用状态

组件的实际启用状态取决于自身状态和所属实体的状态。 当设置状态改变时会触发相应的生命周期回调。

entity

Entity

组件所属的实体

指向拥有此组件的实体实例。

id

readonly

number

组件唯一标识符

在整个游戏生命周期中唯一的数字ID。

updateOrder

number

获取或设置更新顺序

Methods

Method

Modifiers

Description

onAddedToEntity()

组件添加到实体时的回调

当组件被添加到实体时调用,可以在此方法中进行初始化操作。

onDisabled()

组件禁用时的回调

当组件被禁用时调用。

onEnabled()

组件启用时的回调

当组件被启用时调用。

onRemovedFromEntity()

组件从实体移除时的回调

当组件从实体中移除时调用,可以在此方法中进行清理操作。

update()

更新组件

每帧调用,用于更新组件的逻辑。 子类应该重写此方法来实现具体的更新逻辑。

基于 MIT 许可证发布