From 59be714fdc8f4638ef547db6cc76b6e1b026068e Mon Sep 17 00:00:00 2001 From: verysecrethero Date: Tue, 12 Jan 2016 16:35:55 +0100 Subject: [PATCH] 1 Component added: CapturePoint 1 System added: CapturePointSystem 1 Test added: CapturePointTest added 1 variable in PlayerComponent (TeamNumber) added 2 variables in CapturePoint (CaptureTimer,OwnedBy) CapturePointSystem is now handling 2 events: OnTriggerTouch,OnTriggerLeave Added the CapturePointSystem to Game.cpp --- include/Game/CapturePointSystem.h | 35 +++++ resources/Schema/Components.xsd | 2 +- resources/Schema/Components/CapturePoint.xml | 4 + resources/Schema/Components/CapturePoint.xsd | 17 +++ resources/Schema/Components/Player.xml | 1 + resources/Schema/Components/Player.xsd | 1 + resources/Schema/Types/Entity.xsd | 1 + src/Game/CapturePointSystem.cpp | 91 +++++++++++++ src/Game/Game.cpp | 2 + src/Tests/CapturePointTest.cpp | 130 +++++++++++++++++++ src/Tests/CapturePointTest.h | 42 ++++++ 11 files changed, 325 insertions(+), 1 deletion(-) create mode 100644 include/Game/CapturePointSystem.h create mode 100644 resources/Schema/Components/CapturePoint.xml create mode 100644 resources/Schema/Components/CapturePoint.xsd create mode 100644 src/Game/CapturePointSystem.cpp create mode 100644 src/Tests/CapturePointTest.cpp create mode 100644 src/Tests/CapturePointTest.h diff --git a/include/Game/CapturePointSystem.h b/include/Game/CapturePointSystem.h new file mode 100644 index 00000000..6030ad74 --- /dev/null +++ b/include/Game/CapturePointSystem.h @@ -0,0 +1,35 @@ +#ifndef CapturePointSystem_h__ +#define CapturePointSystem_h__ + +#include +#include + +#include "Common.h" +#include "Core/System.h" +#include "Engine/Collision/ETrigger.h" + +#include +#include + +class CapturePointSystem : public PureSystem +{ +public: + //TODO: on new map, destroy all info in the vectors + CapturePointSystem(EventBroker* eventBroker); + + //updatecomponent + virtual void UpdateComponent(World* world, ComponentWrapper& capturePoint, double dt) override; + +private: + //methods which will take care of specific events + EventRelay m_ETriggerTouch; + bool CapturePointSystem::OnTriggerTouch(const Events::TriggerTouch& e); + EventRelay m_ETriggerLeave; + bool CapturePointSystem::OnTriggerLeave(const Events::TriggerLeave& e); + + //vectors which will keep track of enter/leave changes + std::vector> m_ETriggerTouchVector; + std::vector> m_ETriggerLeaveVector; +}; + +#endif \ No newline at end of file diff --git a/resources/Schema/Components.xsd b/resources/Schema/Components.xsd index 33714638..1a78ed0c 100644 --- a/resources/Schema/Components.xsd +++ b/resources/Schema/Components.xsd @@ -11,5 +11,5 @@ - + \ No newline at end of file diff --git a/resources/Schema/Components/CapturePoint.xml b/resources/Schema/Components/CapturePoint.xml new file mode 100644 index 00000000..efab1a5a --- /dev/null +++ b/resources/Schema/Components/CapturePoint.xml @@ -0,0 +1,4 @@ + + 0 + 0 + \ No newline at end of file diff --git a/resources/Schema/Components/CapturePoint.xsd b/resources/Schema/Components/CapturePoint.xsd new file mode 100644 index 00000000..09842933 --- /dev/null +++ b/resources/Schema/Components/CapturePoint.xsd @@ -0,0 +1,17 @@ + + + + + + + + A Capture Point + + + + + + + + + \ No newline at end of file diff --git a/resources/Schema/Components/Player.xml b/resources/Schema/Components/Player.xml index cd3d1620..a05c003e 100644 --- a/resources/Schema/Components/Player.xml +++ b/resources/Schema/Components/Player.xml @@ -1,4 +1,5 @@ + 0 0 false diff --git a/resources/Schema/Components/Player.xsd b/resources/Schema/Components/Player.xsd index 617b7d30..49b0537a 100644 --- a/resources/Schema/Components/Player.xsd +++ b/resources/Schema/Components/Player.xsd @@ -15,6 +15,7 @@ + diff --git a/resources/Schema/Types/Entity.xsd b/resources/Schema/Types/Entity.xsd index 4b67276a..2f352354 100644 --- a/resources/Schema/Types/Entity.xsd +++ b/resources/Schema/Types/Entity.xsd @@ -18,6 +18,7 @@ + diff --git a/src/Game/CapturePointSystem.cpp b/src/Game/CapturePointSystem.cpp new file mode 100644 index 00000000..b7c32c5e --- /dev/null +++ b/src/Game/CapturePointSystem.cpp @@ -0,0 +1,91 @@ +#include "CapturePointSystem.h" +#include + +CapturePointSystem::CapturePointSystem(EventBroker* eventBroker) + : PureSystem(eventBroker, "CapturePoint") +{ + //subscribe/listenTo playerdamage,healthpickup events (using the eventBroker) + EVENT_SUBSCRIBE_MEMBER(m_ETriggerTouch, &CapturePointSystem::OnTriggerTouch); + EVENT_SUBSCRIBE_MEMBER(m_ETriggerLeave, &CapturePointSystem::OnTriggerLeave); +} + +//here all capturepoints will update their component +void CapturePointSystem::UpdateComponent(World *world, ComponentWrapper &capturePoint, double dt) +{ + //NOTE: needs to run each frame, since we're possibly increasing the captureTimer for the capturePoint by dt + int firstTeamPlayersStandingInside = 0; + int secondTeamPlayersStandingInside = 0; + + for (size_t i = 0; i < m_ETriggerTouchVector.size(); i++) + { + auto triggerTouched = m_ETriggerTouchVector[i]; + if (std::get<1>(triggerTouched) == capturePoint.EntityID) { + //some player has touched this - lets figure out: what team, health + EntityID playerID = std::get<0>(triggerTouched); + bool hasHealthComponent = world->HasComponent(playerID, "Health"); + if (!hasHealthComponent) + continue; + double currentHealth = world->GetComponent(playerID, "Health")["Health"]; + //check if player is dead + if ((int)currentHealth == 0) + continue; + //check team - 0 = no team + int teamNumber = (int)world->GetComponent(playerID, "Player")["TeamNumber"]; + if (teamNumber == 1) + firstTeamPlayersStandingInside++; + if (teamNumber == 2) + secondTeamPlayersStandingInside++; + continue; + } + } + + int ownedBy = capturePoint["OwnedBy"]; + double captureTimer = capturePoint["CaptureTimer"]; + + //A.nobodys standing inside + if (firstTeamPlayersStandingInside == 0 && secondTeamPlayersStandingInside == 0) { + //do nothing (?) + } + //B.first team has players but second none + if (firstTeamPlayersStandingInside > 0 && secondTeamPlayersStandingInside == 0) { + if (ownedBy == 2 || ownedBy == 0) + capturePoint["CaptureTimer"] = (double)capturePoint["CaptureTimer"] + dt; + //check if captureTimer > 5 and if so change owner + if ((double)capturePoint["CaptureTimer"] > 5.0) { + capturePoint["OwnedBy"] = 1; + capturePoint["CaptureTimer"] = 0; + } + } + //C.second team has players but second none + if (firstTeamPlayersStandingInside == 0 && secondTeamPlayersStandingInside > 0) { + + } + //D.both teams have players inside + if (firstTeamPlayersStandingInside > 0 && secondTeamPlayersStandingInside > 0) { + + } + + + +} + +bool CapturePointSystem::OnTriggerTouch(const Events::TriggerTouch& e) +{ + //auto personEntered = e.Entity; + //auto thingEntered = e.Trigger; + m_ETriggerTouchVector.push_back(std::make_tuple(e.Entity, e.Trigger)); + return true; +} + +bool CapturePointSystem::OnTriggerLeave(const Events::TriggerLeave& e) +{ + for (size_t i = 0; i < m_ETriggerTouchVector.size(); i++) + { + auto triggerTouched = m_ETriggerTouchVector[i]; + if (std::get<0>(triggerTouched) == e.Entity && std::get<1>(triggerTouched) == e.Trigger) { + m_ETriggerTouchVector.erase(m_ETriggerTouchVector.begin() + i); + break; + } + } + return true; +} diff --git a/src/Game/Game.cpp b/src/Game/Game.cpp index c84c556b..4608d0b6 100644 --- a/src/Game/Game.cpp +++ b/src/Game/Game.cpp @@ -3,6 +3,7 @@ #include "Collision/CollisionSystem.h" #include "Game/HealthSystem.h" #include "Core/EntityFileWriter.h" +#include "Game/CapturePointSystem.h" Game::Game(int argc, char* argv[]) { @@ -70,6 +71,7 @@ Game::Game(int argc, char* argv[]) ++updateOrderLevel; m_SystemPipeline->AddSystem(updateOrderLevel); m_SystemPipeline->AddSystem(updateOrderLevel); + m_SystemPipeline->AddSystem(updateOrderLevel); // Invoke network if (m_Config->Get("Networking.StartNetwork", false)) { diff --git a/src/Tests/CapturePointTest.cpp b/src/Tests/CapturePointTest.cpp new file mode 100644 index 00000000..a0d61e04 --- /dev/null +++ b/src/Tests/CapturePointTest.cpp @@ -0,0 +1,130 @@ +#include +using boost::unit_test_framework::test_suite; +using boost::unit_test_framework::test_case; + +#include "CapturePointTest.h" +#include "Game/HealthSystem.h" + +#include "Collision/TriggerSystem.h" +#include "Collision/CollisionSystem.h" +#include "Core/EntityFileWriter.h" +#include "Game/CapturePointSystem.h" + +BOOST_AUTO_TEST_SUITE(ShootEventTestSuite) + +//dont use the same name as the classname in test cases... +BOOST_AUTO_TEST_CASE(CapturePointTest1) +{ + //Test firing primary weapon + CapturePointTest game(1); + //100 loops will be more than enough to do the test + int loops = 100; + bool success = false; + while (loops > 0) { + game.Tick(); + if (game.TestSucceeded) { + success = true; + break; + } + loops--; + } + //The system will process the events, hence it will take a while before we can read anything + BOOST_TEST(success); +} +BOOST_AUTO_TEST_SUITE_END() + +CapturePointTest::CapturePointTest(int runTestNumber) +{ + ResourceManager::RegisterType("ConfigFile"); + ResourceManager::RegisterType("EntityFile"); + + m_Config = ResourceManager::Load("Config.ini"); + std::string mapToLoad = m_Config->Get("Debug.LoadMap", ""); + LOG_LEVEL = static_cast<_LOG_LEVEL>(m_Config->Get("Debug.LogLevel", 1)); + + // Create the core event broker + m_EventBroker = new EventBroker(); + + // Create a world + m_World = new World(); + + // Create system pipeline + m_SystemPipeline = new SystemPipeline(m_EventBroker); + m_SystemPipeline->AddSystem(0); + m_SystemPipeline->AddSystem(0); + m_SystemPipeline->AddSystem(1); + m_SystemPipeline->AddSystem(1); + m_SystemPipeline->AddSystem(1); + + if (!mapToLoad.empty()) { + auto file = ResourceManager::Load(mapToLoad); + EntityFilePreprocessor fpp(file); + fpp.RegisterComponents(m_World); + EntityFileParser fp(file); + fp.MergeEntities(m_World); + } + + //The Test + //create entity which has transform,player,model,health in it. i.e. is a player + EntityID playerID = m_World->CreateEntity(); + m_PlayerID = playerID; + ComponentWrapper& player = m_World->AttachComponent(playerID, "Player"); + ComponentWrapper health = m_World->AttachComponent(playerID, "Health"); + player["TeamNumber"] = 1; + + EntityID playerID2 = m_World->CreateEntity(); + ComponentWrapper& player2 = m_World->AttachComponent(playerID2, "Player"); + m_PlayerID2 = playerID2; + ComponentWrapper health2 = m_World->AttachComponent(playerID2, "Health"); + player2["TeamNumber"] = 2; + + EntityID capturePointID = m_World->CreateEntity(); + ComponentWrapper& capPointComp = m_World->AttachComponent(capturePointID, "CapturePoint"); + m_CapturePointID = capturePointID; + m_RunTestNumber = runTestNumber; + + //add some touch/leave events + Events::TriggerTouch eTriggerTouched; + eTriggerTouched.Entity = m_PlayerID; + eTriggerTouched.Trigger = m_CapturePointID; + m_EventBroker->Publish(eTriggerTouched); + + Events::TriggerTouch eTriggerTouched2; + eTriggerTouched2.Entity = m_PlayerID2; + eTriggerTouched2.Trigger = m_CapturePointID; + m_EventBroker->Publish(eTriggerTouched2); + + Events::TriggerLeave eTriggerLeft; + eTriggerLeft.Entity = m_PlayerID; + eTriggerLeft.Trigger = m_CapturePointID; + m_EventBroker->Publish(eTriggerLeft); + + Events::TriggerTouch eTriggerTouched3; + eTriggerTouched3.Entity = m_PlayerID; + eTriggerTouched3.Trigger = m_CapturePointID; + m_EventBroker->Publish(eTriggerTouched3); + +} + +CapturePointTest::~CapturePointTest() +{ + delete m_SystemPipeline; + delete m_World; + delete m_EventBroker; +} + +void CapturePointTest::Tick() +{ + glfwPollEvents(); + + double currentTime = glfwGetTime(); + double dt = currentTime - m_LastTime; + m_LastTime = currentTime; + + // Iterate through systems and update world! + m_SystemPipeline->Update(m_World, dt); + + m_EventBroker->Swap(); + m_EventBroker->Clear(); + +} diff --git a/src/Tests/CapturePointTest.h b/src/Tests/CapturePointTest.h new file mode 100644 index 00000000..c0244802 --- /dev/null +++ b/src/Tests/CapturePointTest.h @@ -0,0 +1,42 @@ +#ifndef CapturePointTest_h__ +#define CapturePointTest_h__ + +#include "Core/ResourceManager.h" +#include "Core/ConfigFile.h" +#include "Core/EventBroker.h" +#include "Core/World.h" +#include "Input/InputProxy.h" +#include "Input/KeyboardInputHandler.h" +#include "Input/MouseInputHandler.h" +#include "Core/EKeyDown.h" +#include "Core/EntityFile.h" +#include "Core/SystemPipeline.h" +#include "PlayerSystem.h" + +#include "Core/EntityFilePreprocessor.h" +#include "Core/EntityFileParser.h" +#include "Core/EntityFileWriter.h" + +#include "Engine/Collision/ETrigger.h" + +class CapturePointTest +{ +public: + CapturePointTest(int runTestNumber); + ~CapturePointTest(); + + void Tick(); + bool TestSucceeded = false; + +private: + double m_LastTime; + ConfigFile* m_Config = nullptr; + EventBroker* m_EventBroker; + World* m_World; + SystemPipeline* m_SystemPipeline; + int m_PlayerID, m_PlayerID2, m_CapturePointID; + int m_RunTestNumber; + +}; + +#endif