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,67 @@
# Component
**命名空间**: `XCEngine::Components`
**类型**: `class (abstract)`
**头文件**: `XCEngine/Components/Component.h`
**描述**: ECS 组件基类,定义所有组件的公共接口和生命周期。
## 概述
Component 是 XCEngine ECS 架构中的组件基类,所有具体组件(如 TransformComponent、AudioSourceComponent都继承自此类。它定义了组件的通用生命周期方法Awake、Start、Update、OnEnable、OnDisable、OnDestroy以及获取所属 GameObject 和场景的接口。组件通过友元类 GameObject 自动管理其所属游戏对象。
## 公共方法
| 方法 | 描述 |
|------|------|
| [`GetName`](get-name.md) | 获取组件名称(纯虚函数) |
| [`GetGameObject`](get-game-object.md) | 获取所属 GameObject |
| [`transform`](transform.md) | 获取变换组件引用 |
| [`GetScene`](get-scene.md) | 获取所属场景 |
| [`IsEnabled`](is-enabled.md) | 检查组件是否启用 |
| [`SetEnabled`](set-enabled.md) | 设置组件启用状态 |
## 虚方法(生命周期)
| 方法 | 描述 |
|------|------|
| `virtual void Awake()` | 组件创建后首次启用时调用 |
| `virtual void Start()` | 在首次更新前调用 |
| `virtual void Update(float deltaTime)` | 每帧更新 |
| `virtual void FixedUpdate()` | 固定频率更新(物理) |
| `virtual void LateUpdate(float deltaTime)` | 晚于 Update 更新 |
| `virtual void OnEnable()` | 启用时调用 |
| `virtual void OnDisable()` | 禁用时调用 |
| `virtual void OnDestroy()` | 销毁时调用 |
## 使用示例
```cpp
#include <XCEngine/Components/Component.h>
#include <XCEngine/Components/GameObject.h>
using namespace XCEngine::Components;
class MyComponent : public Component {
public:
void Awake() override {
printf("Awake: %s\n", GetName().c_str());
}
void Update(float deltaTime) override {
// 每帧逻辑
}
std::string GetName() const override {
return "MyComponent";
}
};
```
## 相关文档
- [Components 模块总览](../components.md) - Components 模块总览
- [GameObject](game-object/game-object.md) - 游戏对象
- [TransformComponent](transform-component/transform-component.md) - 变换组件

View File

@@ -0,0 +1,13 @@
# Component::GetGameObject
获取所属的 GameObject。
```cpp
GameObject* GetGameObject() const;
```
**返回:** `GameObject*` - 所属游戏对象指针,如果组件未附加到游戏对象则返回 nullptr
## 相关文档
- [Component 总览](component.md)

View File

@@ -0,0 +1,13 @@
# Component::GetName
获取组件名称。
```cpp
virtual std::string GetName() const = 0;
```
**返回:** `std::string` - 组件名称
## 相关文档
- [Component 总览](component.md)

View File

@@ -0,0 +1,14 @@
# Component::GetScene
获取所属的场景。
```cpp
Scene* GetScene() const;
```
**返回:** `Scene*` - 所属场景指针,如果组件未附加到游戏对象则返回 nullptr
## 相关文档
- [Component 总览](component.md)
- [Scene](../scene/scene/scene.md)

View File

@@ -0,0 +1,14 @@
# Component::IsEnabled
检查组件是否启用。
```cpp
bool IsEnabled() const;
```
**返回:** `bool` - 如果组件启用则返回 true
## 相关文档
- [Component 总览](component.md)
- [SetEnabled](set-enabled.md)

View File

@@ -0,0 +1,17 @@
# Component::SetEnabled
设置组件启用状态。
```cpp
void SetEnabled(bool enabled);
```
当启用状态发生变化时,会自动调用 OnEnable 或 OnDisable 回调。
**参数:**
- `enabled` - true 为启用false 为禁用
## 相关文档
- [Component 总览](component.md)
- [IsEnabled](is-enabled.md)

View File

@@ -0,0 +1,16 @@
# Component::transform
获取变换组件引用。
```cpp
TransformComponent& transform() const;
```
**返回:** `TransformComponent&` - 所属 GameObject 的变换组件引用
**注意:** 如果组件未附加到 GameObject 或 GameObject 没有 TransformComponent可能导致未定义行为。
## 相关文档
- [Component 总览](component.md)
- [TransformComponent](transform-component/transform-component.md)

View File

@@ -12,6 +12,19 @@ Components 模块是 XCEngine ECS 架构中的组件层,提供各种游戏对
## 模块内容
### 核心组件
| 组件 | 文件 | 描述 |
|------|------|------|
| [Component](component/component.md) | `Component.h` | 组件基类,所有组件的父类 |
| [GameObject](game-object/game-object.md) | `GameObject.h` | 游戏对象实体 |
### 变换组件
| 组件 | 文件 | 描述 |
|------|------|------|
| [TransformComponent](transform-component/transform-component.md) | `TransformComponent.h` | 变换组件,包含位置、旋转、缩放 |
### 音频组件
| 组件 | 文件 | 描述 |
@@ -19,12 +32,6 @@ Components 模块是 XCEngine ECS 架构中的组件层,提供各种游戏对
| [AudioSourceComponent](audio-source/audio-source-component.md) | `AudioSourceComponent.h` | 音频源组件,负责播放音频 |
| [AudioListenerComponent](audio-listener/audio-listener-component.md) | `AudioListenerComponent.h` | 音频监听器组件,接收声音 |
### 变换组件
| 组件 | 文件 | 描述 |
|------|------|------|
| TransformComponent | `TransformComponent.h` | 变换组件,包含位置、旋转、缩放 |
## 使用示例
```cpp

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)

View File

@@ -0,0 +1,26 @@
# DetachChildren
Detach all child transforms.
## Syntax
```cpp
void DetachChildren();
```
## Remarks
Removes all children from this transform. The children become root-level transforms (their parent is set to `nullptr`).
## See Also
- [SetParent](set-parent)
- [GetChild](get-child)
## Examples
```cpp
void Example(TransformComponent* transform) {
transform->DetachChildren();
}
```

View File

@@ -0,0 +1,33 @@
# Find
Find a child transform by name.
## Syntax
```cpp
TransformComponent* Find(const std::string& name) const;
```
## Parameters
- `name` - The name of the child transform to find.
## Returns
Returns a pointer to the child transform with the specified name, or `nullptr` if not found. Only searches direct children, not the entire hierarchy.
## See Also
- [GetChild](get-child)
- [GetChildCount](get-child-count)
## Examples
```cpp
void Example(TransformComponent* transform) {
TransformComponent* child = transform->Find("Weapon");
if (child) {
XC_LOG_INFO("Found child: {}", child->GetName());
}
}
```

View File

@@ -0,0 +1,27 @@
# GetChildCount
Get the number of child transforms.
## Syntax
```cpp
size_t GetChildCount() const;
```
## Returns
Returns the number of direct child transforms.
## See Also
- [GetChild](get-child)
- [GetParent](get-parent)
## Examples
```cpp
void Example(TransformComponent* transform) {
size_t count = transform->GetChildCount();
XC_LOG_INFO("Child count: {}", count);
}
```

View File

@@ -0,0 +1,32 @@
# GetChild
Get a child transform by index.
## Syntax
```cpp
TransformComponent* GetChild(size_t index) const;
```
## Parameters
- `index` - The index of the child transform (0-based).
## Returns
Returns a pointer to the child transform at the specified index, or `nullptr` if the index is out of range.
## See Also
- [GetChildCount](get-child-count)
## Examples
```cpp
void Example(TransformComponent* transform) {
TransformComponent* firstChild = transform->GetChild(0);
if (firstChild) {
XC_LOG_INFO("First child: {}", firstChild->GetName());
}
}
```

View File

@@ -0,0 +1,31 @@
# GetForward
Get the forward direction vector in world space.
## Syntax
```cpp
Math::Vector3 GetForward() const;
```
## Returns
Returns the forward direction vector as a `Math::Vector3`.
## Remarks
Returns the forward direction of this transform in world space, based on the world rotation. By convention, forward is typically the negative Z axis direction.
## See Also
- [GetRight](get-right)
- [GetUp](get-up)
## Examples
```cpp
void Example(TransformComponent* transform) {
Math::Vector3 forward = transform->GetForward();
XC_LOG_INFO("Forward: ({}, {}, {})", forward.x, forward.y, forward.z);
}
```

View File

@@ -0,0 +1,31 @@
# GetLocalEulerAngles
Get the local rotation as Euler angles in degrees.
## Syntax
```cpp
Math::Vector3 GetLocalEulerAngles() const;
```
## Returns
Returns the local rotation as Euler angles `(x, y, z)` in degrees.
## Remarks
Converts the local quaternion rotation to Euler angles representation. The angles represent rotation around the X, Y, and Z axes respectively.
## See Also
- [SetLocalEulerAngles](set-local-euler-angles)
- [GetLocalRotation](get-local-rotation)
## Examples
```cpp
void Example(TransformComponent* transform) {
Math::Vector3 eulers = transform->GetLocalEulerAngles();
XC_LOG_INFO("Local euler angles: ({}, {}, {})", eulers.x, eulers.y, eulers.z);
}
```

View File

@@ -0,0 +1,33 @@
# GetLocalPosition
Get the position of this transform relative to the parent transform.
## Syntax
```cpp
const Math::Vector3& GetLocalPosition() const;
```
## Returns
Returns the local position as a `Math::Vector3` reference. The returned reference remains valid until the transform is modified.
## Remarks
Local position represents the position of this transform in parent space. If this transform has no parent, local position is the same as world position.
## See Also
- [SetLocalPosition](set-local-position)
- [GetPosition](get-position)
- [SetPosition](set-position)
## Examples
```cpp
void Example(TransformComponent* transform) {
const Math::Vector3& localPos = transform->GetLocalPosition();
XC_LOG_INFO("Local position: ({}, {}, {})",
localPos.x, localPos.y, localPos.z);
}
```

View File

@@ -0,0 +1,33 @@
# GetLocalRotation
Get the rotation of this transform relative to the parent transform.
## Syntax
```cpp
const Math::Quaternion& GetLocalRotation() const;
```
## Returns
Returns the local rotation as a `Math::Quaternion` reference.
## Remarks
Local rotation represents the rotation of this transform in parent space. If this transform has no parent, local rotation is the same as world rotation.
## See Also
- [SetLocalRotation](set-local-rotation)
- [GetRotation](get-rotation)
- [SetRotation](set-rotation)
## Examples
```cpp
void Example(TransformComponent* transform) {
const Math::Quaternion& localRot = transform->GetLocalRotation();
XC_LOG_INFO("Local rotation: ({}, {}, {}, {})",
localRot.x, localRot.y, localRot.z, localRot.w);
}
```

View File

@@ -0,0 +1,31 @@
# GetLocalScale
Get the scale of this transform relative to the parent transform.
## Syntax
```cpp
const Math::Vector3& GetLocalScale() const;
```
## Returns
Returns the local scale as a `Math::Vector3` reference.
## Remarks
Local scale represents the scale of this transform in parent space. If this transform has no parent, local scale is the same as world scale.
## See Also
- [SetLocalScale](set-local-scale)
- [GetScale](get-scale)
## Examples
```cpp
void Example(TransformComponent* transform) {
const Math::Vector3& scale = transform->GetLocalScale();
XC_LOG_INFO("Local scale: ({}, {}, {})", scale.x, scale.y, scale.z);
}
```

View File

@@ -0,0 +1,29 @@
# GetLocalToWorldMatrix
Get the transformation matrix from local space to world space.
## Syntax
```cpp
const Math::Matrix4x4& GetLocalToWorldMatrix() const;
```
## Returns
Returns the local-to-world transformation matrix as a `Math::Matrix4x4` reference.
## Remarks
Returns the matrix that transforms a point from local space to world space. The matrix is computed from the local position, rotation, and scale combined with the parent's world transform. Uses lazy evaluation and is cached until the transform is marked dirty.
## See Also
- [GetWorldToLocalMatrix](get-world-to-local-matrix)
## Examples
```cpp
void Example(TransformComponent* transform) {
const Math::Matrix4x4& matrix = transform->GetLocalToWorldMatrix();
}
```

View File

@@ -0,0 +1,32 @@
# GetParent
Get the parent transform.
## Syntax
```cpp
TransformComponent* GetParent() const;
```
## Returns
Returns a pointer to the parent `TransformComponent`, or `nullptr` if this transform has no parent.
## Remarks
Returns the parent transform in the transform hierarchy. The parent transform affects this transform's world position, rotation, and scale.
## See Also
- [SetParent](set-parent)
## Examples
```cpp
void Example(TransformComponent* transform) {
TransformComponent* parent = transform->GetParent();
if (parent) {
XC_LOG_INFO("Has parent: {}", parent->GetName());
}
}
```

View File

@@ -0,0 +1,33 @@
# GetPosition
Get the world position of this transform.
## Syntax
```cpp
Math::Vector3 GetPosition() const;
```
## Returns
Returns the world position as a `Math::Vector3`.
## Remarks
Returns the world-space position of this transform. If this transform has a parent, the world position is computed by combining the local position with the parent's world position.
## See Also
- [GetLocalPosition](get-local-position)
- [SetLocalPosition](set-local-position)
- [SetPosition](set-position)
## Examples
```cpp
void Example(TransformComponent* transform) {
Math::Vector3 worldPos = transform->GetPosition();
XC_LOG_INFO("World position: ({}, {}, {})",
worldPos.x, worldPos.y, worldPos.z);
}
```

View File

@@ -0,0 +1,30 @@
# GetRight
Get the right direction vector in world space.
## Syntax
```cpp
Math::Vector3 GetRight() const;
```
## Returns
Returns the right direction vector as a `Math::Vector3`.
## Remarks
Returns the right direction of this transform in world space, based on the world rotation. By convention, right is typically the positive X axis direction.
## See Also
- [GetForward](get-forward)
- [GetUp](get-up)
## Examples
```cpp
void Example(TransformComponent* transform) {
Math::Vector3 right = transform->GetRight();
}
```

View File

@@ -0,0 +1,31 @@
# GetRotation
Get the world rotation of this transform.
## Syntax
```cpp
Math::Quaternion GetRotation() const;
```
## Returns
Returns the world rotation as a `Math::Quaternion`.
## Remarks
Returns the world-space rotation of this transform. If this transform has a parent, the world rotation is computed by combining the local rotation with the parent's world rotation.
## See Also
- [GetLocalRotation](get-local-rotation)
- [SetLocalRotation](set-local-rotation)
- [SetRotation](set-rotation)
## Examples
```cpp
void Example(TransformComponent* transform) {
Math::Quaternion worldRot = transform->GetRotation();
}
```

View File

@@ -0,0 +1,31 @@
# GetScale
Get the world scale of this transform.
## Syntax
```cpp
Math::Vector3 GetScale() const;
```
## Returns
Returns the world scale as a `Math::Vector3`.
## Remarks
Returns the world-space scale of this transform. If this transform has a parent, the world scale is computed by multiplying the local scale with the parent's world scale.
## See Also
- [GetLocalScale](get-local-scale)
- [SetLocalScale](set-local-scale)
## Examples
```cpp
void Example(TransformComponent* transform) {
Math::Vector3 worldScale = transform->GetScale();
XC_LOG_INFO("World scale: ({}, {}, {})", worldScale.x, worldScale.y, worldScale.z);
}
```

View File

@@ -0,0 +1,28 @@
# GetSiblingIndex
Get the sibling index of this transform among siblings.
## Syntax
```cpp
int GetSiblingIndex() const;
```
## Returns
Returns the sibling index of this transform.
## See Also
- [SetSiblingIndex](set-sibling-index)
- [SetAsFirstSibling](set-as-first-sibling)
- [SetAsLastSibling](set-as-last-sibling)
## Examples
```cpp
void Example(TransformComponent* transform) {
int index = transform->GetSiblingIndex();
XC_LOG_INFO("Sibling index: {}", index);
}
```

View File

@@ -0,0 +1,30 @@
# GetUp
Get the up direction vector in world space.
## Syntax
```cpp
Math::Vector3 GetUp() const;
```
## Returns
Returns the up direction vector as a `Math::Vector3`.
## Remarks
Returns the up direction of this transform in world space, based on the world rotation. By convention, up is typically the positive Y axis direction.
## See Also
- [GetForward](get-forward)
- [GetRight](get-right)
## Examples
```cpp
void Example(TransformComponent* transform) {
Math::Vector3 up = transform->GetUp();
}
```

View File

@@ -0,0 +1,29 @@
# GetWorldToLocalMatrix
Get the transformation matrix from world space to local space.
## Syntax
```cpp
Math::Matrix4x4 GetWorldToLocalMatrix() const;
```
## Returns
Returns the world-to-local transformation matrix as a `Math::Matrix4x4`.
## Remarks
Returns the matrix that transforms a point from world space to local space. This is the inverse of `GetLocalToWorldMatrix()`.
## See Also
- [GetLocalToWorldMatrix](get-local-to-world-matrix)
## Examples
```cpp
void Example(TransformComponent* transform) {
Math::Matrix4x4 worldToLocal = transform->GetWorldToLocalMatrix();
}
```

View File

@@ -0,0 +1,35 @@
# InverseTransformDirection
Transform a direction from world space to local space.
## Syntax
```cpp
Math::Vector3 InverseTransformDirection(const Math::Vector3& direction) const;
```
## Parameters
- `direction` - The world space direction to transform.
## Returns
Returns the transformed direction in local space.
## Remarks
Transforms a direction from world space to local space. Unlike `InverseTransformPoint`, this does not consider translation. Only rotation and scale are applied.
## See Also
- [TransformDirection](transform-direction)
- [InverseTransformPoint](inverse-transform-point)
## Examples
```cpp
void Example(TransformComponent* transform) {
Math::Vector3 worldDir(1.0f, 0.0f, 0.0f);
Math::Vector3 localDir = transform->InverseTransformDirection(worldDir);
}
```

View File

@@ -0,0 +1,35 @@
# InverseTransformPoint
Transform a point from world space to local space.
## Syntax
```cpp
Math::Vector3 InverseTransformPoint(const Math::Vector3& point) const;
```
## Parameters
- `point` - The world space point to transform.
## Returns
Returns the transformed point in local space.
## Remarks
Transforms a point from world space to local space using the transform's world-to-local matrix.
## See Also
- [TransformPoint](transform-point)
- [InverseTransformDirection](inverse-transform-direction)
## Examples
```cpp
void Example(TransformComponent* transform) {
Math::Vector3 worldPoint(10.0f, 0.0f, 0.0f);
Math::Vector3 localPoint = transform->InverseTransformPoint(worldPoint);
}
```

View File

@@ -0,0 +1,28 @@
# LookAt
Rotate the transform to face a target position.
## Syntax
```cpp
void LookAt(const Math::Vector3& target);
void LookAt(const Math::Vector3& target, const Math::Vector3& up);
```
## Parameters
- `target` - The position to look at.
- `up` - The up vector to use (defaults to world up).
## Remarks
Rotates the transform so its forward direction points toward the target position. The optional `up` vector defines which direction is considered up.
## Examples
```cpp
void Example(TransformComponent* transform) {
Math::Vector3 targetPos(10.0f, 0.0f, 5.0f);
transform->LookAt(targetPos);
}
```

View File

@@ -0,0 +1,29 @@
# Rotate
Rotate the transform by the specified angles.
## Syntax
```cpp
void Rotate(const Math::Vector3& eulers, Space relativeTo = Space::Self);
void Rotate(const Math::Vector3& axis, float angle, Space relativeTo = Space::Self);
```
## Parameters
- `eulers` - The Euler angles of rotation (x, y, z).
- `axis` - The axis of rotation.
- `angle` - The angle of rotation in degrees.
- `relativeTo` - Whether the rotation is relative to self or world space.
## Remarks
Applies a rotation to the transform. When `relativeTo` is `Space::Self`, the rotation is applied in local space. When `Space::World`, the rotation is applied in world space.
## Examples
```cpp
void Example(TransformComponent* transform) {
transform->Rotate(Math::Vector3(0.0f, 45.0f, 0.0f), Space::Self);
}
```

View File

@@ -0,0 +1,22 @@
# SetAsFirstSibling
Set this transform as the first sibling (index 0).
## Syntax
```cpp
void SetAsFirstSibling();
```
## See Also
- [SetSiblingIndex](set-sibling-index)
- [SetAsLastSibling](set-as-last-sibling)
## Examples
```cpp
void Example(TransformComponent* transform) {
transform->SetAsFirstSibling();
}
```

View File

@@ -0,0 +1,22 @@
# SetAsLastSibling
Set this transform as the last sibling.
## Syntax
```cpp
void SetAsLastSibling();
```
## See Also
- [SetSiblingIndex](set-sibling-index)
- [SetAsFirstSibling](set-as-first-sibling)
## Examples
```cpp
void Example(TransformComponent* transform) {
transform->SetAsLastSibling();
}
```

View File

@@ -0,0 +1,30 @@
# SetLocalEulerAngles
Set the local rotation using Euler angles in degrees.
## Syntax
```cpp
void SetLocalEulerAngles(const Math::Vector3& eulers);
```
## Parameters
- `eulers` - The Euler angles `(x, y, z)` in degrees.
## Remarks
Sets the local rotation by converting the Euler angles to a quaternion. The angles represent rotation around the X, Y, and Z axes respectively.
## See Also
- [GetLocalEulerAngles](get-local-euler-angles)
- [SetLocalRotation](set-local-rotation)
## Examples
```cpp
void Example(TransformComponent* transform) {
transform->SetLocalEulerAngles(Math::Vector3(45.0f, 90.0f, 0.0f));
}
```

View File

@@ -0,0 +1,31 @@
# SetLocalPosition
Set the position of this transform relative to the parent transform.
## Syntax
```cpp
void SetLocalPosition(const Math::Vector3& position);
```
## Parameters
- `position` - The new local position as a `Math::Vector3`.
## Remarks
Sets the local position of this transform. If this transform has no parent, local position is the same as world position. This marks the transform as dirty, requiring world transform recalculation.
## See Also
- [GetLocalPosition](get-local-position)
- [GetPosition](get-position)
- [SetPosition](set-position)
## Examples
```cpp
void Example(TransformComponent* transform) {
transform->SetLocalPosition(Math::Vector3(10.0f, 0.0f, 5.0f));
}
```

View File

@@ -0,0 +1,32 @@
# SetLocalRotation
Set the rotation of this transform relative to the parent transform.
## Syntax
```cpp
void SetLocalRotation(const Math::Quaternion& rotation);
```
## Parameters
- `rotation` - The new local rotation as a `Math::Quaternion`.
## Remarks
Sets the local rotation of this transform. If this transform has no parent, local rotation is the same as world rotation. This marks the transform as dirty.
## See Also
- [GetLocalRotation](get-local-rotation)
- [GetRotation](get-rotation)
- [SetRotation](set-rotation)
## Examples
```cpp
void Example(TransformComponent* transform) {
Math::Quaternion rotation = Math::Quaternion::FromEuler(0.0f, 90.0f, 0.0f);
transform->SetLocalRotation(rotation);
}
```

View File

@@ -0,0 +1,30 @@
# SetLocalScale
Set the scale of this transform relative to the parent transform.
## Syntax
```cpp
void SetLocalScale(const Math::Vector3& scale);
```
## Parameters
- `scale` - The new local scale as a `Math::Vector3`.
## Remarks
Sets the local scale of this transform. If this transform has no parent, local scale is the same as world scale. This marks the transform as dirty.
## See Also
- [GetLocalScale](get-local-scale)
- [GetScale](get-scale)
## Examples
```cpp
void Example(TransformComponent* transform) {
transform->SetLocalScale(Math::Vector3(2.0f, 2.0f, 2.0f));
}
```

View File

@@ -0,0 +1,31 @@
# SetParent
Set the parent transform.
## Syntax
```cpp
void SetParent(TransformComponent* parent, bool worldPositionStays = true);
```
## Parameters
- `parent` - The new parent transform, or `nullptr` to detach from parent.
- `worldPositionStays` - If `true`, the world position remains unchanged after reparenting.
## Remarks
Sets the parent of this transform. When `worldPositionStays` is `true`, the world position is preserved, which may adjust the local position. When `false`, the local position is preserved, which may adjust the world position.
## See Also
- [GetParent](get-parent)
- [DetachChildren](detach-children)
## Examples
```cpp
void Example(TransformComponent* child, TransformComponent* newParent) {
child->SetParent(newParent, true);
}
```

View File

@@ -0,0 +1,31 @@
# SetPosition
Set the world position of this transform.
## Syntax
```cpp
void SetPosition(const Math::Vector3& position);
```
## Parameters
- `position` - The new world position as a `Math::Vector3`.
## Remarks
Sets the world-space position of this transform. If this transform has a parent, the local position is computed by transforming the world position into parent space.
## See Also
- [GetPosition](get-position)
- [GetLocalPosition](get-local-position)
- [SetLocalPosition](set-local-position)
## Examples
```cpp
void Example(TransformComponent* transform) {
transform->SetPosition(Math::Vector3(100.0f, 50.0f, 25.0f));
}
```

View File

@@ -0,0 +1,32 @@
# SetRotation
Set the world rotation of this transform.
## Syntax
```cpp
void SetRotation(const Math::Quaternion& rotation);
```
## Parameters
- `rotation` - The new world rotation as a `Math::Quaternion`.
## Remarks
Sets the world-space rotation of this transform. If this transform has a parent, the local rotation is computed by transforming the world rotation into parent space.
## See Also
- [GetRotation](get-rotation)
- [GetLocalRotation](get-local-rotation)
- [SetLocalRotation](set-local-rotation)
## Examples
```cpp
void Example(TransformComponent* transform) {
Math::Quaternion rot = Math::Quaternion::FromEuler(0.0f, 180.0f, 0.0f);
transform->SetRotation(rot);
}
```

View File

@@ -0,0 +1,31 @@
# SetScale
Set the world scale of this transform.
## Syntax
```cpp
void SetScale(const Math::Vector3& scale);
```
## Parameters
- `scale` - The new world scale as a `Math::Vector3`.
## Remarks
Sets the world-space scale of this transform. If this transform has a parent, the local scale is computed accordingly.
## See Also
- [GetScale](get-scale)
- [GetLocalScale](get-local-scale)
- [SetLocalScale](set-local-scale)
## Examples
```cpp
void Example(TransformComponent* transform) {
transform->SetScale(Math::Vector3(1.5f, 1.5f, 1.5f));
}
```

View File

@@ -0,0 +1,27 @@
# SetSiblingIndex
Set the sibling index of this transform.
## Syntax
```cpp
void SetSiblingIndex(int index);
```
## Parameters
- `index` - The new sibling index.
## See Also
- [GetSiblingIndex](get-sibling-index)
- [SetAsFirstSibling](set-as-first-sibling)
- [SetAsLastSibling](set-as-last-sibling)
## Examples
```cpp
void Example(TransformComponent* transform) {
transform->SetSiblingIndex(0);
}
```

View File

@@ -0,0 +1,126 @@
# TransformComponent
**命名空间**: `XCEngine::Components`
**类型**: `class`
**头文件**: `XCEngine/Components/TransformComponent.h`
**描述**: 游戏对象变换组件,管理位置、旋转、缩放和层级关系。
## 概述
TransformComponent 是 XCEngine ECS 系统中的变换组件,每个 GameObject 都拥有一个 TransformComponent。它管理对象的局部空间Local和世界空间World位置、旋转四元数和缩放支持父子层级变换、方向向量计算前后左右上下、矩阵运算和坐标变换。变换组件使用延迟更新机制只有在需要时才重新计算世界变换矩阵。
## 枚举
| 枚举值 | 描述 |
|--------|------|
| `Space::Self` | 相对于自身空间 |
| `Space::World` | 相对于世界空间 |
## 公共方法
### 局部空间
| 方法 | 描述 |
|------|------|
| [`GetLocalPosition`](get-local-position.md) | 获取局部位置 |
| [`SetLocalPosition`](set-local-position.md) | 设置局部位置 |
| [`GetLocalRotation`](get-local-rotation.md) | 获取局部旋转 |
| [`SetLocalRotation`](set-local-rotation.md) | 设置局部旋转 |
| [`GetLocalScale`](get-local-scale.md) | 获取局部缩放 |
| [`SetLocalScale`](set-local-scale.md) | 设置局部缩放 |
| [`GetLocalEulerAngles`](get-local-euler-angles.md) | 获取局部欧拉角 |
| [`SetLocalEulerAngles`](set-local-euler-angles.md) | 设置局部欧拉角 |
### 世界空间
| 方法 | 描述 |
|------|------|
| [`GetPosition`](get-position.md) | 获取世界位置 |
| [`SetPosition`](set-position.md) | 设置世界位置 |
| [`GetRotation`](get-rotation.md) | 获取世界旋转 |
| [`SetRotation`](set-rotation.md) | 设置世界旋转 |
| [`GetScale`](get-scale.md) | 获取世界缩放 |
| [`SetScale`](set-scale.md) | 设置世界缩放 |
### 方向向量
| 方法 | 描述 |
|------|------|
| [`GetForward`](get-forward.md) | 获取前方方向 |
| [`GetRight`](get-right.md) | 获取右方方向 |
| [`GetUp`](get-up.md) | 获取上方方向 |
### 矩阵运算
| 方法 | 描述 |
|------|------|
| [`GetLocalToWorldMatrix`](get-local-to-world-matrix.md) | 获取局部到世界矩阵 |
| [`GetWorldToLocalMatrix`](get-world-to-local-matrix.md) | 获取世界到局部矩阵 |
### 层级结构
| 方法 | 描述 |
|------|------|
| [`GetParent`](get-parent.md) | 获取父变换 |
| [`SetParent`](set-parent.md) | 设置父变换 |
| [`GetChildCount`](get-child-count.md) | 获取子对象数量 |
| [`GetChild`](get-child.md) | 获取指定索引的子变换 |
| [`Find`](find.md) | 按名称查找子变换 |
| [`DetachChildren`](detach-children.md) | 分离所有子变换 |
### 同级顺序
| 方法 | 描述 |
|------|------|
| [`GetSiblingIndex`](get-sibling-index.md) | 获取同级索引 |
| [`SetSiblingIndex`](set-sibling-index.md) | 设置同级索引 |
| [`SetAsFirstSibling`](set-as-first-sibling.md) | 设为第一个同级 |
| [`SetAsLastSibling`](set-as-last-sibling.md) | 设为最后一个同级 |
### 变换操作
| 方法 | 描述 |
|------|------|
| [`LookAt`](look-at.md) | 指向目标 |
| [`Rotate`](rotate.md) | 旋转 |
| [`Translate`](translate.md) | 平移 |
### 坐标变换
| 方法 | 描述 |
|------|------|
| [`TransformPoint`](transform-point.md) | 变换点坐标 |
| [`InverseTransformPoint`](inverse-transform-point.md) | 逆变换点坐标 |
| [`TransformDirection`](transform-direction.md) | 变换方向向量 |
| [`InverseTransformDirection`](inverse-transform-direction.md) | 逆变换方向向量 |
## 使用示例
```cpp
#include <XCEngine/Components/TransformComponent.h>
using namespace XCEngine::Components;
void TransformExample(TransformComponent* transform) {
transform->SetPosition(Vector3(10, 0, 0));
transform->SetRotation(Quaternion::FromEuler(0, 90, 0));
Vector3 forward = transform->GetForward();
Vector3 right = transform->GetRight();
transform->Translate(Vector3(0, 0, 5), Space::Self);
transform->Rotate(Vector3(0, 45, 0), Space::World);
Vector3 worldPos = transform->GetPosition();
Matrix4x4 l2w = transform->GetLocalToWorldMatrix();
}
```
## 相关文档
- [Components 模块总览](../components.md) - Components 模块总览
- [Component](component/component.md) - 组件基类
- [GameObject](game-object/game-object.md) - 游戏对象

View File

@@ -0,0 +1,35 @@
# TransformDirection
Transform a direction from local space to world space.
## Syntax
```cpp
Math::Vector3 TransformDirection(const Math::Vector3& direction) const;
```
## Parameters
- `direction` - The local space direction to transform.
## Returns
Returns the transformed direction in world space.
## Remarks
Transforms a direction from local space to world space. Unlike `TransformPoint`, this does not consider translation. Only rotation and scale are applied.
## See Also
- [InverseTransformDirection](inverse-transform-direction)
- [TransformPoint](transform-point)
## Examples
```cpp
void Example(TransformComponent* transform) {
Math::Vector3 localDir(1.0f, 0.0f, 0.0f);
Math::Vector3 worldDir = transform->TransformDirection(localDir);
}
```

View File

@@ -0,0 +1,35 @@
# TransformPoint
Transform a point from local space to world space.
## Syntax
```cpp
Math::Vector3 TransformPoint(const Math::Vector3& point) const;
```
## Parameters
- `point` - The local space point to transform.
## Returns
Returns the transformed point in world space.
## Remarks
Transforms a point from local space to world space using the transform's local-to-world matrix.
## See Also
- [InverseTransformPoint](inverse-transform-point)
- [TransformDirection](transform-direction)
## Examples
```cpp
void Example(TransformComponent* transform) {
Math::Vector3 localPoint(1.0f, 0.0f, 0.0f);
Math::Vector3 worldPoint = transform->TransformPoint(localPoint);
}
```

View File

@@ -0,0 +1,26 @@
# Translate
Move the transform by the specified translation.
## Syntax
```cpp
void Translate(const Math::Vector3& translation, Space relativeTo = Space::Self);
```
## Parameters
- `translation` - The translation vector.
- `relativeTo` - Whether the translation is relative to self or world space.
## Remarks
Moves the transform by the specified amount. When `relativeTo` is `Space::Self`, the translation is applied in local space. When `Space::World`, the translation is applied in world space.
## Examples
```cpp
void Example(TransformComponent* transform) {
transform->Translate(Math::Vector3(0.0f, 0.0f, 10.0f), Space::Self);
}
```

View File

@@ -20,6 +20,7 @@
| **Resources** | [resources/](resources/resources.md) | 资源管理系统 |
| **Audio** | [audio/](audio/audio.md) | 音频系统 |
| **Components** | [components/](components/components.md) | ECS 组件系统 |
| **Scene** | [scene/](scene/scene.md) | 场景管理系统 |
---
@@ -86,7 +87,10 @@ Math::Matrix4 transform = Math::Matrix4::TRS(position, rotation, scale);
| Debug | 9 |
| Core | 6 |
| Containers | 4 |
| **总计** | **124** |
| Audio | 10 |
| Components | 36 |
| Scene | 19 |
| **总计** | **143** |
---

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

Some files were not shown because too many files have changed in this diff Show More