6748fcb6bb9eef9edcd1ed17c9d06f79fa075bf9 — Leinnan 7 months ago 4e72ee9
Spawning units based on config
2 files changed, 57 insertions(+), 1 deletions(-)

M src/factories/UnitFactory.hpp
M src/presets/Unit.hpp
M src/factories/UnitFactory.hpp => src/factories/UnitFactory.hpp +56 -1
@@ 1,7 1,17 @@
 #ifndef UNIT_FACTORY_HPP
 #define UNIT_FACTORY_HPP
-#include "controllers/Controller.hpp"
+#include "components/Actor.hpp"
+#include "components/Transform.hpp"
+#include "components/CameraTarget.hpp"
+#include "components/MeshRenderer.hpp"
+#include "components/Fighter.hpp"
+#include "components/Patrol.hpp"
+#include "components/PlayerInput.hpp"
+#include "components/WorldScreenText.hpp"
+#include "components/Velocity.hpp"
 #include "presets/Unit.hpp"
+
+
 #include <entt/entt.hpp>
 #include <json.hpp>
 #include <memory>


@@ 44,6 54,51 @@ class UnitFactory final {
         const auto size = unitCache.size();
         std::cout << "Size of unitCache: " << size << '\n';
     }
+
+    uint32_t spawnUnit(entt::registry<> &registry,entt::hashed_string id)
+    {
+        if(!unitCache.contains(id))
+            return -1;
+
+        auto entity = registry.create();
+        const auto& unitPreset = unitCache.handle(id);
+
+        Components::Transform transformComponent;
+        transformComponent.pos = {0,0,0};
+        transformComponent.angle = 0.0f;
+        registry.assign<Components::Transform>(entity,transformComponent);
+
+
+        Components::MeshRenderer meshComponent;
+        meshComponent.modelPath = unitPreset->modelPath;
+        meshComponent.diffusePath = unitPreset->diffusePath;
+        meshComponent.scale = unitPreset->scale;
+        registry.assign<Components::MeshRenderer>(entity,meshComponent);
+
+        Components::Actor actorComponent;
+        actorComponent.name = unitPreset->name;
+        actorComponent.speed = unitPreset->speed;
+        registry.assign<Components::Actor>(entity,actorComponent);
+
+        Components::WorldScreenText worldScreenTextComponent;
+        worldScreenTextComponent.fontSize = 10.0f;
+        worldScreenTextComponent.text = unitPreset->name;
+        worldScreenTextComponent.offset = {0.0f,4.0f,0.0f};
+        registry.assign<Components::WorldScreenText>(entity,worldScreenTextComponent);
+        registry.assign<Components::Velocity>(entity);
+
+        if(unitPreset->isPlayer)
+        {
+            registry.assign<Components::PlayerInput>(entity);
+            registry.assign<Components::Fighter>(entity);
+
+            Components::CameraTarget cameraTarget;
+            cameraTarget.offset = {8.0f,12.0f,8.0f};
+            registry.assign<Components::CameraTarget>(entity,cameraTarget);
+        }
+        Components::Patrol patrol;
+        registry.assign<Components::Patrol>(entity,patrol);
+    }
   private:
     entt::resource_cache<Presets::Unit> unitCache;
 };

M src/presets/Unit.hpp => src/presets/Unit.hpp +1 -0
@@ 7,6 7,7 @@ struct Unit final{
     std::string type = "Unit";
 
     float speed;
+    float scale;
     bool isPlayer;
     std::string name;
     std::string modelPath;