Adds scene saving and loading functionality, using a component factory for dynamic component creation and serialization. This allows users to save and load the state of entities, including their components and textures. A new component factory is introduced to register and create different component types. Each component implements serialization and deserialization methods, which are used to store and restore the component's state. A new .ker scene file format is introduced to serialize entity data and to load it back into memory to restore the scene. Also adds a DemoScene_V12.9.0.ker file to showcase the engine.
55 lines
1.9 KiB
C++
55 lines
1.9 KiB
C++
// ComponentFactory.h
|
|
#pragma once
|
|
#include <unordered_map>
|
|
#include <functional>
|
|
#include <memory>
|
|
#include <string>
|
|
|
|
#include "entity.h"
|
|
#include "component.h"
|
|
#include "components/identity_component.h"
|
|
#include "components/render_component.h"
|
|
#include "components/transform_component.h"
|
|
#include "components/physics_component.h"
|
|
#include "components/shader_component.h"
|
|
#include "components/model_path_component.h"
|
|
|
|
class ComponentFactory {
|
|
public:
|
|
static ComponentFactory& Get() {
|
|
static ComponentFactory instance;
|
|
return instance;
|
|
}
|
|
|
|
template<typename T>
|
|
void EnregistrerComposant(const std::string& typeName) {
|
|
m_creators[typeName] = [](std::shared_ptr<ecs::Entity> entity) -> std::shared_ptr<ecs::Component> {
|
|
return entity->AddComponent<T>();
|
|
};
|
|
}
|
|
|
|
std::shared_ptr<ecs::Component> CreerComposant(const std::string& typeName,
|
|
std::shared_ptr<ecs::Entity> entity) {
|
|
auto it = m_creators.find(typeName);
|
|
if (it != m_creators.end()) {
|
|
return it->second(entity);
|
|
}
|
|
return nullptr;
|
|
}
|
|
|
|
private:
|
|
std::unordered_map<std::string, std::function<std::shared_ptr<ecs::Component>(std::shared_ptr<ecs::Entity>)>> m_creators;
|
|
};
|
|
|
|
// Fonction d'initialisation pour enregistrer tous les composants
|
|
inline void EnregistrerTousLesComposants() {
|
|
auto& factory = ComponentFactory::Get();
|
|
factory.EnregistrerComposant<ecs::TransformComponent>("TransformComponent");
|
|
factory.EnregistrerComposant<ecs::PhysicsComponent>("PhysicsComponent");
|
|
factory.EnregistrerComposant<ecs::ShaderComponent>("ShaderComponent");
|
|
factory.EnregistrerComposant<ecs::ModelPathComponent>("ModelPathComponent");
|
|
factory.EnregistrerComposant<ecs::IdentityComponent>("IdentityComponent");
|
|
factory.EnregistrerComposant<ecs::RenderComponent>("RenderComponent");
|
|
// Ajouter d'autres composants ici
|
|
}
|