e2276a4b431d11809d995fc5afe12ea6217a0885 — Leinnan 7 months ago 7e025e1
Create damage system
M CMakeLists.txt => CMakeLists.txt +2 -2
@@ 7,7 7,7 @@ if(${PLATFORM} MATCHES "Web")
     set(CMAKE_VERBOSE_MAKEFILE on)
     file(COPY ${CMAKE_SOURCE_DIR}/webBase/index.html DESTINATION ${CMAKE_BINARY_DIR}/output)
 endif()
-    
+
 set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/output)
 
 SET(BUILD_EXAMPLES OFF CACHE BOOL "Build the examples.")


@@ 22,7 22,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)
-add_executable(${PROJECT_NAME} src/main.cpp src/utils/Arkona.cpp src/utils/JsonParser.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)
+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)
 if(${PLATFORM} MATCHES "Web")

M src/components/DamageBox.hpp => src/components/DamageBox.hpp +1 -0
@@ 6,6 6,7 @@ namespace Components {
 struct DamageBox final {
     Collision collision;
     BoundingBox box;
+    int damage;
     DamageBox() {}
 };
 

A src/components/Destroyable.hpp => src/components/Destroyable.hpp +20 -0
@@ 0,0 1,20 @@
+#ifndef DESTROYABLE_HPP
+#define DESTROYABLE_HPP
+#include <string>
+
+namespace Components {
+struct Destroyable final {
+    int maxLife = 1;
+    int currentLife = 1;
+    std::string destroyedModel;
+    std::string destroyedModelDiffuse;
+
+    Destroyable(){}
+    Destroyable(int life, std::string destroyedModel, std::string diffuse) :
+    destroyedModel( destroyedModel), destroyedModelDiffuse(diffuse), maxLife(life),currentLife(life){}
+};
+
+} // namespace Components
+  // Components
+
+#endif

M src/main.cpp => src/main.cpp +4 -0
@@ 1,6 1,7 @@
 #include "raylib.h"
 #include "systems/AiSystem.hpp"
 #include "systems/CollisionSystem.hpp"
+#include "systems/DamageSystem.hpp"
 #include "systems/DestroySystem.hpp"
 #include "systems/MissileSystem.hpp"
 #include "systems/MovementSystem.hpp"


@@ 24,6 25,7 @@ Systems::MovementSystem movementSystem;
 Systems::DestroySystem destroySystem;
 Systems::AiSystem aiSystem;
 Systems::MissileSystem missileSystem;
+Systems::DamageSystem damageSystem;
 entt::registry<> registry;
 Camera camera;
 


@@ 33,6 35,7 @@ int main(int argc, char **argv) {
     renderSystem.Init(registry);
     collisionSystem.Init(registry);
     aiSystem.Init(registry);
+    damageSystem.Init();
     Components::readFromFile(registry, argc == 2 ? argv[1] : "data/test.json");
 
     camera = {0};


@@ 62,6 65,7 @@ int main(int argc, char **argv) {
 //----------------------------------------------------------------------------------
 void UpdateDrawFrame(void) {
     destroySystem.Update(registry);
+    damageSystem.Update(registry,GetFrameTime());
     playerInputSystem.Update(registry, camera);
     missileSystem.Update(registry);
     aiSystem.Update(registry, GetFrameTime());

M src/systems/AiSystem.cpp => src/systems/AiSystem.cpp +0 -34
@@ 72,40 72,6 @@ void AiSystem::Update(entt::registry<> &registry, const float &delta) {
             }
         });
 
-        registry.view<Components::Actor,Components::Fighter>().each(
-                [&](const auto entity, auto &actor, auto& fighter) {
-                if(fighter.isAttacking)
-                {
-                    if(fighter.currentAttackDuration > fighter.getTotalDuration())
-                    {
-                        fighter.isAttacking = false;
-                    }
-                    else
-                    {
-                        fighter.currentAttackDuration += delta;
-                    }
-                    const auto previousAction = actor.curAction;
-                    actor.curAction = fighter.getCurrentActivity();
-
-                    if(previousAction != actor.curAction)
-                    {
-                        if(actor.curAction == Components::Activity::AttackRecovery && registry.has<Components::DamageBox>(entity))
-                        {
-                            registry.remove<Components::DamageBox>(entity);
-                        }
-                        else if(actor.curAction == Components::Activity::AttackContact)
-                        {
-                            Components::DamageBox damageBox;
-                            damageBox.collision.offset = (Vector3){0.0,0.0,0.0f};
-                            damageBox.collision.size = (Vector3){3.0,3.0,3.0f};
-
-                            registry.assign_or_replace<Components::DamageBox>(entity,damageBox);
-                        }
-                    }
-                }
-        });
-
-
         registry.view<Components::Actor>().each(
                 [&](const auto entity, const auto &actor) {
         if(registry.has<Components::WorldScreenText>(entity)) {

M src/systems/CollisionSystem.cpp => src/systems/CollisionSystem.cpp +0 -1
@@ 60,7 60,6 @@ void CollisionSystem::PrepareCollisions(entt::registry<> &registry, std::uint32_
         {
             auto &damageBox = registry.get<Components::DamageBox>(entity);
             damageBox.box = BoundingBoxFromCollision(transform.pos,damageBox.collision);
-            std::cout << "Damage box applied\n";
         }
     }
 }

A src/systems/DamageSystem.cpp => src/systems/DamageSystem.cpp +47 -0
@@ 0,0 1,47 @@
+#include "DamageSystem.hpp"
+
+#include "components/Actor.hpp"
+#include "components/DamageBox.hpp"
+#include "components/Destroyable.hpp"
+#include "components/Fighter.hpp"
+
+#include "raylib.h"
+
+namespace Systems {
+void DamageSystem::Update(entt::registry<> &registry, const float delta) {
+        registry.view<Components::Actor,Components::Fighter>().each(
+                [&](const auto entity, auto &actor, auto& fighter) {
+                if(fighter.isAttacking)
+                {
+                    if(fighter.currentAttackDuration > fighter.getTotalDuration())
+                    {
+                        fighter.isAttacking = false;
+                    }
+                    else
+                    {
+                        fighter.currentAttackDuration += delta;
+                    }
+                    const auto previousAction = actor.curAction;
+                    actor.curAction = fighter.getCurrentActivity();
+
+                    if(previousAction != actor.curAction)
+                    {
+                        if(actor.curAction == Components::Activity::AttackRecovery && registry.has<Components::DamageBox>(entity))
+                        {
+                            registry.remove<Components::DamageBox>(entity);
+                        }
+                        else if(actor.curAction == Components::Activity::AttackContact)
+                        {
+                            Components::DamageBox damageBox;
+                            damageBox.collision.offset = (Vector3){0.0,0.0,0.0f};
+                            damageBox.collision.size = (Vector3){3.0,3.0,3.0f};
+                            damageBox.damage = 3;
+
+                            registry.assign_or_replace<Components::DamageBox>(entity,damageBox);
+                        }
+                    }
+                }
+        });
+}
+} // namespace Systems
+  // Systems

A src/systems/DamageSystem.hpp => src/systems/DamageSystem.hpp +16 -0
@@ 0,0 1,16 @@
+#ifndef DAMAGE_SYSTEM_HPP
+#define DAMAGE_SYSTEM_HPP
+#include <entt/entt.hpp>
+
+namespace Systems {
+class DamageSystem {
+  public:
+    DamageSystem(){};
+    void Init(){};
+    void Update(entt::registry<> &registry, const float delta);
+    void Shutdown(){};
+};
+} // namespace Systems
+  // Systems
+
+#endif