<commit_before>#pragma once
// Project specific
#include <PlayerHandler.hpp>
#include <Doremi/Core/Include/InputHandler.hpp>
#include <DoremiEngine/Physics/Include/PhysicsModule.hpp>
#include <DoremiEngine/Physics/Include/RigidBodyManager.hpp>
#include <DoremiEngine/Physics/Include/CharacterControlManager.hpp>
//#include <DoremiEngine/Physics/Include/PhysicsMaterialManager.hpp>
#include <DoremiEngine/Physics/Include/PhysicsMaterialManager.hpp>
#include <EntityComponent/EntityHandler.hpp>
#include <Doremi/Core/Include/EntityComponent/Components/TransformComponent.hpp>
#include <EntityComponent/Components/RigidBodyComponent.hpp>
#include <EntityComponent/Components/PhysicsMaterialComponent.hpp>
#include <EntityComponent/Components/MovementComponent.hpp>
#include <EntityComponent/Components/JumpComponent.hpp>
#include <EntityComponent/Components/GravityComponent.hpp>
#include <EntityComponent/Components/PressureParticleComponent.hpp>
#include <InputHandlerClient.hpp>
#include <DoremiEngine/Input/Include/InputModule.hpp>
#include <Doremi/Core/Include/EventHandler/EventHandler.hpp>
#include <Doremi/Core/Include/EventHandler/Events/PlayerCreationEvent.hpp>
#include <Doremi/Core/Include/InputHandlerServer.hpp>
#include <Doremi/Core/Include/AddRemoveSyncHandler.hpp>
#include <Doremi/Core/Include/FrequencyBufferHandler.hpp>

// Timing
#include <Utility/Timer/Include/Measure/MeasureTimer.hpp>

#include <iostream>


namespace Doremi
{
    namespace Core
    {
        PlayerHandler::PlayerHandler(const DoremiEngine::Core::SharedContext& p_sharedContext) : m_sharedContext(p_sharedContext) {}

        PlayerHandler::~PlayerHandler() {}

        PlayerHandler* PlayerHandler::m_singleton = nullptr;

        void PlayerHandler::StartPlayerHandler(const DoremiEngine::Core::SharedContext& p_sharedContext)
        {
            if(m_singleton != nullptr)
            {
                std::runtime_error("PlayerHandler StartPlayerHandler called multiple times.");
            }
            m_singleton = new PlayerHandler(p_sharedContext);
        }

        PlayerHandler* PlayerHandler::GetInstance()
        {
            if(m_singleton == nullptr)
            {
                std::runtime_error("PlayerHandler not initialized, GetInstance called.");
            }

            return m_singleton;
        }

        std::map<uint32_t, Player*>& PlayerHandler::GetPlayerMap() { return m_playerMap; }

        InputHandler* PlayerHandler::GetDefaultInputHandler()
        {
            InputHandler* OutPointer = nullptr;

            if(m_playerMap.size())
            {
                OutPointer = m_playerMap.begin()->second->m_inputHandler;
            }

            return OutPointer;
        }

        InputHandler* PlayerHandler::GetInputHandlerForPlayer(uint32_t p_playerID)
        {
            std::map<uint32_t, Player*>::iterator iter = m_playerMap.find(p_playerID);

            InputHandler* outPointer = nullptr;

            if(iter != m_playerMap.end())
            {
                outPointer = iter->second->m_inputHandler;
            }

            return outPointer;
        }

        AddRemoveSyncHandler* PlayerHandler::GetAddRemoveSyncHandlerForPlayer(uint32_t p_playerID)
        {
            std::map<uint32_t, Player*>::iterator iter = m_playerMap.find(p_playerID);

            AddRemoveSyncHandler* outPointer = nullptr;

            if(iter != m_playerMap.end())
            {
                outPointer = iter->second->m_addRemoveSyncHandler;
            }

            return outPointer;
        }

        FrequencyBufferHandler* PlayerHandler::GetDefaultFrequencyBufferHandler()
        {
            FrequencyBufferHandler* OutPointer = nullptr;

            if(m_playerMap.size())
            {
                OutPointer = m_playerMap.begin()->second->m_frequencyBufferHandler;
            }

            return OutPointer;
        }

        FrequencyBufferHandler* PlayerHandler::GetFrequencyBufferHandlerForPlayer(uint32_t p_playerID)
        {
            std::map<uint32_t, Player*>::iterator iter = m_playerMap.find(p_playerID);

            FrequencyBufferHandler* outPointer = nullptr;

            if(iter != m_playerMap.end())
            {
                outPointer = iter->second->m_frequencyBufferHandler;
            }

            return outPointer;
        }

        bool PlayerHandler::GetDefaultPlayerEntityID(EntityID& o_outID)
        {
            if(m_playerMap.size() == 0)
            {
                std::runtime_error("GetDefaultPlayerEntityID called without any players aviable.");
                return false;
            }
            o_outID = m_playerMap.begin()->second->m_playerEntityID;

            return true;
        }

        bool PlayerHandler::GetEntityIDForPlayer(uint32_t p_playerID, EntityID& outID)
        {
            std::map<uint32_t, Player*>::iterator iter = m_playerMap.find(p_playerID);


            if(iter != m_playerMap.end())
            {
                outID = iter->second->m_playerEntityID;
                return true;
            }
            else
            {
                return false;
            }
        }

        void PlayerHandler::CreateNewPlayer(uint32_t p_playerID, InputHandler* p_inputHandler)
        {
            std::map<uint32_t, Player*>::iterator iter = m_playerMap.find(p_playerID);


            if(iter != m_playerMap.end())
            {
                std::runtime_error("Creating player twice with same ID.");
            }

            AddRemoveSyncHandler* newAddRemoveSyncHandler = new AddRemoveSyncHandler();
            FrequencyBufferHandler* newFrequencyHandler = new FrequencyBufferHandler();

            Player* NewPlayer = new Player(p_inputHandler, newAddRemoveSyncHandler, newFrequencyHandler);

            // TODOCM hard coded entityID for new players
            NewPlayer->m_playerEntityID = EntityHandler::GetInstance().CreateEntity(Blueprints::PlayerEntity);

            m_playerMap[p_playerID] = NewPlayer;


            int materialID = EntityHandler::GetInstance().GetComponentFromStorage<Core::PhysicsMaterialComponent>(NewPlayer->m_playerEntityID)->p_materialID;
            DirectX::XMFLOAT3 position = DirectX::XMFLOAT3(5, 30, 0);
            DirectX::XMFLOAT4 orientation = DirectX::XMFLOAT4(0, 0, 0, 1);
            m_sharedContext.GetPhysicsModule().GetCharacterControlManager().AddController(NewPlayer->m_playerEntityID, materialID, position, XMFLOAT2(1, 1));

            /// Create the gun
            // Check if we have the gun
            if(EntityHandler::GetInstance().HasComponents(NewPlayer->m_playerEntityID, (int)ComponentType::PressureParticleSystem))
            {
                ParticlePressureComponent* particleComp =
                    EntityHandler::GetInstance().GetComponentFromStorage<ParticlePressureComponent>(NewPlayer->m_playerEntityID);
                particleComp->data.m_active = false;
                particleComp->data.m_density = 2;
                particleComp->data.m_dimensions = XMFLOAT2(0, 0);
                particleComp->data.m_direction = EntityHandler::GetInstance().GetComponentFromStorage<TransformComponent>(NewPlayer->m_playerEntityID)->rotation;
                particleComp->data.m_emissionAreaDimensions = XMFLOAT2(0.1, 0.4);
                particleComp->data.m_emissionRate = 0.1;
                particleComp->data.m_launchPressure = 100;
                particleComp->data.m_position = EntityHandler::GetInstance().GetComponentFromStorage<TransformComponent>(NewPlayer->m_playerEntityID)->position;
                m_sharedContext.GetPhysicsModule().GetFluidManager().CreateParticleEmitter(NewPlayer->m_playerEntityID, particleComp->data);
            }


            // Create event
            PlayerCreationEvent* playerCreateEvent = new PlayerCreationEvent(NewPlayer->m_playerEntityID);

            // Broadcast event<commit_msg>Fixed float warning in PlayerHandler.cpp<commit_after>#pragma once
// Project specific
#include <PlayerHandler.hpp>
#include <Doremi/Core/Include/InputHandler.hpp>
#include <DoremiEngine/Physics/Include/PhysicsModule.hpp>
#include <DoremiEngine/Physics/Include/RigidBodyManager.hpp>
#include <DoremiEngine/Physics/Include/CharacterControlManager.hpp>
//#include <DoremiEngine/Physics/Include/PhysicsMaterialManager.hpp>
#include <DoremiEngine/Physics/Include/PhysicsMaterialManager.hpp>
#include <EntityComponent/EntityHandler.hpp>
#include <Doremi/Core/Include/EntityComponent/Components/TransformComponent.hpp>
#include <EntityComponent/Components/RigidBodyComponent.hpp>
#include <EntityComponent/Components/PhysicsMaterialComponent.hpp>
#include <EntityComponent/Components/MovementComponent.hpp>
#include <EntityComponent/Components/JumpComponent.hpp>
#include <EntityComponent/Components/GravityComponent.hpp>
#include <EntityComponent/Components/PressureParticleComponent.hpp>
#include <InputHandlerClient.hpp>
#include <DoremiEngine/Input/Include/InputModule.hpp>
#include <Doremi/Core/Include/EventHandler/EventHandler.hpp>
#include <Doremi/Core/Include/EventHandler/Events/PlayerCreationEvent.hpp>
#include <Doremi/Core/Include/InputHandlerServer.hpp>
#include <Doremi/Core/Include/AddRemoveSyncHandler.hpp>
#include <Doremi/Core/Include/FrequencyBufferHandler.hpp>

// Timing
#include <Utility/Timer/Include/Measure/MeasureTimer.hpp>

#include <iostream>


namespace Doremi
{
    namespace Core
    {
        PlayerHandler::PlayerHandler(const DoremiEngine::Core::SharedContext& p_sharedContext) : m_sharedContext(p_sharedContext) {}

        PlayerHandler::~PlayerHandler() {}

        PlayerHandler* PlayerHandler::m_singleton = nullptr;

        void PlayerHandler::StartPlayerHandler(const DoremiEngine::Core::SharedContext& p_sharedContext)
        {
            if(m_singleton != nullptr)
            {
                std::runtime_error("PlayerHandler StartPlayerHandler called multiple times.");
            }
            m_singleton = new PlayerHandler(p_sharedContext);
        }

        PlayerHandler* PlayerHandler::GetInstance()
        {
            if(m_singleton == nullptr)
            {
                std::runtime_error("PlayerHandler not initialized, GetInstance called.");
            }

            return m_singleton;
        }

        std::map<uint32_t, Player*>& PlayerHandler::GetPlayerMap() { return m_playerMap; }

        InputHandler* PlayerHandler::GetDefaultInputHandler()
        {
            InputHandler* OutPointer = nullptr;

            if(m_playerMap.size())
            {
                OutPointer = m_playerMap.begin()->second->m_inputHandler;
            }

            return OutPointer;
        }

        InputHandler* PlayerHandler::GetInputHandlerForPlayer(uint32_t p_playerID)
        {
            std::map<uint32_t, Player*>::iterator iter = m_playerMap.find(p_playerID);

            InputHandler* outPointer = nullptr;

            if(iter != m_playerMap.end())
            {
                outPointer = iter->second->m_inputHandler;
            }

            return outPointer;
        }

        AddRemoveSyncHandler* PlayerHandler::GetAddRemoveSyncHandlerForPlayer(uint32_t p_playerID)
        {
            std::map<uint32_t, Player*>::iterator iter = m_playerMap.find(p_playerID);

            AddRemoveSyncHandler* outPointer = nullptr;

            if(iter != m_playerMap.end())
            {
                outPointer = iter->second->m_addRemoveSyncHandler;
            }

            return outPointer;
        }

        FrequencyBufferHandler* PlayerHandler::GetDefaultFrequencyBufferHandler()
        {
            FrequencyBufferHandler* OutPointer = nullptr;

            if(m_playerMap.size())
            {
                OutPointer = m_playerMap.begin()->second->m_frequencyBufferHandler;
            }

            return OutPointer;
        }

        FrequencyBufferHandler* PlayerHandler::GetFrequencyBufferHandlerForPlayer(uint32_t p_playerID)
        {
            std::map<uint32_t, Player*>::iterator iter = m_playerMap.find(p_playerID);

            FrequencyBufferHandler* outPointer = nullptr;

            if(iter != m_playerMap.end())
            {
                outPointer = iter->second->m_frequencyBufferHandler;
            }

            return outPointer;
        }

        bool PlayerHandler::GetDefaultPlayerEntityID(EntityID& o_outID)
        {
            if(m_playerMap.size() == 0)
            {
                std::runtime_error("GetDefaultPlayerEntityID called without any players aviable.");
                return false;
            }
            o_outID = m_playerMap.begin()->second->m_playerEntityID;

            return true;
        }

        bool PlayerHandler::GetEntityIDForPlayer(uint32_t p_playerID, EntityID& outID)
        {
            std::map<uint32_t, Player*>::iterator iter = m_playerMap.find(p_playerID);


            if(iter != m_playerMap.end())
            {
                outID = iter->second->m_playerEntityID;
                return true;
            }
            else
            {
                return false;
            }
        }

        void PlayerHandler::CreateNewPlayer(uint32_t p_playerID, InputHandler* p_inputHandler)
        {
            std::map<uint32_t, Player*>::iterator iter = m_playerMap.find(p_playerID);


            if(iter != m_playerMap.end())
            {
                std::runtime_error("Creating player twice with same ID.");
            }

            AddRemoveSyncHandler* newAddRemoveSyncHandler = new AddRemoveSyncHandler();
            FrequencyBufferHandler* newFrequencyHandler = new FrequencyBufferHandler();

            Player* NewPlayer = new Player(p_inputHandler, newAddRemoveSyncHandler, newFrequencyHandler);

            // TODOCM hard coded entityID for new players
            NewPlayer->m_playerEntityID = EntityHandler::GetInstance().CreateEntity(Blueprints::PlayerEntity);

            m_playerMap[p_playerID] = NewPlayer;


            int materialID = EntityHandler::GetInstance().GetComponentFromStorage<Core::PhysicsMaterialComponent>(NewPlayer->m_playerEntityID)->p_materialID;
            DirectX::XMFLOAT3 position = DirectX::XMFLOAT3(5.0f, 30.0f, 0.0f);
            DirectX::XMFLOAT4 orientation = DirectX::XMFLOAT4(0.0f, 0.0f, 0.0f, 1.0f);
            m_sharedContext.GetPhysicsModule().GetCharacterControlManager().AddController(NewPlayer->m_playerEntityID, materialID, position, XMFLOAT2(1.0f, 1.0f));

            /// Create the gun
            // Check if we have the gun
            if(EntityHandler::GetInstance().HasComponents(NewPlayer->m_playerEntityID, (int)ComponentType::PressureParticleSystem))
            {
                ParticlePressureComponent* particleComp =
                    EntityHandler::GetInstance().GetComponentFromStorage<ParticlePressureComponent>(NewPlayer->m_playerEntityID);
                particleComp->data.m_active = false;
                particleComp->data.m_density = 2.0f;
                particleComp->data.m_dimensions = XMFLOAT2(0.0f, 0.0f);
                particleComp->data.m_direction = EntityHandler::GetInstance().GetComponentFromStorage<TransformComponent>(NewPlayer->m_playerEntityID)->rotation;
                particleComp->data.m_emissionAreaDimensions = XMFLOAT2(0.1f, 0.4f);
                particleComp->data.m_emissionRate = 0.1f;
                particleComp->data.m_launchPressure = 100;
                particleComp->data.m_position = EntityHandler::GetInstance().GetComponentFromStorage<TransformComponent>(NewPlayer->m_playerEntityID)->position;
                m_sharedContext.GetPhysicsModule().GetFluidManager().CreateParticleEmitter(NewPlayer->m_playerEntityID, particleComp->data);
            }


            // Create event
            PlayerCreationEvent* playerCreateEvent = new PlayerCreationEvent(NewPlayer->m_playerEntityID);

            // Broadcast event
{'fraction_non_alphanumeric': 0.09840929175609141, 'fraction_numerical': 0.007322307789420527, 'mean_word_length': 2.708567415730337, 'pattern_counts': {'":': 0, '<': 71, '<?xml version=': 0, '>': 141, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6701769', 'n_tokens_mistral': 4357, 'n_tokens_neox': 4144, 'n_words': 605}