* @zh 获取指定类型的组件 * @en Get component of specified type * * @param type - @zh 组件类型构造函数 @en Component type constructor * @returns @zh 组件实例,如果不存在则返回null @en Component instance, or null if not found * * @example * ```typescript * const position = entity.getComponen
(type: ComponentType<T>)
| 515 | * ``` |
| 516 | */ |
| 517 | public getComponent<T extends Component>(type: ComponentType<T>): T | null { |
| 518 | // 快速检查:位掩码 |
| 519 | if (!this.hasComponent(type)) { |
| 520 | return null; |
| 521 | } |
| 522 | |
| 523 | // 从Scene存储获取 |
| 524 | if (!this.scene?.componentStorageManager) { |
| 525 | return null; |
| 526 | } |
| 527 | |
| 528 | const component = this.scene.componentStorageManager.getComponent(this.id, type); |
| 529 | return component as T | null; |
| 530 | } |
| 531 | |
| 532 | /** |
| 533 | * @zh 检查实体是否拥有指定类型的组件 |
no test coverage detected