diff --git a/include/Engine/Core/EPlayerDamage.h b/include/Engine/Core/EPlayerDamage.h index db070159..e0f2acd7 100644 --- a/include/Engine/Core/EPlayerDamage.h +++ b/include/Engine/Core/EPlayerDamage.h @@ -9,8 +9,8 @@ namespace Events struct PlayerDamage : Event { - int DamageAmount; - EntityID PlayerID; + double DamageAmount; + EntityID PlayerDamagedID; }; } diff --git a/include/Engine/Core/EPlayerHealthPickup.h b/include/Engine/Core/EPlayerHealthPickup.h index 2071f8b2..e7d01a4e 100644 --- a/include/Engine/Core/EPlayerHealthPickup.h +++ b/include/Engine/Core/EPlayerHealthPickup.h @@ -9,8 +9,9 @@ namespace Events struct PlayerHealthPickup : Event { - int HealthAmount; + double HealthAmount; EntityID HealthPickupID; + EntityID playerHealedID; }; } diff --git a/include/Game/HealthSystem.h b/include/Game/HealthSystem.h index fad2c616..c4ea4695 100644 --- a/include/Game/HealthSystem.h +++ b/include/Game/HealthSystem.h @@ -10,23 +10,27 @@ #include "Core\EPlayerHealthPickup.h"; #include "Core\EPlayerDeath.h"; +#include +#include + class HealthSystem : public PureSystem { public: HealthSystem(EventBroker* eventBroker); - virtual void UpdateComponent(World* world, ComponentWrapper& player, double dt) override; -private: - float m_Speed = 5; + //updatecomponent + virtual void UpdateComponent(World* world, ComponentWrapper& health, double dt) override; +private: //create the methods which will take care of specific events EventRelay m_EPlayerDamage; bool HealthSystem::OnPlayerDamaged(const Events::PlayerDamage& e); EventRelay m_EPlayerHealthPickup; bool HealthSystem::OnPlayerHealthPickup(const Events::PlayerHealthPickup& e); - int playerDeltaHealth; - + //create the vector which will keep track of health changes + std::vector> m_DeltaHealthVector; + }; #endif \ No newline at end of file diff --git a/resources/Schema/Components.xsd b/resources/Schema/Components.xsd index fd04fd39..7fcdd565 100644 --- a/resources/Schema/Components.xsd +++ b/resources/Schema/Components.xsd @@ -8,4 +8,5 @@ + \ No newline at end of file diff --git a/resources/Schema/Components/Health.xml b/resources/Schema/Components/Health.xml new file mode 100644 index 00000000..143a91d1 --- /dev/null +++ b/resources/Schema/Components/Health.xml @@ -0,0 +1,4 @@ + + 100 + 100 + \ No newline at end of file diff --git a/resources/Schema/Components/Health.xsd b/resources/Schema/Components/Health.xsd new file mode 100644 index 00000000..0da80c1f --- /dev/null +++ b/resources/Schema/Components/Health.xsd @@ -0,0 +1,14 @@ + + + + + + + + + + + + + + \ No newline at end of file diff --git a/resources/Schema/Types/Entity.xsd b/resources/Schema/Types/Entity.xsd index 92f7dc31..5178b525 100644 --- a/resources/Schema/Types/Entity.xsd +++ b/resources/Schema/Types/Entity.xsd @@ -15,6 +15,7 @@ + diff --git a/src/Game/Game.cpp b/src/Game/Game.cpp index 4242aa9c..8f479e03 100644 --- a/src/Game/Game.cpp +++ b/src/Game/Game.cpp @@ -28,7 +28,7 @@ Game::Game(int argc, char* argv[]) 0, m_Config->Get("Video.Width", 1280), m_Config->Get("Video.Height", 720) - )); + )); m_Renderer->Initialize(); m_Renderer->Camera()->SetFOV(glm::radians(m_Config->Get("Video.FOV", 90.f))); @@ -60,7 +60,26 @@ Game::Game(int argc, char* argv[]) m_SystemPipeline->AddSystem(); m_SystemPipeline->AddSystem(); - // Invoke network + //TEMP TEST DEL LATER + //skapar entityn som har komponenterna transf,model,player,health i sig. dvs är en spelare + EntityID playerID = m_World->CreateEntity(); + ComponentWrapper transform = m_World->AttachComponent(playerID, "Transform"); + ComponentWrapper model = m_World->AttachComponent(playerID, "Model"); + model["Resource"] = "Models/Core/UnitSphere.obj"; + ComponentWrapper player = m_World->AttachComponent(playerID, "Player"); + ComponentWrapper health = m_World->AttachComponent(playerID, "Health"); + Events::PlayerDamage e; + e.DamageAmount = 50.0f; + e.PlayerDamagedID = 9; + m_EventBroker->Publish(e); + Events::PlayerHealthPickup e2; + e2.HealthAmount = 40.0f; + e2.playerHealedID = 9; + m_EventBroker->Publish(e2); + + //END TEST + + // Invoke network if (m_Config->Get("Networking.StartNetwork", false)) { //boost::thread workerThread(&Game::networkFunction, this); networkFunction(); diff --git a/src/Game/HealthSystem.cpp b/src/Game/HealthSystem.cpp index 2e52f44c..9ab8f13a 100644 --- a/src/Game/HealthSystem.cpp +++ b/src/Game/HealthSystem.cpp @@ -1,4 +1,5 @@ #include "HealthSystem.h" +#include HealthSystem::HealthSystem(EventBroker* eventBroker) : PureSystem(eventBroker, "Health") @@ -6,33 +7,48 @@ HealthSystem::HealthSystem(EventBroker* eventBroker) //subscribe/listenTo playerdamage,healthpickup events with the eventbroker EVENT_SUBSCRIBE_MEMBER(m_EPlayerDamage, &HealthSystem::OnPlayerDamaged); EVENT_SUBSCRIBE_MEMBER(m_EPlayerHealthPickup, &HealthSystem::OnPlayerHealthPickup); - playerDeltaHealth = 0; } -void HealthSystem::UpdateComponent(World * world, ComponentWrapper & player, double dt) + +void HealthSystem::UpdateComponent(World * world, ComponentWrapper & health, double dt) { - //Health is only affected by pickup/shoot events - player["Health"] += playerDeltaHealth; - playerDeltaHealth = 0; - if (player["Health"] < 0) { - //sendout/publish death event + //if entityID of health is 9 then the players ID is also 9 (player,health are connected to the same entity) + ComponentWrapper player = world->GetComponent(health.EntityID, "Player"); + double currentHealth = (double) world->GetComponent(health.EntityID, "Health")["Health"]; + double maxHealth = (double)world->GetComponent(health.EntityID, "Health")["MaxHealth"]; + + //process the DeltaHealthVector and change the entitys health accordingly + for (size_t i = m_DeltaHealthVector.size(); i >0; i--) + { + auto deltaHP = m_DeltaHealthVector[i-1]; + if (std::get<0>(deltaHP) == player.EntityID) { + //re-read currentHealth for each iteration + currentHealth = (double)world->GetComponent(health.EntityID, "Health")["Health"]; + //get the deltaHP value from the tuple and make sure you dont get more than maxHealth + double newHealth = std::min(currentHealth + (double)std::get<1>(deltaHP), maxHealth); + health.SetProperty("Health", newHealth); + m_DeltaHealthVector.erase(m_DeltaHealthVector.begin()+i-1); + } + } + + currentHealth = (double)world->GetComponent(health.EntityID, "Health")["Health"]; + if (currentHealth < 0.0f) { + //publish death event Events::PlayerDeath e; e.PlayerID = player.EntityID; m_EventBroker->Publish(e); } } + bool HealthSystem::OnPlayerDamaged(const Events::PlayerDamage& e) { - //vem skadades? - //antagligen spelaren själv - //såna här events skickas av network te spelare som kan lyssna på / kolla på de och tar hand om sina egna +-hp endast - //just add that damage to a variable, which will later be taken care of by UpdateComponent - playerDeltaHealth -= e.DamageAmount; - //ev skicka ut playerdeath event + //save the changed HP to a vector. it will be taken care of in UpdateComponent + m_DeltaHealthVector.push_back(std::make_tuple(e.PlayerDamagedID, -e.DamageAmount)); return true; } + bool HealthSystem::OnPlayerHealthPickup(const Events::PlayerHealthPickup& e) { - //vem tog upp hp? - playerDeltaHealth += e.HealthAmount; + //save the changed HP to a vector. it will be taken care of in UpdateComponent + m_DeltaHealthVector.push_back(std::make_tuple(e.playerHealedID, e.HealthAmount)); return true; }