ShitEngine 1.4.2
基于 C++20 与 SDL3 的轻量级 2D 游戏引擎
载入中...
搜索中...
未找到
Prefab.h
浏览该文件的文档.
1#pragma once
2#include "GameObject.h"
3#include <nlohmann/json.hpp>
4#include <string>
5#include <vector>
6
7namespace Shit {
8
9class Scene;
10class TypeInfo;
11
23class SHIT_API Prefab {
24public:
27 std::string typeName;
28 uint64_t uuid = 0;
29 nlohmann::json fields;
30 nlohmann::json properties;
31 };
32
34 static Prefab Capture(GameObject* source);
35
37 static Prefab FromJson(const nlohmann::json& j);
38
40 nlohmann::json toJson() const;
41
43 void apply(GameObject* go) const;
44
46 GameObject* instantiate(Scene* scene, const std::string& name = "") const;
47
49 bool hasData() const { return !m_components.empty(); }
50
52 const std::vector<ComponentData>& getComponents() const { return m_components; }
53
54private:
55 Prefab() = default;
56
59 void applyInternal(GameObject* go, bool restoreUuid) const;
60
63 void applyProperties(const TypeInfo* ti, const ComponentData& data, Component* comp) const;
64
65 std::vector<ComponentData> m_components;
66};
67
68} // namespace Shit
#define SHIT_API
定义 Core.h:14
组件基类
定义 Component.h:25
游戏物体类
定义 GameObject.h:23
预制体,可重复生成相同配置的 GameObject
定义 Prefab.h:23
nlohmann::json toJson() const
序列化为 JSON
static Prefab Capture(GameObject *source)
从现有 GameObject 捕获组件与字段(跳过 readOnly 字段与不可序列化类型)
const std::vector< ComponentData > & getComponents() const
已捕获的组件数据(只读,供编辑器序列化/预览)
定义 Prefab.h:52
bool hasData() const
是否包含反射数据
定义 Prefab.h:49
void apply(GameObject *go) const
把此预制体应用到已有 GameObject(反射克隆;恢复记录的组件 UUID)
static Prefab FromJson(const nlohmann::json &j)
从 JSON 反序列化
GameObject * instantiate(Scene *scene, const std::string &name="") const
创建新 GameObject 并应用此预制体
场景类
定义 Scene.h:36
定义 AnimationClip.h:12
单个组件的数据(类型名 + 持久 UUID + 反射字段值,JSON 可序列化)
定义 Prefab.h:26
nlohmann::json fields
字段名 → 值(仅反射且可序列化的字段)
定义 Prefab.h:29
uint64_t uuid
组件持久 ID(0 = 未记录,加载时现场分配)
定义 Prefab.h:28
std::string typeName
组件类型名(TypeRegistry::Get 查询)
定义 Prefab.h:27
nlohmann::json properties
属性名 → 值(SHIT_PROPERTY getter/setter 对,可读写者随档)
定义 Prefab.h:30
定义 TypeInfo.h:96