<commit_before>/// Project
#include <Manager/DamageManager.hpp>
#include <PlayerHandler.hpp>
// Components
#include <EntityComponent/EntityHandler.hpp>
#include <EntityComponent/Components/EntityTypeComponent.hpp>
#include <EntityComponent/Components/HealthComponent.hpp>
#include <EntityComponent/Components/AudioComponent.hpp>

// Events
#include <EventHandler/EventHandler.hpp>
#include <EventHandler/Events/DamageTakenEvent.hpp>
#include <EventHandler/Events/TriggerEvent.hpp>
#include <EventHandler/Events/PlaySoundEvent.hpp>

#include <Doremi/Core/Include/PlayerSpawnerHandler.hpp>

/// Engine
// Physics
#include <DoremiEngine/Physics/Include/PhysicsModule.hpp>
#include <DoremiEngine/Physics/Include/FluidManager.hpp>
#include <DoremiEngine/Physics/Include/RigidBodyManager.hpp>
#include <DoremiEngine/Physics/Include/CharacterControlManager.hpp>

// Third Party
#include <DirectXMath.h>
#include <iostream>
#include <set>
namespace Doremi
{
    namespace Core
    {
        DamageManager::DamageManager(const DoremiEngine::Core::SharedContext& p_sharedContext) : Manager(p_sharedContext, "DamageManager")
        {
            EventHandler::GetInstance()->Subscribe(EventType::Trigger, this);
        }

        DamageManager::~DamageManager() {}
        void DamageManager::Update(double p_dt)
        {
            std::map<uint32_t, Player*> t_players = PlayerHandler::GetInstance()->GetPlayerMap();
            EntityHandler& t_entityHandler = EntityHandler::GetInstance();

            // Check if the player was hit by any enemy bullet
            std::vector<DoremiEngine::Physics::CollisionPair> t_collisionPairs = m_sharedContext.GetPhysicsModule().GetCollisionPairs();
            size_t length = t_collisionPairs.size();

            // Start with getting every pair with a enemy bullet and save it to a list with bullet first, other second
            std::vector<XMINT2> t_bulletPairs;
            for(size_t i = 0; i < length; i++)
            {
                if(EntityHandler::GetInstance().HasComponents(t_collisionPairs[i].firstID, (int)ComponentType::EntityType))
                {
                    EntityTypeComponent* typeComp = EntityHandler::GetInstance().GetComponentFromStorage<EntityTypeComponent>(t_collisionPairs[i].firstID);
                    if(((int)typeComp->type & (int)EntityType::EnemyBullet) == (int)EntityType::EnemyBullet) // if first entity is bullet
                    {
                        t_bulletPairs.push_back(XMINT2(t_collisionPairs[i].firstID, t_collisionPairs[i].secondID));
                    }
                }
                else if(EntityHandler::GetInstance().HasComponents(t_collisionPairs[i].secondID, (int)ComponentType::EntityType))
                {
                    EntityTypeComponent* typeComp = EntityHandler::GetInstance().GetComponentFromStorage<EntityTypeComponent>(t_collisionPairs[i].secondID);
                    if(((int)typeComp->type & (int)EntityType::EnemyBullet) == (int)EntityType::EnemyBullet) // if second entity is bullet
                    {
                        t_bulletPairs.push_back(XMINT2(t_collisionPairs[i].secondID, t_collisionPairs[i].firstID));
                    }
                }
            }
            // for each player we check if a bullet did hit us
            length = t_bulletPairs.size();
            for(auto pairs : t_players)
            {
                for(size_t i = 0; i < length; i++)
                {
                    if(pairs.second->m_playerEntityID == t_bulletPairs[i].y)
                    {
                        // do we have a health comp?
                        std::cout << "Hit vs player detected" << std::endl;
                        if(EntityHandler::GetInstance().HasComponents(pairs.second->m_playerEntityID, (int)ComponentType::Health))
                        {
                            HealthComponent* hpComp = EntityHandler::GetInstance().GetComponentFromStorage<HealthComponent>(pairs.second->m_playerEntityID);

                            DamageTakenEvent* t_damageTakenEvent = new DamageTakenEvent(10, pairs.second->m_playerEntityID);

                            EventHandler::GetInstance()->BroadcastEvent(t_damageTakenEvent);
                            // hpComp->currentHealth -= 10; // TODOKO change to getting from comp
                            // if(hpComp->currentHealth <= 0)
                            //{
                            //    // u ded TODOKO
                            //    std::cout << "U DED!!!" << std::endl;
                            //}
                        }
                    }
                }
            }
            // We need this set to ensure that a bullet that hits multiple targets isnt removed twice
            std::set<int> removedBullets;
            for(size_t i = 0; i < length; i++)
            {
                // Remove all bullets that hit something, TODOKO review if this is what we want
                if(removedBullets.count(t_bulletPairs[i].x) == 0)
                {
                    EntityHandler::GetInstance().RemoveEntity(t_bulletPairs[i].x);
                    m_sharedContext.GetPhysicsModule().GetRigidBodyManager().RemoveBody(t_bulletPairs[i].x);
                    removedBullets.insert(t_bulletPairs[i].x);
                }
                else
                {
                    // The bullet did hit multiple targets but have already been removed, do nothing.
                }
            }
            // Check if the player hit any enemies

            // Look through our entities for the enemies

            size_t entitiesLength = EntityHandler::GetInstance().GetLastEntityIndex();
            for(size_t i = 0; i < entitiesLength; i++)
            {
                if(EntityHandler::GetInstance().HasComponents(i, (int)ComponentType::PressureParticleSystem))
                {
                    std::vector<int> t_drainsHit = m_sharedContext.GetPhysicsModule().GetFluidManager().GetDrainsHit(i);
                    size_t particleVecLength = t_drainsHit.size();

                    for(size_t o = 0; o < particleVecLength; o++)
                    {
                        if(t_drainsHit[o] != -1)
                        {
                            if(EntityHandler::GetInstance().HasComponents(t_drainsHit[o], (int)ComponentType::Health | (int)ComponentType::AIAgent |
                                                                                              (int)ComponentType::Transform | (int)ComponentType::CharacterController))
                            {
                                HealthComponent* drainHitHpComp = EntityHandler::GetInstance().GetComponentFromStorage<HealthComponent>(t_drainsHit[o]);
                                // TODOEA Make it related to the guns damage and not hard coded
                                // std::cout << drainHitHpComp->currentHealth << std::endl;
                                // TODOCONFIG
                                drainHitHpComp->currentHealth -= 2; // TODOKO change to getting from comp

                                DamageTakenEvent* t_damageTakenEvent = new DamageTakenEvent(2, t_drainsHit[o]);

                                EventHandler::GetInstance()->BroadcastEvent(t_damageTakenEvent);

                                // if(drainHitHpComp->currentHealth <= 0)
                                //{
                                //    // m_sharedContext.GetPhysicsModule().GetCharacterControlManager().RemoveCharacterController(t_drainsHit[o]);
                                //    EntityHandler::GetInstance().RemoveEntity(t_drainsHit[o]);
                                //    // DEBUG
                                //    // std::cout << "Enemy DED!!!" << std::endl;
                                //    // TODOSOUND Deathsound.
                                //}
                                // else
                                //{
                                //    // DEBUG
                                //    // std::cout << "Enemy Hit!!" << std::endl;
                                //    // TODOSOUND DamageSound
                                //}
                            }
                        }
                        else
                        {
                            // nothing
                        }
                    }
                }
            }
        }
        void DamageManager::OnEvent(Event* p_event)
        {
            if(p_event->eventType == EventType::Trigger)
            {
                TriggerEvent* t_triggerEvent = static_cast<TriggerEvent*>(p_event);

                if(t_triggerEvent->triggerType == TriggerType::DeathTrigger)
                {
                    if(EntityHandler::GetInstance().HasComponents(t_triggerEvent->objectEntityID, static_cast<uint32_t>(ComponentType::Health)))
                    {
                        if(PlayerHandler::GetInstance()->IsPlayer(t_triggerEvent->objectEntityID))
                        {
                            PlayerSpawnerHandler::GetInstance()->RespawnPlayer(t_triggerEvent->objectEntityID);
                        }
                        else
                        {
                            EntityHandler::GetInstance().RemoveEntity(t_triggerEvent->objectEntityID);
                        }

                      //  // Send soundevent for death.
                      //  AudioComponent* audioComp = EntityHandler::GetInstance().GetComponentFromStorage<AudioComponent>(t_triggerEvent->objectEntityID);
                      //  PlaySoundEvent* playSoundEvent =
                      //      new PlaySoundEvent(t_triggerEvent->objectEntityID, audioComp->m_enumToSoundID[(int32_t)AudioCompEnum::Death]);
                      //  EventHandler::GetInstance()->BroadcastEvent(playSoundEvent);
                    }
                }
            }
        }
    }
}<commit_msg>Forgot to add after formatting<commit_after>/// Project
#include <Manager/DamageManager.hpp>
#include <PlayerHandler.hpp>
// Components
#include <EntityComponent/EntityHandler.hpp>
#include <EntityComponent/Components/EntityTypeComponent.hpp>
#include <EntityComponent/Components/HealthComponent.hpp>
#include <EntityComponent/Components/AudioComponent.hpp>

// Events
#include <EventHandler/EventHandler.hpp>
#include <EventHandler/Events/DamageTakenEvent.hpp>
#include <EventHandler/Events/TriggerEvent.hpp>
#include <EventHandler/Events/PlaySoundEvent.hpp>

#include <Doremi/Core/Include/PlayerSpawnerHandler.hpp>

/// Engine
// Physics
#include <DoremiEngine/Physics/Include/PhysicsModule.hpp>
#include <DoremiEngine/Physics/Include/FluidManager.hpp>
#include <DoremiEngine/Physics/Include/RigidBodyManager.hpp>
#include <DoremiEngine/Physics/Include/CharacterControlManager.hpp>

// Third Party
#include <DirectXMath.h>
#include <iostream>
#include <set>
namespace Doremi
{
    namespace Core
    {
        DamageManager::DamageManager(const DoremiEngine::Core::SharedContext& p_sharedContext) : Manager(p_sharedContext, "DamageManager")
        {
            EventHandler::GetInstance()->Subscribe(EventType::Trigger, this);
        }

        DamageManager::~DamageManager() {}
        void DamageManager::Update(double p_dt)
        {
            std::map<uint32_t, Player*> t_players = PlayerHandler::GetInstance()->GetPlayerMap();
            EntityHandler& t_entityHandler = EntityHandler::GetInstance();

            // Check if the player was hit by any enemy bullet
            std::vector<DoremiEngine::Physics::CollisionPair> t_collisionPairs = m_sharedContext.GetPhysicsModule().GetCollisionPairs();
            size_t length = t_collisionPairs.size();

            // Start with getting every pair with a enemy bullet and save it to a list with bullet first, other second
            std::vector<XMINT2> t_bulletPairs;
            for(size_t i = 0; i < length; i++)
            {
                if(EntityHandler::GetInstance().HasComponents(t_collisionPairs[i].firstID, (int)ComponentType::EntityType))
                {
                    EntityTypeComponent* typeComp = EntityHandler::GetInstance().GetComponentFromStorage<EntityTypeComponent>(t_collisionPairs[i].firstID);
                    if(((int)typeComp->type & (int)EntityType::EnemyBullet) == (int)EntityType::EnemyBullet) // if first entity is bullet
                    {
                        t_bulletPairs.push_back(XMINT2(t_collisionPairs[i].firstID, t_collisionPairs[i].secondID));
                    }
                }
                else if(EntityHandler::GetInstance().HasComponents(t_collisionPairs[i].secondID, (int)ComponentType::EntityType))
                {
                    EntityTypeComponent* typeComp = EntityHandler::GetInstance().GetComponentFromStorage<EntityTypeComponent>(t_collisionPairs[i].secondID);
                    if(((int)typeComp->type & (int)EntityType::EnemyBullet) == (int)EntityType::EnemyBullet) // if second entity is bullet
                    {
                        t_bulletPairs.push_back(XMINT2(t_collisionPairs[i].secondID, t_collisionPairs[i].firstID));
                    }
                }
            }
            // for each player we check if a bullet did hit us
            length = t_bulletPairs.size();
            for(auto pairs : t_players)
            {
                for(size_t i = 0; i < length; i++)
                {
                    if(pairs.second->m_playerEntityID == t_bulletPairs[i].y)
                    {
                        // do we have a health comp?
                        std::cout << "Hit vs player detected" << std::endl;
                        if(EntityHandler::GetInstance().HasComponents(pairs.second->m_playerEntityID, (int)ComponentType::Health))
                        {
                            HealthComponent* hpComp = EntityHandler::GetInstance().GetComponentFromStorage<HealthComponent>(pairs.second->m_playerEntityID);

                            DamageTakenEvent* t_damageTakenEvent = new DamageTakenEvent(10, pairs.second->m_playerEntityID);

                            EventHandler::GetInstance()->BroadcastEvent(t_damageTakenEvent);
                            // hpComp->currentHealth -= 10; // TODOKO change to getting from comp
                            // if(hpComp->currentHealth <= 0)
                            //{
                            //    // u ded TODOKO
                            //    std::cout << "U DED!!!" << std::endl;
                            //}
                        }
                    }
                }
            }
            // We need this set to ensure that a bullet that hits multiple targets isnt removed twice
            std::set<int> removedBullets;
            for(size_t i = 0; i < length; i++)
            {
                // Remove all bullets that hit something, TODOKO review if this is what we want
                if(removedBullets.count(t_bulletPairs[i].x) == 0)
                {
                    EntityHandler::GetInstance().RemoveEntity(t_bulletPairs[i].x);
                    m_sharedContext.GetPhysicsModule().GetRigidBodyManager().RemoveBody(t_bulletPairs[i].x);
                    removedBullets.insert(t_bulletPairs[i].x);
                }
                else
                {
                    // The bullet did hit multiple targets but have already been removed, do nothing.
                }
            }
            // Check if the player hit any enemies

            // Look through our entities for the enemies

            size_t entitiesLength = EntityHandler::GetInstance().GetLastEntityIndex();
            for(size_t i = 0; i < entitiesLength; i++)
            {
                if(EntityHandler::GetInstance().HasComponents(i, (int)ComponentType::PressureParticleSystem))
                {
                    std::vector<int> t_drainsHit = m_sharedContext.GetPhysicsModule().GetFluidManager().GetDrainsHit(i);
                    size_t particleVecLength = t_drainsHit.size();

                    for(size_t o = 0; o < particleVecLength; o++)
                    {
                        if(t_drainsHit[o] != -1)
                        {
                            if(EntityHandler::GetInstance().HasComponents(t_drainsHit[o], (int)ComponentType::Health | (int)ComponentType::AIAgent |
                                                                                              (int)ComponentType::Transform | (int)ComponentType::CharacterController))
                            {
                                HealthComponent* drainHitHpComp = EntityHandler::GetInstance().GetComponentFromStorage<HealthComponent>(t_drainsHit[o]);
                                // TODOEA Make it related to the guns damage and not hard coded
                                // std::cout << drainHitHpComp->currentHealth << std::endl;
                                // TODOCONFIG
                                drainHitHpComp->currentHealth -= 2; // TODOKO change to getting from comp

                                DamageTakenEvent* t_damageTakenEvent = new DamageTakenEvent(2, t_drainsHit[o]);

                                EventHandler::GetInstance()->BroadcastEvent(t_damageTakenEvent);

                                // if(drainHitHpComp->currentHealth <= 0)
                                //{
                                //    // m_sharedContext.GetPhysicsModule().GetCharacterControlManager().RemoveCharacterController(t_drainsHit[o]);
                                //    EntityHandler::GetInstance().RemoveEntity(t_drainsHit[o]);
                                //    // DEBUG
                                //    // std::cout << "Enemy DED!!!" << std::endl;
                                //    // TODOSOUND Deathsound.
                                //}
                                // else
                                //{
                                //    // DEBUG
                                //    // std::cout << "Enemy Hit!!" << std::endl;
                                //    // TODOSOUND DamageSound
                                //}
                            }
                        }
                        else
                        {
                            // nothing
                        }
                    }
                }
            }
        }
        void DamageManager::OnEvent(Event* p_event)
        {
            if(p_event->eventType == EventType::Trigger)
            {
                TriggerEvent* t_triggerEvent = static_cast<TriggerEvent*>(p_event);

                if(t_triggerEvent->triggerType == TriggerType::DeathTrigger)
                {
                    if(EntityHandler::GetInstance().HasComponents(t_triggerEvent->objectEntityID, static_cast<uint32_t>(ComponentType::Health)))
                    {
                        if(PlayerHandler::GetInstance()->IsPlayer(t_triggerEvent->objectEntityID))
                        {
                            PlayerSpawnerHandler::GetInstance()->RespawnPlayer(t_triggerEvent->objectEntityID);
                        }
                        else
                        {
                            EntityHandler::GetInstance().RemoveEntity(t_triggerEvent->objectEntityID);
                        }

                        //  // Send soundevent for death.
                        //  AudioComponent* audioComp =
                        //  EntityHandler::GetInstance().GetComponentFromStorage<AudioComponent>(t_triggerEvent->objectEntityID);
                        //  PlaySoundEvent* playSoundEvent =
                        //      new PlaySoundEvent(t_triggerEvent->objectEntityID, audioComp->m_enumToSoundID[(int32_t)AudioCompEnum::Death]);
                        //  EventHandler::GetInstance()->BroadcastEvent(playSoundEvent);
                    }
                }
            }
        }
    }
}
{'fraction_non_alphanumeric': 0.10029706459896279, 'fraction_numerical': 0.002416796737324405, 'mean_word_length': 1.5395729446362358, 'pattern_counts': {'":': 0, '<': 117, '<?xml version=': 0, '>': 117, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 50, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '702', 'n_tokens_mistral': 5031, 'n_tokens_neox': 4770, 'n_words': 782}