Skip to content

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

Entity class

游戏实体类

ECS架构中的实体(Entity),作为组件的容器。 实体本身不包含游戏逻辑,所有功能都通过组件来实现。 支持父子关系,可以构建实体层次结构。

Signature:

typescript
export declare class Entity

Example

typescript
// 创建实体
const entity = new Entity("Player", 1);

// 添加组件
const healthComponent = entity.addComponent(new HealthComponent(100));

// 获取组件
const health = entity.getComponent(HealthComponent);

// 添加位置组件
entity.addComponent(new PositionComponent(100, 200));

// 添加子实体
const weapon = new Entity("Weapon", 2);
entity.addChild(weapon);

Constructors

Constructor

Modifiers

Description

(constructor)(name, id)

构造函数

Properties

Property

Modifiers

Type

Description

_isDestroyed

boolean

销毁状态标志

active

boolean

获取活跃状态

activeInHierarchy

readonly

boolean

获取实体的有效活跃状态

考虑父实体的活跃状态,只有当实体本身和所有父实体都处于活跃状态时才返回true。

childCount

readonly

number

获取子实体数量

children

readonly

readonly Entity[]

获取子实体数组的只读副本

componentMask

readonly

BitMask64Data

获取组件位掩码

components

readonly

Component[]

组件集合

enabled

boolean

获取启用状态

entityComparer

static

EntityComparer

实体比较器实例

eventBus

static

EventBus | null

全局事件总线实例 用于发射组件相关事件

id

readonly

number

实体唯一标识符

isDestroyed

readonly

boolean

获取销毁状态

name

string

实体名称

parent

readonly

Entity | null

获取父实体

scene

IScene | null

所属场景引用

tag

number

获取实体标签

updateInterval

number

更新间隔

updateOrder

number

获取更新顺序

Methods

Method

Modifiers

Description

addChild(child)

添加子实体

addComponent(component)

添加组件到实体

addComponents(components)

批量添加组件

compareTo(other)

比较实体

createComponent(componentType, args)

创建并添加组件

destroy()

销毁实体

移除所有组件、子实体并标记为已销毁。

findChild(name, recursive)

根据名称查找子实体

findChildrenByTag(tag, recursive)

根据标签查找子实体

forEachChild(callback, recursive)

遍历所有子实体(深度优先)

getComponent(type)

获取指定类型的组件

getComponents(type)

获取所有指定类型的组件

getDebugInfo()

获取实体的调试信息(包含组件缓存信息)

getDepth()

获取层次深度

getOrCreateComponent(type, args)

获取或创建指定类型的组件

getRoot()

获取根实体

hasComponent(type)

检查实体是否有指定类型的组件

isAncestorOf(entity)

检查是否是指定实体的祖先

isDescendantOf(entity)

检查是否是指定实体的后代

removeAllChildren()

移除所有子实体

removeAllComponents()

移除所有组件

removeChild(child)

移除子实体

removeComponent(component)

移除指定的组件

removeComponentByType(type)

移除指定类型的组件

removeComponentsByTypes(componentTypes)

批量移除组件类型

toString()

获取实体的字符串表示

update()

更新实体

调用所有组件的更新方法,并更新子实体。

基于 MIT 许可证发布