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,35 @@
# CreateGameObject
Create a new GameObject in this scene.
## Syntax
```cpp
GameObject* CreateGameObject(const std::string& name, GameObject* parent = nullptr);
```
## Parameters
- `name` - The name of the new GameObject.
- `parent` - Optional parent GameObject (defaults to `nullptr`).
## Returns
Returns a pointer to the newly created `GameObject`.
## Remarks
Creates a new GameObject with the specified name and optionally sets its parent. The GameObject is owned by the scene and will be destroyed when the scene is destroyed.
## See Also
- [DestroyGameObject](destroy-game-object)
## Examples
```cpp
void Example(Scene* scene) {
GameObject* player = scene->CreateGameObject("Player");
GameObject* weapon = scene->CreateGameObject("Sword", player);
}
```

View File

@@ -0,0 +1,29 @@
# DestroyGameObject
Destroy a GameObject in this scene.
## Syntax
```cpp
void DestroyGameObject(GameObject* gameObject);
```
## Parameters
- `gameObject` - Pointer to the GameObject to destroy.
## Remarks
Destroys the specified GameObject and all its components. The GameObject will be destroyed at the end of the current frame.
## See Also
- [CreateGameObject](create-game-object)
## Examples
```cpp
void Example(Scene* scene, GameObject* enemy) {
scene->DestroyGameObject(enemy);
}
```

View File

@@ -0,0 +1,36 @@
# FindGameObjectWithTag
Find the first GameObject with the specified tag.
## Syntax
```cpp
GameObject* FindGameObjectWithTag(const std::string& tag) const;
```
## Parameters
- `tag` - The tag to search for.
## Returns
Returns a pointer to the first GameObject with the specified tag, or `nullptr` if not found.
## Remarks
Searches the scene for a GameObject with the specified tag. Tags must be set on individual GameObjects.
## See Also
- [Find](find)
## Examples
```cpp
void Example(Scene* scene) {
GameObject* enemy = scene->FindGameObjectWithTag("Enemy");
if (enemy) {
XC_LOG_INFO("Found enemy!");
}
}
```

View File

@@ -0,0 +1,36 @@
# Find
Find a GameObject by name.
## Syntax
```cpp
GameObject* Find(const std::string& name) const;
```
## Parameters
- `name` - The name of the GameObject to find.
## Returns
Returns a pointer to the GameObject with the specified name, or `nullptr` if not found.
## Remarks
Searches the scene for a GameObject with the specified name. The search includes all root GameObjects and their children.
## See Also
- [FindGameObjectWithTag](find-game-object-with-tag)
## Examples
```cpp
void Example(Scene* scene) {
GameObject* player = scene->Find("Player");
if (player) {
XC_LOG_INFO("Found player!");
}
}
```

View File

@@ -0,0 +1,30 @@
# FixedUpdate
Perform fixed update on all GameObjects in this scene.
## Syntax
```cpp
void FixedUpdate(float fixedDeltaTime);
```
## Parameters
- `fixedDeltaTime` - The fixed time step in seconds.
## Remarks
Calls `FixedUpdate` on all GameObjects and their components. This is typically called at a fixed time interval for physics updates.
## See Also
- [Update](update)
- [LateUpdate](late-update)
## Examples
```cpp
void Example(Scene* scene) {
scene->FixedUpdate(0.02f);
}
```

View File

@@ -0,0 +1,26 @@
# GetName
Get the name of this scene.
## Syntax
```cpp
const std::string& GetName() const;
```
## Returns
Returns the scene name as a `std::string` reference.
## See Also
- [SetName](set-name)
## Examples
```cpp
void Example(Scene* scene) {
const std::string& name = scene->GetName();
XC_LOG_INFO("Scene name: {}", name);
}
```

View File

@@ -0,0 +1,27 @@
# GetRootGameObjects
Get all root-level GameObjects in this scene.
## Syntax
```cpp
std::vector<GameObject*> GetRootGameObjects() const;
```
## Returns
Returns a `std::vector` containing pointers to all root-level GameObjects.
## Remarks
Root-level GameObjects are those without a parent within the scene hierarchy.
## Examples
```cpp
void Example(Scene* scene) {
for (GameObject* go : scene->GetRootGameObjects()) {
XC_LOG_INFO("Root object: {}", go->GetName());
}
}
```

View File

@@ -0,0 +1,27 @@
# IsActive
Check if this scene is active.
## Syntax
```cpp
bool IsActive() const;
```
## Returns
Returns `true` if the scene is active, `false` otherwise.
## See Also
- [SetActive](set-active)
## Examples
```cpp
void Example(Scene* scene) {
if (scene->IsActive()) {
XC_LOG_INFO("Scene is active");
}
}
```

View File

@@ -0,0 +1,30 @@
# LateUpdate
Perform late update on all GameObjects in this scene.
## Syntax
```cpp
void LateUpdate(float deltaTime);
```
## Parameters
- `deltaTime` - The time elapsed since the last frame in seconds.
## Remarks
Calls `LateUpdate` on all GameObjects and their components. This is typically called after `Update` for operations that need to run after other updates.
## See Also
- [Update](update)
- [FixedUpdate](fixed-update)
## Examples
```cpp
void Example(Scene* scene) {
scene->LateUpdate(0.016f);
}
```

View File

@@ -0,0 +1,29 @@
# Load
Load a scene from a file.
## Syntax
```cpp
void Load(const std::string& filePath);
```
## Parameters
- `filePath` - The path to the scene file.
## Remarks
Deserializes the scene from the specified file path, replacing the current scene contents.
## See Also
- [Save](save)
## Examples
```cpp
void Example(Scene* scene) {
scene->Load("assets/scenes/my_scene.scene");
}
```

View File

@@ -0,0 +1,31 @@
# OnGameObjectCreated
Event triggered when a GameObject is created in this scene.
## Syntax
```cpp
Core::Event<GameObject*>& OnGameObjectCreated();
```
## Returns
Returns a reference to the `Core::Event<GameObject*>` for this event.
## Remarks
Subscribe to this event to be notified when a GameObject is created in the scene. The event passes the newly created GameObject as a parameter.
## See Also
- [OnGameObjectDestroyed](on-game-object-destroyed)
## Examples
```cpp
void Example(Scene* scene) {
scene->OnGameObjectCreated().AddCallback([](GameObject* go) {
XC_LOG_INFO("GameObject created: {}", go->GetName());
});
}
```

View File

@@ -0,0 +1,31 @@
# OnGameObjectDestroyed
Event triggered when a GameObject is destroyed in this scene.
## Syntax
```cpp
Core::Event<GameObject*>& OnGameObjectDestroyed();
```
## Returns
Returns a reference to the `Core::Event<GameObject*>` for this event.
## Remarks
Subscribe to this event to be notified when a GameObject is destroyed in the scene. The event passes the destroyed GameObject as a parameter.
## See Also
- [OnGameObjectCreated](on-game-object-created)
## Examples
```cpp
void Example(Scene* scene) {
scene->OnGameObjectDestroyed().AddCallback([](GameObject* go) {
XC_LOG_INFO("GameObject destroyed: {}", go->GetName());
});
}
```

View File

@@ -0,0 +1,29 @@
# Save
Save this scene to a file.
## Syntax
```cpp
void Save(const std::string& filePath);
```
## Parameters
- `filePath` - The path to save the scene file.
## Remarks
Serializes the scene and all its GameObjects to the specified file path.
## See Also
- [Load](load)
## Examples
```cpp
void Example(Scene* scene) {
scene->Save("assets/scenes/my_scene.scene");
}
```

View File

@@ -0,0 +1,90 @@
# Scene
**命名空间**: `XCEngine::Components`
**类型**: `class`
**头文件**: `XCEngine/Scene/Scene.h`
**描述**: 场景类,管理场景中的所有 GameObject 和层级结构。
## 概述
Scene 是 XCEngine 中的场景类,代表一个独立的游戏空间(如主菜单、游戏关卡、过场动画等)。每个 Scene 包含一个 GameObject 层级树通过根对象Root GameObjects组织。Scene 负责创建和销毁 GameObject提供查找功能支持场景保存/加载,并发布 GameObject 创建和销毁事件。
## 类型别名
| 别名 | 类型 | 描述 |
|------|------|------|
| `GameObjectID` | `uint64_t` | 游戏对象唯一标识符类型 |
| `INVALID_GAMEOBJECT_ID` | `static constexpr GameObjectID` | 无效 ID 常量,值为 0 |
## 公共方法
### 基础信息
| 方法 | 描述 |
|------|------|
| [`GetName`](get-name.md) | 获取场景名称 |
| [`SetName`](set-name.md) | 设置场景名称 |
| [`IsActive`](is-active.md) | 检查场景是否激活 |
| [`SetActive`](set-active.md) | 设置场景激活状态 |
### GameObject 管理
| 方法 | 描述 |
|------|------|
| [`CreateGameObject`](create-game-object.md) | 创建游戏对象 |
| [`DestroyGameObject`](destroy-game-object.md) | 销毁游戏对象 |
### 查找
| 方法 | 描述 |
|------|------|
| [`Find`](find.md) | 按名称查找 GameObject |
| [`FindGameObjectWithTag`](find-game-object-with-tag.md) | 按标签查找 GameObject |
| [`FindObjectOfType`](find-object-of-type.md) | 查找指定类型的组件 |
| [`FindObjectsOfType`](find-objects-of-type.md) | 查找所有指定类型的组件 |
| [`GetRootGameObjects`](get-root-game-objects.md) | 获取所有根对象 |
### 生命周期
| 方法 | 描述 |
|------|------|
| [`Update`](update.md) | 每帧更新场景 |
| [`FixedUpdate`](fixed-update.md) | 固定频率更新 |
| [`LateUpdate`](late-update.md) | 晚更新 |
| [`Load`](load.md) | 加载场景 |
| [`Save`](save.md) | 保存场景 |
### 事件
| 方法 | 描述 |
|------|------|
| [`OnGameObjectCreated`](on-game-object-created.md) | GameObject 创建事件 |
| [`OnGameObjectDestroyed`](on-game-object-destroyed.md) | GameObject 销毁事件 |
## 使用示例
```cpp
#include <XCEngine/Scene/Scene.h>
#include <XCEngine/Components/GameObject.h>
using namespace XCEngine::Components;
void SceneExample(Scene* scene) {
GameObject* player = scene->CreateGameObject("Player");
GameObject* enemy = scene->CreateGameObject("Enemy");
scene->SetActive(true);
GameObject* found = scene->Find("Player");
auto cameras = scene->FindObjectsOfType<TransformComponent>();
}
```
## 相关文档
- [Scene 模块总览](../scene.md) - Scene 模块总览
- [SceneManager](scene-manager/scene-manager.md) - 场景管理器
- [GameObject](../components/game-object/game-object.md) - 游戏对象

View File

@@ -0,0 +1,25 @@
# SetActive
Set whether this scene is active.
## Syntax
```cpp
void SetActive(bool active);
```
## Parameters
- `active` - Whether to activate or deactivate the scene.
## See Also
- [IsActive](is-active)
## Examples
```cpp
void Example(Scene* scene) {
scene->SetActive(false);
}
```

View File

@@ -0,0 +1,25 @@
# SetName
Set the name of this scene.
## Syntax
```cpp
void SetName(const std::string& name);
```
## Parameters
- `name` - The new scene name.
## See Also
- [GetName](get-name)
## Examples
```cpp
void Example(Scene* scene) {
scene->SetName("MainScene");
}
```

View File

@@ -0,0 +1,25 @@
# Update
Update all GameObjects in this scene.
## Syntax
```cpp
void Update(float deltaTime);
```
## Parameters
- `deltaTime` - The time elapsed since the last frame in seconds.
## Remarks
Calls `Update` on all GameObjects and their components. This is typically called once per frame from the main game loop.
## Examples
```cpp
void Example(Scene* scene) {
scene->Update(0.016f);
}
```