docs: Add Component, GameObject, TransformComponent and Scene API documentation

- Add Component class documentation with lifecycle methods
- Add GameObject class documentation with component system
- Add TransformComponent documentation with transform methods
- Add Scene class documentation with GameObject management
- Add SceneManager singleton documentation with scene loading
- Update components.md overview with all component classes
- Update main.md navigation with Scene module
This commit is contained in:
2026-03-22 03:33:55 +08:00
parent d83ed56177
commit a9d5a68dd6
105 changed files with 3003 additions and 7 deletions

View File

@@ -0,0 +1,23 @@
# GameObject::AddComponent
向游戏对象添加组件。
```cpp
template<typename T, typename... Args>
T* AddComponent(Args&&... args);
```
模板参数 T 必须继承自 Component。组件被添加后其 m_gameObject 成员会自动设置为该 GameObject。
**模板参数:**
- `T` - 组件类型
**参数:**
- `args` - 传递给组件构造函数的参数
**返回:** `T*` - 创建的组件指针
## 相关文档
- [GameObject 总览](game-object.md)
- [Component](component/component.md)

View File

@@ -0,0 +1,14 @@
# GameObject::Awake
唤醒游戏对象及其所有组件。
```cpp
void Awake();
```
在对象创建后首次添加到活动场景时调用,用于初始化组件状态。
## 相关文档
- [GameObject 总览](game-object.md)
- [Start](start.md)

View File

@@ -0,0 +1,14 @@
# GameObject::Destroy
销毁游戏对象。
```cpp
void Destroy();
```
将对象标记为待销毁,在当前帧结束时实际销毁。
## 相关文档
- [GameObject 总览](game-object.md)
- [OnDestroy](on-destroy.md)

View File

@@ -0,0 +1,14 @@
# GameObject::DetachChildren
分离所有子对象。
```cpp
void DetachChildren();
```
将所有子对象从当前 GameObject 分离,使它们成为根对象。
## 相关文档
- [GameObject 总览](game-object.md)
- [SetParent](set-parent.md)

View File

@@ -0,0 +1,14 @@
# GameObject::FixedUpdate
固定频率更新游戏对象及其所有组件(用于物理)。
```cpp
void FixedUpdate();
```
以固定时间间隔调用,适合物理模拟更新。
## 相关文档
- [GameObject 总览](game-object.md)
- [Update](update.md)

View File

@@ -0,0 +1,118 @@
# GameObject
**命名空间**: `XCEngine::Components`
**类型**: `class`
**头文件**: `XCEngine/Components/GameObject.h`
**描述**: ECS 架构中的游戏对象实体,可附加组件并形成层级结构。
## 概述
GameObject 是 XCEngine ECS 架构中的实体Entity代表游戏世界中的一个个对象如角色、道具、光源等。每个 GameObject 可以通过 AddComponent 附加各种组件(如 TransformComponent、AudioSourceComponent来定义其行为和数据。GameObject 支持层级结构(父子关系)、名称查找、标签系统和生命周期管理。
## 类型别名
| 别名 | 类型 | 描述 |
|------|------|------|
| `ID` | `uint64_t` | 游戏对象唯一标识符类型 |
| `INVALID_ID` | `static constexpr ID` | 无效 ID 常量,值为 0 |
## 公共方法
### 基础信息
| 方法 | 描述 |
|------|------|
| [`GetID`](get-id.md) | 获取唯一标识符 |
| [`GetName`](get-name.md) | 获取名称 |
| [`SetName`](set-name.md) | 设置名称 |
| [`GetScene`](get-scene.md) | 获取所属场景 |
### 组件系统
| 方法 | 描述 |
|------|------|
| [`GetTransform`](get-transform.md) | 获取变换组件 |
| [`AddComponent`](add-component.md) | 添加组件(模板方法) |
| [`GetComponent`](get-component.md) | 获取组件(模板方法) |
| [`GetComponents`](get-components.md) | 获取所有指定类型组件(模板方法) |
| [`GetComponentInChildren`](get-component-in-children.md) | 在子对象中查找组件 |
| [`GetComponentInParent`](get-component-in-parent.md) | 在父对象中查找组件 |
| [`GetComponentsInChildren`](get-components-in-children.md) | 获取所有子对象的指定组件 |
### 层级结构
| 方法 | 描述 |
|------|------|
| [`GetParent`](get-parent.md) | 获取父对象 |
| [`SetParent`](set-parent.md) | 设置父对象 |
| [`GetChildCount`](get-child-count.md) | 获取子对象数量 |
| [`GetChild`](get-child.md) | 获取指定索引的子对象 |
| [`GetChildren`](get-children.md) | 获取所有子对象 |
| [`DetachChildren`](detach-children.md) | 分离所有子对象 |
### 激活状态
| 方法 | 描述 |
|------|------|
| [`IsActive`](is-active.md) | 检查自身激活状态 |
| [`SetActive`](set-active.md) | 设置激活状态 |
| [`IsActiveInHierarchy`](is-active-in-hierarchy.md) | 检查在层级中的激活状态 |
### 静态查找
| 方法 | 描述 |
|------|------|
| `static Find(name)` | 查找指定名称的对象 |
| `static FindObjectsOfType()` | 查找所有对象 |
| `static FindGameObjectsWithTag(tag)` | 查找具有指定标签的对象 |
### 生命周期
| 方法 | 描述 |
|------|------|
| [`Awake`](awake.md) | 唤醒对象 |
| [`Start`](start.md) | 开始对象 |
| [`Update`](update.md) | 每帧更新 |
| [`FixedUpdate`](fixed-update.md) | 固定频率更新 |
| [`LateUpdate`](late-update.md) | 晚更新 |
| [`OnDestroy`](on-destroy.md) | 销毁回调 |
| [`Destroy`](destroy.md) | 销毁对象 |
## 使用示例
```cpp
#include <XCEngine/Components/GameObject.h>
#include <XCEngine/Components/TransformComponent.h>
#include <XCEngine/Components/AudioSourceComponent.h>
using namespace XCEngine::Components;
void CreatePlayer() {
GameObject* player = new GameObject("Player");
auto transform = player->AddComponent<TransformComponent>();
transform->SetPosition(Vector3(0, 0, 0));
auto audio = player->AddComponent<AudioSourceComponent>();
audio->SetVolume(0.8f);
player->SetParent(cameraObject);
}
void FindAndOperate() {
GameObject* enemy = GameObject::Find("Enemy");
if (enemy) {
enemy->SetActive(false);
}
}
```
## 相关文档
- [Components 模块总览](../components.md) - Components 模块总览
- [Component](component/component.md) - 组件基类
- [TransformComponent](transform-component/transform-component.md) - 变换组件
- [Scene](../scene/scene/scene.md) - 场景

View File

@@ -0,0 +1,14 @@
# GameObject::GetChildCount
获取子对象数量。
```cpp
size_t GetChildCount() const;
```
**返回:** `size_t` - 子对象数量
## 相关文档
- [GameObject 总览](game-object.md)
- [GetChild](get-child.md)

View File

@@ -0,0 +1,17 @@
# GameObject::GetChild
获取指定索引的子对象。
```cpp
GameObject* GetChild(size_t index) const;
```
**参数:**
- `index` - 子对象索引
**返回:** `GameObject*` - 指定索引的子对象,如果索引无效则返回 nullptr
## 相关文档
- [GameObject 总览](game-object.md)
- [GetChildren](get-children.md)

View File

@@ -0,0 +1,14 @@
# GameObject::GetChildren
获取所有子对象。
```cpp
std::vector<GameObject*> GetChildren() const;
```
**返回:** `std::vector<GameObject*>` - 包含所有子对象的向量
## 相关文档
- [GameObject 总览](game-object.md)
- [GetChild](get-child.md)

View File

@@ -0,0 +1,20 @@
# GameObject::GetComponentInChildren
在子对象中查找指定类型的组件。
```cpp
template<typename T>
T* GetComponentInChildren();
```
递归搜索所有子对象(深度优先),返回找到的第一个匹配组件。
**模板参数:**
- `T` - 组件类型
**返回:** `T*` - 找到的组件指针,如果未找到则返回 nullptr
## 相关文档
- [GameObject 总览](game-object.md)
- [GetComponentsInChildren](get-components-in-children.md)

View File

@@ -0,0 +1,20 @@
# GameObject::GetComponentInParent
在父对象链中查找指定类型的组件。
```cpp
template<typename T>
T* GetComponentInParent();
```
递归向上搜索父对象及其父对象,返回找到的第一个匹配组件。
**模板参数:**
- `T` - 组件类型
**返回:** `T*` - 找到的组件指针,如果未找到则返回 nullptr
## 相关文档
- [GameObject 总览](game-object.md)
- [GetComponentInChildren](get-component-in-children.md)

View File

@@ -0,0 +1,23 @@
# GameObject::GetComponent
获取游戏对象上指定类型的组件。
```cpp
template<typename T>
T* GetComponent();
template<typename T>
const T* GetComponent() const;
```
使用 dynamic_cast 在组件列表中查找匹配类型的第一个组件。
**模板参数:**
- `T` - 组件类型
**返回:** `T*` - 找到的组件指针,如果未找到则返回 nullptr
## 相关文档
- [GameObject 总览](game-object.md)
- [GetComponents](get-components.md)

View File

@@ -0,0 +1,20 @@
# GameObject::GetComponentsInChildren
获取所有子对象中指定类型的组件。
```cpp
template<typename T>
std::vector<T*> GetComponentsInChildren();
```
递归搜索所有子对象,返回所有匹配类型的组件。
**模板参数:**
- `T` - 组件类型
**返回:** `std::vector<T*>` - 包含所有匹配组件的向量
## 相关文档
- [GameObject 总览](game-object.md)
- [GetComponentInChildren](get-component-in-children.md)

View File

@@ -0,0 +1,21 @@
# GameObject::GetComponents
获取游戏对象上所有指定类型的组件。
```cpp
template<typename T>
std::vector<T*> GetComponents();
template<typename T>
std::vector<const T*> GetComponents() const;
```
**模板参数:**
- `T` - 组件类型
**返回:** `std::vector<T*>` - 包含所有匹配组件的向量
## 相关文档
- [GameObject 总览](game-object.md)
- [GetComponent](get-component.md)

View File

@@ -0,0 +1,13 @@
# GameObject::GetID
获取游戏对象的唯一标识符。
```cpp
ID GetID() const;
```
**返回:** `ID` - 游戏对象的唯一标识符
## 相关文档
- [GameObject 总览](game-object.md)

View File

@@ -0,0 +1,14 @@
# GameObject::GetName
获取游戏对象的名称。
```cpp
const std::string& GetName() const;
```
**返回:** `const std::string&` - 游戏对象名称引用
## 相关文档
- [GameObject 总览](game-object.md)
- [SetName](set-name.md)

View File

@@ -0,0 +1,14 @@
# GameObject::GetParent
获取父游戏对象。
```cpp
GameObject* GetParent() const;
```
**返回:** `GameObject*` - 父对象指针,如果无父对象则返回 nullptr
## 相关文档
- [GameObject 总览](game-object.md)
- [SetParent](set-parent.md)

View File

@@ -0,0 +1,14 @@
# GameObject::GetScene
获取游戏对象所属的场景。
```cpp
Scene* GetScene() const;
```
**返回:** `Scene*` - 所属场景指针,如果对象未属于任何场景则返回 nullptr
## 相关文档
- [GameObject 总览](game-object.md)
- [Scene](../scene/scene/scene.md)

View File

@@ -0,0 +1,15 @@
# GameObject::GetTransform
获取游戏对象的变换组件。
```cpp
TransformComponent* GetTransform();
const TransformComponent* GetTransform() const;
```
**返回:** `TransformComponent*` - 变换组件指针
## 相关文档
- [GameObject 总览](game-object.md)
- [TransformComponent](transform-component/transform-component.md)

View File

@@ -0,0 +1,16 @@
# GameObject::IsActiveInHierarchy
检查游戏对象在层级中的激活状态。
```cpp
bool IsActiveInHierarchy() const;
```
如果对象本身激活但父对象(或更上层的祖先)被禁用,则返回 false。只有当对象及其所有祖先都激活时才返回 true。
**返回:** `bool` - 如果对象在层级中激活则返回 true
## 相关文档
- [GameObject 总览](game-object.md)
- [IsActive](is-active.md)

View File

@@ -0,0 +1,15 @@
# GameObject::IsActive
检查游戏对象的自身激活状态。
```cpp
bool IsActive() const;
```
**返回:** `bool` - 如果对象自身激活则返回 true
## 相关文档
- [GameObject 总览](game-object.md)
- [SetActive](set-active.md)
- [IsActiveInHierarchy](is-active-in-hierarchy.md)

View File

@@ -0,0 +1,17 @@
# GameObject::LateUpdate
晚更新游戏对象及其所有组件。
```cpp
void LateUpdate(float deltaTime);
```
在所有 Update 调用之后调用,适合处理依赖于其他对象更新的逻辑。
**参数:**
- `deltaTime` - 距离上一帧的时间(秒)
## 相关文档
- [GameObject 总览](game-object.md)
- [Update](update.md)

View File

@@ -0,0 +1,14 @@
# GameObject::OnDestroy
销毁回调。
```cpp
void OnDestroy();
```
在对象被销毁前调用,用于清理资源。
## 相关文档
- [GameObject 总览](game-object.md)
- [Destroy](destroy.md)

View File

@@ -0,0 +1,15 @@
# GameObject::SetActive
设置游戏对象的激活状态。
```cpp
void SetActive(bool active);
```
**参数:**
- `active` - true 激活false 禁用
## 相关文档
- [GameObject 总览](game-object.md)
- [IsActive](is-active.md)

View File

@@ -0,0 +1,15 @@
# GameObject::SetName
设置游戏对象的名称。
```cpp
void SetName(const std::string& name);
```
**参数:**
- `name` - 新的名称
## 相关文档
- [GameObject 总览](game-object.md)
- [GetName](get-name.md)

View File

@@ -0,0 +1,19 @@
# GameObject::SetParent
设置父游戏对象。
```cpp
void SetParent(GameObject* parent);
void SetParent(GameObject* parent, bool worldPositionStays);
```
将当前 GameObject 的父对象设置为指定对象。
**参数:**
- `parent` - 新的父对象nullptr 表示成为根对象
- `worldPositionStays` - true 保持世界坐标不变false 保持本地坐标不变
## 相关文档
- [GameObject 总览](game-object.md)
- [GetParent](get-parent.md)

View File

@@ -0,0 +1,15 @@
# GameObject::Start
开始游戏对象及其所有组件。
```cpp
void Start();
```
在首次 Update 之前调用,用于在游戏逻辑开始前完成初始化。
## 相关文档
- [GameObject 总览](game-object.md)
- [Awake](awake.md)
- [Update](update.md)

View File

@@ -0,0 +1,15 @@
# GameObject::Update
每帧更新游戏对象及其所有组件。
```cpp
void Update(float deltaTime);
```
**参数:**
- `deltaTime` - 距离上一帧的时间(秒)
## 相关文档
- [GameObject 总览](game-object.md)
- [LateUpdate](late-update.md)