🤖 Entity 基类方法¶
tongsim.entity.entity ¶
Entity ¶
Entity 类代表一个 TongSim 世界中的对象
类的职责包括:
- 管理组件 ID,按组件类型分类
- 提供能力(Ability)访问与转换机制
注意: Entity 不直接持有组件数据,仅维护 component_id 结构。
Source code in src\tongsim\entity\entity.py
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 |
|
from_grpc
async
classmethod
¶
from_grpc(
entity_id: str, world_context: WorldContext
) -> Entity
通过 gRPC 查询构造 Entity。
:param conn: GrpcConnection :param entity_id: Entity 唯一 ID :return: Entity 实例
Source code in src\tongsim\entity\entity.py
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
|
get_components_id_list ¶
get_components_id_list(
component_type: ComponentType,
) -> list[str]
获取指定类型的所有组件 ID。
:param component_type: 组件类型(如 "CamParamComponent") :return: 组件 ID 列表(可能为空)
Source code in src\tongsim\entity\entity.py
73 74 75 76 77 78 79 80 |
|
get_component_id ¶
get_component_id(component_type: ComponentType) -> str
获取指定类型的第一个组件 ID(如果存在)。
:param component_type: 组件类型 :return: 组件 ID :raises: KeyError 如果组件不存在
Source code in src\tongsim\entity\entity.py
82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
|
has_component_type ¶
has_component_type(component_type: ComponentType) -> bool
判断是否拥有指定类型的组件。
Parameters:
Name | Type | Description | Default |
---|---|---|---|
component_type
|
ComponentType
|
组件类型(如 "Camera") |
required |
Returns:
Name | Type | Description |
---|---|---|
bool |
bool
|
是否拥有该类型的组件。 |
Source code in src\tongsim\entity\entity.py
97 98 99 100 101 102 103 104 105 106 107 |
|
has_component_id ¶
has_component_id(component_id: str) -> bool
判断是否包含指定组件 ID。
Parameters:
Name | Type | Description | Default |
---|---|---|---|
component_id
|
str
|
要检查的组件唯一 ID |
required |
Returns:
Name | Type | Description |
---|---|---|
bool |
bool
|
是否包含该组件 ID。 |
Source code in src\tongsim\entity\entity.py
109 110 111 112 113 114 115 116 117 118 119 |
|
has_ability ¶
has_ability(ability_type: type[T]) -> bool
判断该 Entity 是否支持指定的能力接口(Ability)。
Parameters:
Name | Type | Description | Default |
---|---|---|---|
ability_type
|
type[T]
|
能力类型(Protocol) |
required |
Returns:
Name | Type | Description |
---|---|---|
bool |
bool
|
是否支持该能力 |
Source code in src\tongsim\entity\entity.py
121 122 123 124 125 126 127 128 129 130 131 132 |
|
as_ ¶
as_(ability_type: type[T]) -> T
将 Entity 同步转换为具备指定能力的对象(Ability 实现类)。
注意
- 本方法为同步封装,内部通过
sync_run
调用异步的async_as_
。 - 若在事件循环线程内调用此方法,可能会导致死锁,应优先使用
async_as_
。
Parameters:
Name | Type | Description | Default |
---|---|---|---|
ability_type
|
type[T]
|
能力接口类型(Protocol)。 |
required |
Returns:
Name | Type | Description |
---|---|---|
T |
T
|
实现了该能力的能力对象实例。 |
Raises:
Type | Description |
---|---|
RuntimeError
|
如果能力未注册,或当前 Entity 不支持该能力。 |
Source code in src\tongsim\entity\entity.py
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
|
async_as_
async
¶
async_as_(ability_type: type[T]) -> T
异步地将 Entity 转换为具备指定能力的对象(Ability 实现类)。
本方法通过能力注册表获取对应的实现类,并进行初始化绑定。 若已缓存能力实例,将直接返回缓存。
Parameters:
Name | Type | Description | Default |
---|---|---|---|
ability_type
|
type[T]
|
能力接口类型(Protocol)。 |
required |
Returns:
Name | Type | Description |
---|---|---|
T |
T
|
实现了该能力的能力对象实例。 |
Raises:
Type | Description |
---|---|
RuntimeError
|
如果未注册该能力,或当前 Entity 不具备该能力要求。 |
Source code in src\tongsim\entity\entity.py
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 |
|