ShitEngine
基于 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;
10
22class SHIT_API Prefab {
23public:
26 std::string typeName;
27 nlohmann::json fields;
28 };
29
31 static Prefab Capture(GameObject* source);
32
34 static Prefab FromJson(const nlohmann::json& j);
35
37 nlohmann::json toJson() const;
38
40 void apply(GameObject* go) const;
41
43 GameObject* instantiate(Scene* scene, const std::string& name = "") const;
44
46 bool hasData() const { return !m_components.empty(); }
47
49 const std::vector<ComponentData>& getComponents() const { return m_components; }
50
51private:
52 Prefab() = default;
53
54 std::vector<ComponentData> m_components;
55};
56
57} // namespace Shit
#define SHIT_API
定义 Core.h:14
游戏物体类
定义 GameObject.h:23
预制体,可重复生成相同配置的 GameObject
定义 Prefab.h:22
nlohmann::json toJson() const
序列化为 JSON
static Prefab Capture(GameObject *source)
从现有 GameObject 捕获组件与字段(跳过 readOnly 字段与不可序列化类型)
const std::vector< ComponentData > & getComponents() const
已捕获的组件数据(只读,供编辑器序列化/预览)
定义 Prefab.h:49
bool hasData() const
是否包含反射数据
定义 Prefab.h:46
void apply(GameObject *go) const
把此预制体应用到已有 GameObject(反射克隆)
static Prefab FromJson(const nlohmann::json &j)
从 JSON 反序列化
GameObject * instantiate(Scene *scene, const std::string &name="") const
创建新 GameObject 并应用此预制体
场景类
定义 Scene.h:34
定义 AudioPlayer.h:10
单个组件的数据(类型名 + 反射字段值,JSON 可序列化)
定义 Prefab.h:25
nlohmann::json fields
字段名 → 值(仅反射且可序列化的字段)
定义 Prefab.h:27
std::string typeName
组件类型名(TypeRegistry::Get 查询)
定义 Prefab.h:26