feat? add basich ah scene loading
This commit is contained in:
@@ -19,6 +19,10 @@ public:
|
||||
explicit Animator(GameObject& parent);
|
||||
|
||||
void Update() override;
|
||||
std::string GetTypeName() const override
|
||||
{
|
||||
return "Animator";
|
||||
}
|
||||
void ImGuiInspector() override;
|
||||
|
||||
void addClip(std::shared_ptr<SkeletalAnimation> clip);
|
||||
|
||||
@@ -18,6 +18,11 @@ public:
|
||||
|
||||
void SetMaterialID(MaterialID id) { materialID = id; }
|
||||
MaterialID GetMaterialID() const { return materialID; }
|
||||
std::string GetTypeName() const override
|
||||
{
|
||||
return "MeshRendererComponent";
|
||||
}
|
||||
|
||||
private:
|
||||
MeshID meshID{NULL_MESH_ID};
|
||||
MaterialID materialID{NULL_MATERIAL_ID};
|
||||
|
||||
@@ -11,6 +11,11 @@ public:
|
||||
|
||||
void Update() override {}
|
||||
|
||||
std::string GetTypeName() const override
|
||||
{
|
||||
return "BoxCollider";
|
||||
}
|
||||
|
||||
|
||||
[[nodiscard]] PhysicsShapeDesc BuildPhysicsShape() const override {
|
||||
return PhysicsShapeDesc::Box(m_HalfExtents, m_CenterOffset, m_IsTrigger);
|
||||
|
||||
@@ -16,6 +16,11 @@ public:
|
||||
|
||||
void Update() override {}
|
||||
|
||||
std::string GetTypeName() const override
|
||||
{
|
||||
return "RigidBody";
|
||||
}
|
||||
|
||||
void AttachPhysicsBody(PhysicsWorld* world, PhysicsBodyHandle body);
|
||||
void DetachPhysicsBody();
|
||||
|
||||
|
||||
@@ -11,6 +11,10 @@ public:
|
||||
, m_Radius(radius) {}
|
||||
|
||||
void Update() override {}
|
||||
std::string GetTypeName() const override
|
||||
{
|
||||
return "SphereCollider";
|
||||
}
|
||||
|
||||
[[nodiscard]] PhysicsShapeDesc BuildPhysicsShape() const override {
|
||||
return PhysicsShapeDesc::Sphere(m_Radius, m_CenterOffset, m_IsTrigger);
|
||||
|
||||
@@ -1,14 +1,21 @@
|
||||
#ifndef COMPONENT_H
|
||||
#define COMPONENT_H
|
||||
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
|
||||
#include <nlohmann/json.hpp>
|
||||
|
||||
#include <destrum/Graphics/RenderContext.h>
|
||||
#include <destrum/ObjectModel/Object.h>
|
||||
#include <destrum/ObjectModel/ObjectId.h>
|
||||
|
||||
class GameObject;
|
||||
class Transform;
|
||||
|
||||
class Component: public Object {
|
||||
class Component : public Object {
|
||||
public:
|
||||
using ObjectMap = std::unordered_map<ObjectId, GameObject*>;
|
||||
|
||||
~Component() override = default;
|
||||
|
||||
@@ -18,30 +25,44 @@ public:
|
||||
Component& operator=(Component&& other) noexcept = delete;
|
||||
|
||||
[[nodiscard]] bool isEnabled() const { return m_IsEnabled; }
|
||||
[[nodiscard]] GameObject *GetGameObject() const { return m_ParentGameObjectPtr; }
|
||||
[[nodiscard]] GameObject* GetGameObject() const { return m_ParentGameObjectPtr; }
|
||||
[[nodiscard]] Transform& GetTransform() const;
|
||||
|
||||
void Destroy() override;
|
||||
|
||||
virtual void Start();
|
||||
virtual void SetEnabled(bool enabled);
|
||||
|
||||
virtual void Update() = 0;
|
||||
virtual void LateUpdate();
|
||||
virtual void FixedUpdate();
|
||||
virtual void ImGuiInspector(); //Specifically for the inspector
|
||||
virtual void ImGuiRender();
|
||||
|
||||
virtual void ImGuiInspector();
|
||||
virtual void ImGuiRender();
|
||||
|
||||
virtual void Render(const RenderContext& ctx);
|
||||
|
||||
// Serialization.
|
||||
virtual std::string GetTypeName() const = 0;
|
||||
|
||||
virtual nlohmann::json Serialize() const {
|
||||
return {};
|
||||
}
|
||||
|
||||
virtual void Deserialize(const nlohmann::json& data) {
|
||||
}
|
||||
|
||||
virtual void ResolveReferences(const ObjectMap& objects) {
|
||||
}
|
||||
|
||||
bool HasStarted{false};
|
||||
|
||||
protected:
|
||||
explicit Component(GameObject& pParent, const std::string& name = "Component");
|
||||
|
||||
private:
|
||||
GameObject* m_ParentGameObjectPtr{};
|
||||
bool m_IsEnabled{true};
|
||||
|
||||
};
|
||||
|
||||
#endif //COMPONENT_H
|
||||
#endif // COMPONENT_H
|
||||
@@ -1,14 +1,17 @@
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
#include <algorithm>
|
||||
|
||||
#include <destrum/ObjectModel/Component.h>
|
||||
#include <destrum/ObjectModel/Object.h>
|
||||
#include <destrum/ObjectModel/ObjectId.h>
|
||||
#include <destrum/ObjectModel/Transform.h>
|
||||
|
||||
class Scene;
|
||||
|
||||
class GameObject final: public Object {
|
||||
class GameObject final : public Object {
|
||||
public:
|
||||
friend class Scene;
|
||||
|
||||
@@ -31,14 +34,28 @@ public:
|
||||
SetActiveDirty();
|
||||
}
|
||||
|
||||
[[nodiscard]] std::vector<std::unique_ptr<Component>>& GetComponents() { return m_Components; }
|
||||
[[nodiscard]] bool IsActive() const { return m_Active; }
|
||||
|
||||
[[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() {
|
||||
return m_Components;
|
||||
}
|
||||
|
||||
[[nodiscard]] const std::vector<std::unique_ptr<Component>>& GetComponents() const {
|
||||
return m_Components;
|
||||
}
|
||||
|
||||
[[nodiscard]] Transform& GetTransform() { return m_TransformPtr; }
|
||||
[[nodiscard]] const Transform& GetTransform() const { return m_TransformPtr; }
|
||||
|
||||
[[nodiscard]] bool IsActiveInHierarchy();
|
||||
|
||||
[[nodiscard]] bool IsActive() const { return m_Active; }
|
||||
|
||||
explicit GameObject(const std::string& name = "GameObject");
|
||||
~GameObject() override;
|
||||
|
||||
@@ -57,20 +74,30 @@ public:
|
||||
return static_cast<TComponent*>(addedComponent.get());
|
||||
}
|
||||
|
||||
template <typename Component>
|
||||
[[nodiscard]] Component *GetComponent() {
|
||||
for (const auto& component: m_Components) {
|
||||
if (auto casted = dynamic_cast<Component*>(component.get())) {
|
||||
template <typename TComponent>
|
||||
[[nodiscard]] TComponent* GetComponent() {
|
||||
for (const auto& component : m_Components) {
|
||||
if (auto casted = dynamic_cast<TComponent*>(component.get())) {
|
||||
return casted;
|
||||
}
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
template <typename Component>
|
||||
Component *DestroyComponent() {
|
||||
for (const auto& component: m_Components) {
|
||||
if (auto casted = dynamic_cast<Component*>(component.get())) {
|
||||
template <typename TComponent>
|
||||
[[nodiscard]] const TComponent* GetComponent() const {
|
||||
for (const auto& component : m_Components) {
|
||||
if (auto casted = dynamic_cast<const TComponent*>(component.get())) {
|
||||
return casted;
|
||||
}
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
template <typename TComponent>
|
||||
TComponent* DestroyComponent() {
|
||||
for (const auto& component : m_Components) {
|
||||
if (auto casted = dynamic_cast<TComponent*>(component.get())) {
|
||||
casted->Destroy();
|
||||
return casted;
|
||||
}
|
||||
@@ -78,40 +105,50 @@ public:
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
template <typename Component>
|
||||
[[nodiscard]] bool HasComponent() {
|
||||
for (const auto& component: m_Components) {
|
||||
if (auto casted = dynamic_cast<Component*>(component.get())) {
|
||||
template <typename TComponent>
|
||||
[[nodiscard]] bool HasComponent() const {
|
||||
for (const auto& component : m_Components) {
|
||||
if (dynamic_cast<TComponent*>(component.get())) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
template <typename Component>
|
||||
Component *GetComponentInChildren() {
|
||||
return GetComponentInChildrenRecursive<Component>(&GetTransform());
|
||||
template <typename TComponent>
|
||||
TComponent* GetComponentInChildren() {
|
||||
return GetComponentInChildrenRecursive<TComponent>(&GetTransform());
|
||||
}
|
||||
|
||||
private:
|
||||
template <typename Component>
|
||||
static Component *GetComponentInChildrenRecursive(Transform* transform) {
|
||||
template <typename TComponent>
|
||||
static TComponent* GetComponentInChildrenRecursive(Transform* transform) {
|
||||
if (!transform) return nullptr;
|
||||
|
||||
GameObject* owner = transform->GetOwner();
|
||||
if (owner) {
|
||||
if (Component* comp = owner->GetComponent<Component>()) {
|
||||
if (TComponent* comp = owner->GetComponent<TComponent>()) {
|
||||
return comp;
|
||||
}
|
||||
}
|
||||
for (Transform* child: transform->GetChildren()) {
|
||||
if (Component* found = GetComponentInChildrenRecursive<Component>(child)) {
|
||||
|
||||
for (Transform* child : transform->GetChildren()) {
|
||||
if (TComponent* found = GetComponentInChildrenRecursive<TComponent>(child)) {
|
||||
return found;
|
||||
}
|
||||
}
|
||||
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
void SetScene(Scene* scene) { m_Scene = scene; }
|
||||
|
||||
void SetActiveDirty();
|
||||
void UpdateActiveState();
|
||||
|
||||
inline static ObjectId s_NextId{1};
|
||||
|
||||
ObjectId m_Id{InvalidObjectId};
|
||||
|
||||
bool m_Active{true};
|
||||
Scene* m_Scene{};
|
||||
@@ -119,6 +156,5 @@ private:
|
||||
std::vector<std::unique_ptr<Component>> m_Components{};
|
||||
|
||||
bool m_ActiveDirty{true};
|
||||
bool m_ActiveInHierarchy{true}; //Derived
|
||||
void UpdateActiveState();
|
||||
};
|
||||
bool m_ActiveInHierarchy{true};
|
||||
};
|
||||
@@ -0,0 +1,10 @@
|
||||
#ifndef DESTRUM_OBJECTID_H
|
||||
#define DESTRUM_OBJECTID_H
|
||||
|
||||
#include <cstdint>
|
||||
|
||||
using ObjectId = std::uint64_t;
|
||||
|
||||
inline constexpr ObjectId InvalidObjectId = 0;
|
||||
|
||||
#endif //DESTRUM_OBJECTID_H
|
||||
@@ -35,6 +35,12 @@ public:
|
||||
|
||||
[[nodiscard]] bool IsBeingUnloaded() const { return m_BeingUnloaded; }
|
||||
|
||||
void CommitPendingAdditions();
|
||||
|
||||
[[nodiscard]] const std::vector<std::shared_ptr<GameObject>>& GetObjects() const {
|
||||
return m_objects;
|
||||
}
|
||||
|
||||
void SetRegisterBindings(std::function<void()> registerBindings) {
|
||||
m_registerBindings = std::move(registerBindings);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
#ifndef DESTRUM_COMPONENTFACTORY_H
|
||||
#define DESTRUM_COMPONENTFACTORY_H
|
||||
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
|
||||
#include <destrum/ObjectModel/Component.h>
|
||||
#include <destrum/ObjectModel/GameObject.h>
|
||||
|
||||
class ComponentFactory final {
|
||||
public:
|
||||
using CreateFn = std::function<Component*(GameObject&)>;
|
||||
|
||||
static void Register(const std::string& typeName, CreateFn createFn) {
|
||||
Registry()[typeName] = std::move(createFn);
|
||||
}
|
||||
|
||||
static Component* Create(const std::string& typeName, GameObject& owner) {
|
||||
const auto it = Registry().find(typeName);
|
||||
|
||||
if (it == Registry().end()) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
return it->second(owner);
|
||||
}
|
||||
|
||||
private:
|
||||
static std::unordered_map<std::string, CreateFn>& Registry() {
|
||||
static std::unordered_map<std::string, CreateFn> registry;
|
||||
return registry;
|
||||
}
|
||||
};
|
||||
|
||||
#endif //DESTRUM_COMPONENTFACTORY_H
|
||||
@@ -0,0 +1,6 @@
|
||||
#ifndef DESTRUM_COMPONENTREGISTRY_H
|
||||
#define DESTRUM_COMPONENTREGISTRY_H
|
||||
|
||||
void RegisterEngineComponents();
|
||||
|
||||
#endif //DESTRUM_COMPONENTREGISTRY_H
|
||||
@@ -0,0 +1,14 @@
|
||||
#ifndef DESTRUM_SCENESERIALIZER_H
|
||||
#define DESTRUM_SCENESERIALIZER_H
|
||||
|
||||
#include <filesystem>
|
||||
|
||||
class Scene;
|
||||
|
||||
class SceneSerializer final {
|
||||
public:
|
||||
static bool Save(Scene& scene, const std::filesystem::path& path);
|
||||
static bool Load(Scene& scene, const std::filesystem::path& path);
|
||||
};
|
||||
|
||||
#endif //DESTRUM_SCENESERIALIZER_H
|
||||
Reference in New Issue
Block a user