feat: add ObjectRef for scene loading to not rely on runtime id's
This commit is contained in:
@@ -44,9 +44,6 @@ public:
|
||||
|
||||
[[nodiscard]] ObjectId GetId() const { return m_Id; }
|
||||
|
||||
// Use this only while loading a scene.
|
||||
void SetIdForDeserialization(ObjectId id);
|
||||
|
||||
[[nodiscard]] Scene* GetScene() const { return m_Scene; }
|
||||
|
||||
[[nodiscard]] std::vector<std::unique_ptr<Component>>& GetComponents() {
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
#ifndef DESTRUM_OBJECTREF_H
|
||||
#define DESTRUM_OBJECTREF_H
|
||||
|
||||
#include <unordered_map>
|
||||
|
||||
#include <nlohmann/json_fwd.hpp>
|
||||
|
||||
#include <destrum/ObjectModel/ObjectId.h>
|
||||
|
||||
class GameObject;
|
||||
class Scene;
|
||||
|
||||
class ObjectRef final {
|
||||
public:
|
||||
using ObjectMap = std::unordered_map<ObjectId, GameObject*>;
|
||||
|
||||
ObjectRef() = default;
|
||||
explicit ObjectRef(ObjectId id) : m_Id(id) {}
|
||||
explicit ObjectRef(const GameObject& object);
|
||||
|
||||
[[nodiscard]] ObjectId GetId() const { return m_Id; }
|
||||
[[nodiscard]] bool IsSet() const { return m_Id != InvalidObjectId; }
|
||||
|
||||
[[nodiscard]] GameObject* Get(const Scene& scene) const;
|
||||
|
||||
bool Resolve(const ObjectMap& objects);
|
||||
|
||||
bool operator==(const ObjectRef&) const = default;
|
||||
|
||||
private:
|
||||
ObjectId m_Id{InvalidObjectId};
|
||||
};
|
||||
|
||||
void to_json(nlohmann::json& json, const ObjectRef& ref);
|
||||
void from_json(const nlohmann::json& json, ObjectRef& ref);
|
||||
|
||||
#endif //DESTRUM_OBJECTREF_H
|
||||
Reference in New Issue
Block a user