1a1078884d898840580104a57e3662d990e21bce — Leinnan 7 months ago 6748fcb
Working spawning
4 files changed, 8 insertions(+), 78 deletions(-)

M data/test.json
M src/factories/UnitFactory.hpp
M src/main.cpp
M src/systems/AiSystem.cpp
M data/test.json => data/test.json +0 -67
@@ 232,72 232,5 @@
             "type": "Patrol",
             "waitTime": 3.0
         }
-    ],
-    [
-        {
-            "diffusePath": "data/gfx/knight.png",
-            "modelPath": "data/gfx/knight.obj",
-            "scale": 2.0,
-            "type": "MeshRenderer"
-        },
-        {
-            "offsetX": 8.0,
-            "offsetY": 12.0,
-            "offsetZ": 8.0,
-            "type": "CameraTarget"
-        },
-        {
-            "angle": 0.0,
-            "posX": 70.0,
-            "posY": 0.0,
-            "posZ": 0.0,
-            "type": "Transform"
-        },
-        {
-            "type": "PlayerInput"
-        },
-        {
-            "type": "Fighter"
-        },
-        {
-            "fontSize": 10,
-            "offsetX": 0.0,
-            "offsetY": 4.0,
-            "offsetZ": 0.0,
-            "text": "",
-            "type": "WorldScreenText"
-        },
-        {
-            "type": "Actor",
-            "name": "Hero",
-            "speed": 15.0
-        },
-        {
-            "points": [
-                {
-                    "x": 0.0,
-                    "y": 0.0,
-                    "z": 0.0
-                }
-            ],
-            "type": "Patrol",
-            "waitTime": 0.3
-        },
-        {
-            "type": "Velocity"
-        },
-        {
-            "collisions": [
-                {
-                    "offsetX": 0.0,
-                    "offsetY": 1.0,
-                    "offsetZ": 0.0,
-                    "sizeX": 0.8,
-                    "sizeY": 2.0,
-                    "sizeZ": 0.8
-                }
-            ],
-            "type": "Collisions"
-        }
     ]
 ]

M src/factories/UnitFactory.hpp => src/factories/UnitFactory.hpp +5 -10
@@ 25,7 25,7 @@ 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("scale").get<float>();
+        unit->scale = j.at("scale").get<float>();
         unit->speed = j.at("speed").get<float>();
         unit->name= j.at("name").get<std::string>();
         unit->modelPath= j.at("modelPath").get<std::string>();


@@ 67,13 67,7 @@ class UnitFactory final {
         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);
+        registry.assign<Components::MeshRenderer>(entity,unitPreset->modelPath.c_str(),unitPreset->diffusePath.c_str(),unitPreset->scale);
 
         Components::Actor actorComponent;
         actorComponent.name = unitPreset->name;


@@ 96,8 90,9 @@ class UnitFactory final {
             cameraTarget.offset = {8.0f,12.0f,8.0f};
             registry.assign<Components::CameraTarget>(entity,cameraTarget);
         }
-        Components::Patrol patrol;
-        registry.assign<Components::Patrol>(entity,patrol);
+        registry.assign<Components::Patrol>(entity);
+
+        return entity;
     }
   private:
     entt::resource_cache<Presets::Unit> unitCache;

M src/main.cpp => src/main.cpp +2 -0
@@ 61,7 61,9 @@ int main(int argc, char **argv) {
     aiSystem.Init(registry);
     damageSystem.Init();
     unitFactory.loadFromFile("data/units.json");
+    auto result = unitFactory.spawnUnit(registry,entt::hashed_string("player"));
     Components::readFromFile(registry, jsonFile.c_str());
+    std::cout << "Spawn Unit result " << result << '\n';
 
     camera = {0};
     camera.target = (Vector3){0.0f, 0.5f, 0.0f};

M src/systems/AiSystem.cpp => src/systems/AiSystem.cpp +1 -1
@@ 27,7 27,7 @@ void AiSystem::Update(entt::registry<> &registry, const float &delta) {
         [&](const auto entity, auto &transform, auto &vel, auto &patrol, auto &actor) {
             const bool isFighter = registry.has<Components::Fighter>(entity);
             const bool isAttacking = isFighter ? registry.get<Components::Fighter>(entity).isAttacking : false;
-            if(isAttacking)
+            if(isAttacking || patrol.points.size() == 0)
                 return;
             vel.movement.x = 0.f;
             vel.movement.z = 0.f;