50b9559105862237b1c01e8ea310555f4dde1692 — Leinnan 7 months ago 1187f49 feature/attacking
Switch models on destroy
3 files changed, 27 insertions(+), 3 deletions(-)

M data/test.json
M src/systems/DamageSystem.cpp
M src/utils/JsonParser.cpp
M data/test.json => data/test.json +3 -1
@@ 97,7 97,9 @@
             "type": "Transform"
         },
         {
-            "type": "Destroyable"
+            "type": "Destroyable",
+            "destroyedModelDiffuse": "data/gfx/rocks.dds",
+            "destroyedModel": "data/gfx/pebbles_1.obj"
         },
         {
             "collisions": [

M src/systems/DamageSystem.cpp => src/systems/DamageSystem.cpp +15 -0
@@ 5,6 5,7 @@
 #include "components/DamageBox.hpp"
 #include "components/Destroyable.hpp"
 #include "components/Fighter.hpp"
+#include "components/MeshRenderer.hpp"
 #include "components/Transform.hpp"
 
 #include "raylib.h"


@@ 80,6 81,20 @@ void DamageSystem::Update(entt::registry<> &registry, const float delta) {
             destroyable.currentLife -= damageTaken;
             if(damageTaken > 0)
                 std::cout << "Damage points! Even " << damageTaken << " of them, there is only " << destroyable.currentLife << " left!\n";
+            if(destroyable.currentLife <= 0)
+            {
+                if(registry.has<Components::MeshRenderer>(entity))
+                {
+                    auto renderer = registry.get<Components::MeshRenderer>(entity);
+
+                    registry.remove<Components::MeshRenderer>(entity);
+
+                    renderer.diffusePath = destroyable.destroyedModelDiffuse;
+                    renderer.modelPath = destroyable.destroyedModel;
+                    registry.assign<Components::MeshRenderer>(entity,renderer);
+                }
+                registry.remove<Components::Destroyable>(entity);
+            }
         });
 }
 } // namespace Systems

M src/utils/JsonParser.cpp => src/utils/JsonParser.cpp +9 -2
@@ 48,9 48,16 @@ void Components::to_json(json &j, const Components::Fighter &t) { j = json{{"typ
 
 void Components::from_json(const json &j, Components::Fighter &t) {}
 
-void Components::to_json(json &j, const Components::Destroyable &t) { j = json{{"type", "Destroyable"}}; }
+void Components::to_json(json &j, const Components::Destroyable &t) {
+    j = json{{"type", "Destroyable"},
+        {"destroyedModelDiffuse",t.destroyedModelDiffuse},
+        {"destroyedModel",t.destroyedModel}};
+    }
 
-void Components::from_json(const json &j, Components::Destroyable &t) {}
+void Components::from_json(const json &j, Components::Destroyable &t) {
+    t.destroyedModel = j.at("destroyedModel").get<std::string>();
+    t.destroyedModelDiffuse = j.at("destroyedModelDiffuse").get<std::string>();
+}
 
 void Components::to_json(json &j, const Components::Actor &t) {
     j = json{{"type", "Actor"}, {"name", t.name}, {"speed", t.speed}};