feat: add prefabs
This commit is contained in:
@@ -27,11 +27,13 @@ struct SceneEnvironment {
|
||||
float skyboxRotation{0.0f};
|
||||
};
|
||||
class SceneSerializer;
|
||||
class PrefabSerializer;
|
||||
class PhysicsWorld;
|
||||
|
||||
class Scene final {
|
||||
friend Scene& SceneManager::CreateScene(const std::string& name);
|
||||
friend class SceneSerializer;
|
||||
friend class PrefabSerializer;
|
||||
|
||||
public:
|
||||
// void Add(std::shared_ptr<GameObject> object);
|
||||
|
||||
@@ -0,0 +1,44 @@
|
||||
#ifndef DESTRUM_OBJECTSERIALIZATION_H
|
||||
#define DESTRUM_OBJECTSERIALIZATION_H
|
||||
|
||||
#include <filesystem>
|
||||
#include <optional>
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
#include <vector>
|
||||
|
||||
#include <nlohmann/json.hpp>
|
||||
|
||||
#include <destrum/ObjectModel/ObjectId.h>
|
||||
|
||||
class GameObject;
|
||||
class Scene;
|
||||
|
||||
namespace ObjectSerialization {
|
||||
using ObjectMap = std::unordered_map<ObjectId, GameObject*>;
|
||||
using ValidationError = std::optional<std::string>;
|
||||
|
||||
constexpr int CurrentObjectVersion = 2;
|
||||
|
||||
[[nodiscard]] std::optional<std::string> FindNonFinite(const nlohmann::json& value, const std::string& path);
|
||||
|
||||
[[nodiscard]] ValidationError ValidateObjects(const nlohmann::json& objects, int version);
|
||||
|
||||
void WarnMissingAssets(const nlohmann::json& objects, const std::filesystem::path& source);
|
||||
|
||||
[[nodiscard]] nlohmann::json SerializeObjects(const std::vector<const GameObject*>& objects);
|
||||
|
||||
[[nodiscard]] std::vector<const GameObject*> CollectHierarchy(const GameObject& root);
|
||||
|
||||
struct BuiltObjects {
|
||||
std::vector<GameObject*> objects;
|
||||
ObjectMap idMap;
|
||||
};
|
||||
|
||||
[[nodiscard]] BuiltObjects BuildObjects(Scene& scene, const nlohmann::json& objects,
|
||||
const ObjectMap& externalObjects = {});
|
||||
|
||||
[[nodiscard]] bool WriteFileAtomically(const std::filesystem::path& path, const std::string& contents);
|
||||
}
|
||||
|
||||
#endif //DESTRUM_OBJECTSERIALIZATION_H
|
||||
@@ -0,0 +1,37 @@
|
||||
#ifndef DESTRUM_PREFABCACHE_H
|
||||
#define DESTRUM_PREFABCACHE_H
|
||||
|
||||
#include <filesystem>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <unordered_map>
|
||||
|
||||
#include <nlohmann/json.hpp>
|
||||
|
||||
#include <destrum/Singleton.h>
|
||||
|
||||
class PrefabCache final : public Singleton<PrefabCache> {
|
||||
friend class Singleton<PrefabCache>;
|
||||
|
||||
public:
|
||||
[[nodiscard]] std::shared_ptr<const nlohmann::json> Get(std::string_view path);
|
||||
void Invalidate(const std::filesystem::path& path);
|
||||
void Clear();
|
||||
|
||||
[[nodiscard]] static std::filesystem::path ResolvePath(std::string_view path);
|
||||
|
||||
private:
|
||||
PrefabCache() = default;
|
||||
|
||||
struct Entry {
|
||||
std::shared_ptr<const nlohmann::json> prefab;
|
||||
std::filesystem::file_time_type writeTime;
|
||||
};
|
||||
|
||||
[[nodiscard]] static std::string MakeKey(const std::filesystem::path& path);
|
||||
|
||||
std::unordered_map<std::string, Entry> m_Prefabs;
|
||||
};
|
||||
|
||||
#endif //DESTRUM_PREFABCACHE_H
|
||||
@@ -0,0 +1,31 @@
|
||||
#ifndef DESTRUM_PREFABSERIALIZER_H
|
||||
#define DESTRUM_PREFABSERIALIZER_H
|
||||
|
||||
#include <filesystem>
|
||||
#include <optional>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
|
||||
#include <nlohmann/json.hpp>
|
||||
|
||||
class GameObject;
|
||||
class Scene;
|
||||
class Transform;
|
||||
|
||||
class PrefabSerializer final {
|
||||
public:
|
||||
static constexpr int CurrentPrefabVersion = 1;
|
||||
|
||||
[[nodiscard]] static std::optional<nlohmann::json> Capture(const GameObject& root);
|
||||
static bool Save(const GameObject& root, const std::filesystem::path& path);
|
||||
|
||||
[[nodiscard]] static std::optional<nlohmann::json> LoadFile(const std::filesystem::path& path);
|
||||
[[nodiscard]] static std::optional<std::string> Validate(const nlohmann::json& prefab);
|
||||
|
||||
static GameObject* InstantiateFromJson(Scene& scene, const nlohmann::json& prefab, Transform* parent = nullptr);
|
||||
static GameObject* Instantiate(Scene& scene, std::string_view path, Transform* parent = nullptr);
|
||||
|
||||
static GameObject* Duplicate(GameObject& object);
|
||||
};
|
||||
|
||||
#endif //DESTRUM_PREFABSERIALIZER_H
|
||||
Reference in New Issue
Block a user