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,31 @@
# CreateScene
Create a new scene with the specified name.
## Syntax
```cpp
Scene* CreateScene(const std::string& name);
```
## Parameters
- `name` - The name for the new scene.
## Returns
Returns a pointer to the newly created `Scene`.
## See Also
- [LoadScene](load-scene)
- [UnloadScene](unload-scene)
## Examples
```cpp
void Example() {
SceneManager& manager = SceneManager::Get();
Scene* newScene = manager.CreateScene("Level1");
}
```

View File

@@ -0,0 +1,29 @@
# GetActiveScene
Get the currently active scene.
## Syntax
```cpp
Scene* GetActiveScene() const;
```
## Returns
Returns a pointer to the currently active `Scene`, or `nullptr` if no scene is active.
## See Also
- [SetActiveScene](set-active-scene)
## Examples
```cpp
void Example() {
SceneManager& manager = SceneManager::Get();
Scene* active = manager.GetActiveScene();
if (active) {
XC_LOG_INFO("Active scene: {}", active->GetName());
}
}
```

View File

@@ -0,0 +1,28 @@
# GetAllScenes
Get all loaded scenes.
## Syntax
```cpp
std::vector<Scene*> GetAllScenes() const;
```
## Returns
Returns a `std::vector` containing pointers to all loaded scenes.
## See Also
- [GetScene](get-scene)
## Examples
```cpp
void Example() {
SceneManager& manager = SceneManager::Get();
for (Scene* scene : manager.GetAllScenes()) {
XC_LOG_INFO("Scene: {}", scene->GetName());
}
}
```

View File

@@ -0,0 +1,33 @@
# GetScene
Get a scene by name.
## Syntax
```cpp
Scene* GetScene(const std::string& name) const;
```
## Parameters
- `name` - The name of the scene.
## Returns
Returns a pointer to the scene with the specified name, or `nullptr` if not found.
## See Also
- [GetAllScenes](get-all-scenes)
## Examples
```cpp
void Example() {
SceneManager& manager = SceneManager::Get();
Scene* scene = manager.GetScene("Level1");
if (scene) {
XC_LOG_INFO("Found scene: {}", scene->GetName());
}
}
```

View File

@@ -0,0 +1,25 @@
# Get
Get the singleton SceneManager instance.
## Syntax
```cpp
static SceneManager& Get();
```
## Returns
Returns a reference to the singleton `SceneManager` instance.
## Remarks
Access the SceneManager singleton through this static method. The SceneManager is created on first access.
## Examples
```cpp
void Example() {
SceneManager& manager = SceneManager::Get();
}
```

View File

@@ -0,0 +1,34 @@
# LoadSceneAsync
Load a scene from a file asynchronously.
## Syntax
```cpp
void LoadSceneAsync(const std::string& filePath, std::function<void(Scene*)> callback);
```
## Parameters
- `filePath` - The path to the scene file.
- `callback` - Callback function called when loading is complete, receives the loaded Scene.
## Remarks
Loads a scene from the specified file path asynchronously. The callback is invoked when loading is complete with a pointer to the loaded scene.
## See Also
- [LoadScene](load-scene)
## Examples
```cpp
void Example() {
SceneManager& manager = SceneManager::Get();
manager.LoadSceneAsync("assets/scenes/level1.scene", [](Scene* scene) {
XC_LOG_INFO("Scene loaded: {}", scene->GetName());
manager.SetActiveScene(scene);
});
}
```

View File

@@ -0,0 +1,31 @@
# LoadScene
Load a scene from a file synchronously.
## Syntax
```cpp
void LoadScene(const std::string& filePath);
```
## Parameters
- `filePath` - The path to the scene file.
## Remarks
Loads a scene from the specified file path. This is a synchronous operation and will block until the scene is fully loaded.
## See Also
- [LoadSceneAsync](load-scene-async)
- [CreateScene](create-scene)
## Examples
```cpp
void Example() {
SceneManager& manager = SceneManager::Get();
manager.LoadScene("assets/scenes/level1.scene");
}
```

View File

@@ -0,0 +1,33 @@
# OnActiveSceneChanged
Event triggered when the active scene changes.
## Syntax
```cpp
Core::Event<Scene*>& OnActiveSceneChanged();
```
## Returns
Returns a reference to the `Core::Event<Scene*>` for this event.
## Remarks
Subscribe to this event to be notified when the active scene changes.
## See Also
- [SetActiveScene](set-active-scene)
- [GetActiveScene](get-active-scene)
## Examples
```cpp
void Example() {
SceneManager& manager = SceneManager::Get();
manager.OnActiveSceneChanged().AddCallback([](Scene* scene) {
XC_LOG_INFO("Active scene changed: {}", scene->GetName());
});
}
```

View File

@@ -0,0 +1,33 @@
# OnSceneLoaded
Event triggered when a scene is loaded.
## Syntax
```cpp
Core::Event<Scene*>& OnSceneLoaded();
```
## Returns
Returns a reference to the `Core::Event<Scene*>` for this event.
## Remarks
Subscribe to this event to be notified when a scene is loaded.
## See Also
- [OnSceneUnloaded](on-scene-unloaded)
- [OnActiveSceneChanged](on-active-scene-changed)
## Examples
```cpp
void Example() {
SceneManager& manager = SceneManager::Get();
manager.OnSceneLoaded().AddCallback([](Scene* scene) {
XC_LOG_INFO("Scene loaded: {}", scene->GetName());
});
}
```

View File

@@ -0,0 +1,33 @@
# OnSceneUnloaded
Event triggered when a scene is unloaded.
## Syntax
```cpp
Core::Event<Scene*>& OnSceneUnloaded();
```
## Returns
Returns a reference to the `Core::Event<Scene*>` for this event.
## Remarks
Subscribe to this event to be notified when a scene is unloaded.
## See Also
- [OnSceneLoaded](on-scene-loaded)
- [OnActiveSceneChanged](on-active-scene-changed)
## Examples
```cpp
void Example() {
SceneManager& manager = SceneManager::Get();
manager.OnSceneUnloaded().AddCallback([](Scene* scene) {
XC_LOG_INFO("Scene unloaded: {}", scene->GetName());
});
}
```

View File

@@ -0,0 +1,74 @@
# SceneManager
**命名空间**: `XCEngine::Components`
**类型**: `class (singleton)`
**头文件**: `XCEngine/Scene/SceneManager.h`
**描述**: 场景管理器单例,管理所有场景和活动场景。
## 概述
SceneManager 是 XCEngine 中的场景管理器单例,负责管理所有加载的场景、场景切换(同步/异步)、设置活动场景,以及发布场景加载/卸载事件。它是场景系统的入口点,提供场景创建、加载、卸载和切换的接口。
## 单例访问
| 方法 | 描述 |
|------|------|
| `static SceneManager& Get()` | 获取场景管理器单例实例 |
## 公共方法
### 场景管理
| 方法 | 描述 |
|------|------|
| [`CreateScene`](create-scene.md) | 创建新场景 |
| [`LoadScene`](load-scene.md) | 同步加载场景 |
| [`LoadSceneAsync`](load-scene-async.md) | 异步加载场景 |
| [`UnloadScene`](unload-scene.md) | 卸载场景 |
### 活动场景
| 方法 | 描述 |
|------|------|
| [`SetActiveScene`](set-active-scene.md) | 设置活动场景 |
| [`GetActiveScene`](get-active-scene.md) | 获取当前活动场景 |
### 查找
| 方法 | 描述 |
|------|------|
| [`GetScene`](get-scene.md) | 按名称获取场景 |
| [`GetAllScenes`](get-all-scenes.md) | 获取所有场景 |
### 事件
| 方法 | 描述 |
|------|------|
| [`OnSceneLoaded`](on-scene-loaded.md) | 场景加载完成事件 |
| [`OnSceneUnloaded`](on-scene-unloaded.md) | 场景卸载完成事件 |
| [`OnActiveSceneChanged`](on-active-scene-changed.md) | 活动场景变更事件 |
## 使用示例
```cpp
#include <XCEngine/Scene/SceneManager.h>
using namespace XCEngine::Components;
void SceneManagerExample() {
Scene* scene = SceneManager::Get().CreateScene("GameLevel");
SceneManager::Get().SetActiveScene(scene);
SceneManager::Get().LoadSceneAsync("levels/menu.json", [](Scene* loadedScene) {
printf("Scene loaded: %s\n", loadedScene->GetName().c_str());
});
}
```
## 相关文档
- [Scene 模块总览](../scene.md) - Scene 模块总览
- [Scene](scene/scene.md) - 场景类

View File

@@ -0,0 +1,32 @@
# SetActiveScene
Set the currently active scene.
## Syntax
```cpp
void SetActiveScene(Scene* scene);
void SetActiveScene(const std::string& sceneName);
```
## Parameters
- `scene` - Pointer to the scene to set as active.
- `sceneName` - Name of the scene to set as active.
## Remarks
Sets which scene is currently active. The active scene is the one that receives updates.
## See Also
- [GetActiveScene](get-active-scene)
## Examples
```cpp
void Example() {
SceneManager& manager = SceneManager::Get();
manager.SetActiveScene("Level1");
}
```

View File

@@ -0,0 +1,33 @@
# UnloadScene
Unload a scene.
## Syntax
```cpp
void UnloadScene(Scene* scene);
void UnloadScene(const std::string& sceneName);
```
## Parameters
- `scene` - Pointer to the scene to unload.
- `sceneName` - Name of the scene to unload.
## Remarks
Removes a scene from the SceneManager. The scene and all its GameObjects will be destroyed.
## See Also
- [CreateScene](create-scene)
- [LoadScene](load-scene)
## Examples
```cpp
void Example() {
SceneManager& manager = SceneManager::Get();
manager.UnloadScene("Level1");
}
```

46
docs/api/scene/scene.md Normal file
View File

@@ -0,0 +1,46 @@
# Scene 模块概览
**命名空间**: `XCEngine::Components`
**类型**: `module`
**描述**: XCEngine 的场景管理模块,提供场景创建、加载和 GameObject 管理功能。
## 概述
Scene 模块是 XCEngine 中管理游戏场景的核心模块。它提供了 Scene 类用于管理场景中的所有 GameObject以及 SceneManager 单例用于管理多个场景、场景切换和生命周期。场景是 GameObject 的容器,支持场景保存/加载、子对象查找、事件通知等功能。
## 模块内容
### 核心类
| 组件 | 文件 | 描述 |
|------|------|------|
| [Scene](scene/scene.md) | `Scene.h` | 场景类,管理 GameObject 层级和生命周期 |
| [SceneManager](scene-manager/scene-manager.md) | `SceneManager.h` | 场景管理器单例,管理所有场景 |
## 使用示例
```cpp
#include <XCEngine/Scene/SceneManager.h>
#include <XCEngine/Scene/Scene.h>
#include <XCEngine/Components/GameObject.h>
using namespace XCEngine::Components;
void CreateNewScene() {
Scene* scene = SceneManager::Get().CreateScene("MainMenu");
SceneManager::Get().SetActiveScene(scene);
GameObject* camera = scene->CreateGameObject("MainCamera");
}
void LoadSceneExample() {
SceneManager::Get().LoadScene("levels/gameplay.json");
}
```
## 相关文档
- [Components 模块](../components/components.md) - Components 模块总览
- [GameObject](../components/game-object/game-object.md) - 游戏对象

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);
}
```