3f649f3bc5d4950ad27ce0bc6ba3e107c01a6f24 — Leinnan 7 months ago b2b368b
Create Unit factory
6 files changed, 56 insertions(+), 26 deletions(-)

M CMakeLists.txt
A src/factories/UnitFactory.cpp
A src/factories/UnitFactory.hpp
M src/main.cpp
D src/presets/Preset.hpp
M src/presets/Unit.hpp
M CMakeLists.txt => CMakeLists.txt +1 -1
@@ 21,7 21,7 @@ endif()
 
 file(COPY ${CMAKE_SOURCE_DIR}/data DESTINATION ${CMAKE_BINARY_DIR}/output)
 
-include_directories(src ext src/controllers src/components src/systems src/utils)
+include_directories(src ext src/controllers src/components src/systems src/utils src/presets src/factories)
 add_executable(${PROJECT_NAME} src/main.cpp src/utils/Arkona.cpp src/utils/JsonParser.cpp src/systems/DamageSystem.cpp src/systems/MissileSystem.cpp src/systems/AiSystem.cpp src/systems/CollisionSystem.cpp src/systems/DestroySystem.cpp src/systems/MovementSystem.cpp src/systems/RenderSystem.cpp src/systems/PlayerInputSystem.cpp)
 #set(raylib_VERBOSE 1)
 target_link_libraries(${PROJECT_NAME} raylib EnTT)

A src/factories/UnitFactory.cpp => src/factories/UnitFactory.cpp +1 -0
@@ 0,0 1,1 @@
+

A src/factories/UnitFactory.hpp => src/factories/UnitFactory.hpp +48 -0
@@ 0,0 1,48 @@
+#ifndef UNIT_FACTORY_HPP
+#define UNIT_FACTORY_HPP
+#include "controllers/Controller.hpp"
+#include "presets/Unit.hpp"
+#include <entt/entt.hpp>
+#include <json.hpp>
+#include <memory>
+#include <fstream>
+#include <iostream>
+#include <sstream>
+#include <string>
+
+namespace Factories {
+struct UnitLoader final: entt::resource_loader<UnitLoader, Presets::Unit> {
+    std::shared_ptr<Presets::Unit> load(nlohmann::json j) const
+    {
+        std::shared_ptr<Presets::Unit> unit = std::make_shared<Presets::Unit>();
+        unit->speed = j.at("speed").get<float>();
+        unit->name= j.at("name").get<std::string>();
+        unit->modelPath= j.at("modelPath").get<std::string>();
+        unit->diffusePath= j.at("diffusePath").get<std::string>();
+        unit->isPlayer = (j.at("isPlayer").get<int>() == 1);
+
+        return unit;
+    }
+};
+
+class UnitFactory final: public Controllers::Controller<UnitFactory> {
+  public:
+    UnitFactory(){};
+    void loadFromFile(std::string path)
+    {
+        nlohmann::json jsonReader;
+        std::ifstream file(path);
+        file >> jsonReader;
+
+        for(nlohmann::json &object : jsonReader)
+        {
+            const auto hashedPath = entt::hashed_string(object.at("id").get<std::string>().c_str());
+            unitCache.load<UnitLoader>(hashedPath,object);
+        }
+    }
+  private:
+    entt::resource_cache<Presets::Unit> unitCache;
+};
+};
+
+#endif

M src/main.cpp => src/main.cpp +4 -2
@@ 9,9 9,11 @@
 #include "systems/PlayerInputSystem.hpp"
 #include "systems/RenderSystem.hpp"
 #include "utils/JsonParser.hpp"
-#include <INIReader.hpp>
-#include <entt/entt.hpp>
 #include <iostream>
+#include "INIReader.hpp"
+#include <entt/entt.hpp>
+#include <string>
+#include "factories/UnitFactory.hpp"
 #if defined(PLATFORM_WEB)
 #include <emscripten/emscripten.h>
 #endif

D src/presets/Preset.hpp => src/presets/Preset.hpp +0 -17
@@ 1,17 0,0 @@
-#ifndef PRESET_HPP
-#define PRESET_HPP
-#include <entt/entt.hpp>
-
-namespace Presets {
-
-struct Preset {
-public:
-std::string type;
-
-Preset(){};
-
-virtual uint32_t CreateEntity(entt::registry<> &registry){};
-};
-}
-
-#endif

M src/presets/Unit.hpp => src/presets/Unit.hpp +2 -6
@@ 1,12 1,9 @@
 #ifndef UNIT_HPP
 #define UNIT_HPP
 #include <string>
-#include "presets/Preset.hpp"
 
 namespace Presets {
-
-struct Unit : Preset {
-public:
+struct Unit final{
     std::string type = "Unit";
 
     float speed;


@@ 14,8 11,7 @@ public:
     std::string name;
     std::string modelPath;
     std::string diffusePath;
-
-    Unit(){};
+};
 }
 
 #endif