|
1 | 1 | #pragma once |
2 | 2 |
|
| 3 | +#include "../ecs/component-registry.hpp" |
3 | 4 | #include "../ecs/entity.hpp" |
4 | 5 | #include "camera.hpp" |
5 | 6 | #include "free-camera-controller.hpp" |
|
8 | 9 |
|
9 | 10 | namespace our { |
10 | 11 |
|
| 12 | + inline void registerCommonComponents() { |
| 13 | + static bool initialized = false; |
| 14 | + if (initialized) return; |
| 15 | + |
| 16 | + ComponentRegistry::registerType<CameraComponent>(); |
| 17 | + ComponentRegistry::registerType<FreeCameraControllerComponent>(); |
| 18 | + ComponentRegistry::registerType<MovementComponent>(); |
| 19 | + ComponentRegistry::registerType<MeshRendererComponent>(); |
| 20 | + |
| 21 | + initialized = true; |
| 22 | + } |
| 23 | + |
11 | 24 | // Given a json object, this function picks and creates a component in the given entity |
12 | 25 | // based on the "type" specified in the json object which is later deserialized from the rest of the json object |
13 | 26 | inline void deserializeComponent(const nlohmann::json& data, Entity* entity) { |
| 27 | + registerCommonComponents(); |
14 | 28 | std::string type = data.value("type", ""); |
15 | | - Component* component = nullptr; |
16 | | - if (type == CameraComponent::getID()) { |
17 | | - component = entity->addComponent<CameraComponent>(); |
18 | | - } else if (type == FreeCameraControllerComponent::getID()) { |
19 | | - component = entity->addComponent<FreeCameraControllerComponent>(); |
20 | | - } else if (type == MovementComponent::getID()) { |
21 | | - component = entity->addComponent<MovementComponent>(); |
22 | | - } else if (type == MeshRendererComponent::getID()) { |
23 | | - component = entity->addComponent<MeshRendererComponent>(); |
24 | | - } |
| 29 | + Component* component = ComponentRegistry::create(type, entity); |
25 | 30 | if (component) component->deserialize(data); |
26 | 31 | } |
27 | 32 |
|
|
0 commit comments