From 8189e3c00c6cc611c12be08d346773aba2a3c677 Mon Sep 17 00:00:00 2001 From: verysecrethero Date: Wed, 20 Jan 2016 14:59:18 +0100 Subject: [PATCH] Logic in CapturePointSystem is working again. Need to update all Tests! --- include/Engine/Core/ComponentPool.h | 1 + include/Game/Systems/CapturePointSystem.h | 16 +- resources/Schema/Components/CapturePoint.xml | 1 + resources/Schema/Components/CapturePoint.xsd | 19 ++ resources/Schema/Entities/CaptureTestState1 | 158 ++++++++++++++++ src/Engine/Core/ComponentPool.cpp | 5 + src/Game/Systems/CapturePointSystem.cpp | 186 +++++++++---------- src/Tests/CapturePointTest.cpp | 4 +- 8 files changed, 290 insertions(+), 100 deletions(-) create mode 100644 resources/Schema/Entities/CaptureTestState1 diff --git a/include/Engine/Core/ComponentPool.h b/include/Engine/Core/ComponentPool.h index ed81d72e..957b8756 100644 --- a/include/Engine/Core/ComponentPool.h +++ b/include/Engine/Core/ComponentPool.h @@ -61,6 +61,7 @@ public: iterator begin() const; iterator end() const; + size_t size() const; //Dumps information about what the pool memory looks like right now //into an output stream (e.g. file/std::cout, anything that has an operator<<) diff --git a/include/Game/Systems/CapturePointSystem.h b/include/Game/Systems/CapturePointSystem.h index 2cac7d95..542d80d5 100644 --- a/include/Game/Systems/CapturePointSystem.h +++ b/include/Game/Systems/CapturePointSystem.h @@ -28,15 +28,23 @@ private: bool CapturePointSystem::OnTriggerTouch(const Events::TriggerTouch& e); EventRelay m_ETriggerLeave; bool CapturePointSystem::OnTriggerLeave(const Events::TriggerLeave& e); + EventRelay m_ECaptured; + bool CapturePointSystem::OnCaptured(const Events::Captured& e); bool m_WinnerWasFound = false; //need to track these variables for the captureSystem to work as per design! const int m_NotACapturePoint = 999; - int m_Team1NextPossibleCapturePoint = m_NotACapturePoint; - int m_Team2NextPossibleCapturePoint = m_NotACapturePoint; - int m_Team1HomeCapturePoint = m_NotACapturePoint; - int m_Team2HomeCapturePoint = m_NotACapturePoint; + int m_RedTeamNextPossibleCapturePoint = m_NotACapturePoint; + int m_BlueTeamNextPossibleCapturePoint = m_NotACapturePoint; + int m_RedTeamHomeCapturePoint = m_NotACapturePoint; + int m_BlueTeamHomeCapturePoint = m_NotACapturePoint; + int m_NumberOfCapturePoints = 0; + std::map m_CapturePointNumberToEntityIDMap; + + //std::vector + + std::map m_NextPossibleCapturePoint; const double m_CaptureTimeToTakeOver = 15.0; //vectors which will keep track of enter/leave changes diff --git a/resources/Schema/Components/CapturePoint.xml b/resources/Schema/Components/CapturePoint.xml index aa65852e..ba164fd9 100644 --- a/resources/Schema/Components/CapturePoint.xml +++ b/resources/Schema/Components/CapturePoint.xml @@ -2,4 +2,5 @@ 0 0 + \ No newline at end of file diff --git a/resources/Schema/Components/CapturePoint.xsd b/resources/Schema/Components/CapturePoint.xsd index ff1a665d..9e96fca6 100644 --- a/resources/Schema/Components/CapturePoint.xsd +++ b/resources/Schema/Components/CapturePoint.xsd @@ -3,6 +3,18 @@ + + + + + + + + + + + + A Capture Point. Add a Team Component to specify who currently owns it @@ -19,6 +31,13 @@ CapturePointNumber specify an int number for this + + + + Specify if this is a HomePoint for either team + + + diff --git a/resources/Schema/Entities/CaptureTestState1 b/resources/Schema/Entities/CaptureTestState1 new file mode 100644 index 00000000..03b93e81 --- /dev/null +++ b/resources/Schema/Entities/CaptureTestState1 @@ -0,0 +1,158 @@ + + + + + + + + + + + + ../assets/Models/DummyScene.obj + + + + + + + + + 60 + + + + + + + 3 + + + + + + + + + + + + + 3 + + + ../assets/Models/Core/UnitSphere.obj + + + + 3 + + + + + + + + + + + + + -3.9175623281664684 + 1 + + + ../assets/Models/Core/UnitSphere.obj + + + + 2 + + + + + + + + + + + + + -1.8667072838033221 + 2 + + + ../assets/Models/Core/UnitSphere.obj + + + + 2 + + + + + + + + + + + + + 2 + 3 + + + ../assets/Models/Core/UnitSphere.obj + + + + 2 + + + + + + + + + + + + + + ../assets/Models/Core/UnitCube.obj + + + + + 2 + + + + + + + + + + + + + ../assets/Models/Core/UnitCube.obj + + + + + 3 + + + + + + + + + + diff --git a/src/Engine/Core/ComponentPool.cpp b/src/Engine/Core/ComponentPool.cpp index ce24c1f7..b6286c28 100644 --- a/src/Engine/Core/ComponentPool.cpp +++ b/src/Engine/Core/ComponentPool.cpp @@ -72,6 +72,11 @@ ComponentPool::iterator ComponentPool::end() const return iterator(m_ComponentInfo, m_Pool.end(), m_Pool.end()); } +size_t ComponentPool::size() const +{ + return m_Pool.size(); +} + template void ComponentPool::Dump() const { diff --git a/src/Game/Systems/CapturePointSystem.cpp b/src/Game/Systems/CapturePointSystem.cpp index af82e95a..e5141cdf 100644 --- a/src/Game/Systems/CapturePointSystem.cpp +++ b/src/Game/Systems/CapturePointSystem.cpp @@ -14,20 +14,80 @@ CapturePointSystem::CapturePointSystem(EventBroker* eventBroker) //NOTE: needs to run each frame, since we're possibly modifying the captureTimer for the capturePoints by dt void CapturePointSystem::UpdateComponent(World* world, EntityWrapper& entity, ComponentWrapper& capturePoint, double dt) { - bool hasTeamComponent = world->HasComponent(capturePoint.EntityID, "Team"); + const int capturePointNumber = capturePoint["CapturePointNumber"]; + const bool hasTeamComponent = world->HasComponent(capturePoint.EntityID, "Team"); + + //if point doesnt have a teamComponent yet, add one. since: + //what if capture point has no team -> we cant get/use the team enum from it... if (!hasTeamComponent) { world->AttachComponent(capturePoint.EntityID, "Team"); ComponentWrapper& teamComponent = world->GetComponent(capturePoint.EntityID, "Team"); teamComponent["Team"] = (int)teamComponent["Team"].Enum("Spectator"); } ComponentWrapper& teamComponent = world->GetComponent(capturePoint.EntityID, "Team"); - int firstTeamPlayersStandingInside = 0; - int secondTeamPlayersStandingInside = 0; - //what if capture point has no TEAM? -> NO ENUM. - const int redTeam = (int)teamComponent["Team"].Enum("Red");//"team 1" - const int blueTeam = (int)teamComponent["Team"].Enum("Blue");//"team 2" + const int redTeam = (int)teamComponent["Team"].Enum("Red"); + const int blueTeam = (int)teamComponent["Team"].Enum("Blue"); const int spectatorTeam = (int)teamComponent["Team"].Enum("Spectator"); + int homePointForTeam = (int)capturePoint["HomePointForTeam"]; + if (m_NumberOfCapturePoints == 0 && capturePointNumber != 0 && (homePointForTeam == redTeam || homePointForTeam == blueTeam)) { + m_NumberOfCapturePoints = capturePointNumber + 1;//ex 2 -> 0,1,2 = 3 + if (homePointForTeam == redTeam) { + m_RedTeamHomeCapturePoint = capturePointNumber; + m_BlueTeamHomeCapturePoint = 0; + } else { + m_BlueTeamHomeCapturePoint = capturePointNumber; + m_RedTeamHomeCapturePoint = 0; + } + } + + //if we havent received all capturepoints yet, just return + if (m_NumberOfCapturePoints == 0 || m_NumberOfCapturePoints != m_CapturePointNumberToEntityIDMap.size()) { + m_CapturePointNumberToEntityIDMap.insert(std::make_pair(capturePointNumber, capturePoint.EntityID)); + return; + } + + //we have all capturepoints now - process stuff + int ownedBy = teamComponent["Team"]; + int redTeamPlayersStandingInside = 0; + int blueTeamPlayersStandingInside = 0; + if (entity.HasComponent("Model")) { + //Now sets team color to the capturepoint, or white if it is uncaptured. + entity["Model"]["Color"] = ownedBy == blueTeam ? glm::vec4(0, 0.2f, 1, 1) : ownedBy == redTeam ? glm::vec4(1, 0.2f, 0, 1) : glm::vec4(1, 1, 1, 1); + } + + //calculate next possible capturePoint for both teams + m_NextPossibleCapturePoint["Red"] = -1; + m_NextPossibleCapturePoint["Blue"] = -1; + for (size_t i = 0; i < m_NumberOfCapturePoints; i++) + { + ComponentWrapper& capturePointOwnedBy = world->GetComponent(m_CapturePointNumberToEntityIDMap[i], "Team"); + if ((int)capturePointOwnedBy["Team"] == redTeam && m_RedTeamHomeCapturePoint == 0) { + m_NextPossibleCapturePoint["Red"] = i + 1; + } + if ((int)capturePointOwnedBy["Team"] == blueTeam && m_BlueTeamHomeCapturePoint == 0) { + m_NextPossibleCapturePoint["Blue"] = i + 1; + } + } + for (int i = m_NumberOfCapturePoints - 1; i >= 0; i--) + { + ComponentWrapper& capturePointOwnedBy = world->GetComponent(m_CapturePointNumberToEntityIDMap[i], "Team"); + if ((int)capturePointOwnedBy["Team"] == redTeam && m_RedTeamHomeCapturePoint != 0) { + m_NextPossibleCapturePoint["Red"] = i - 1; + } + if ((int)capturePointOwnedBy["Team"] == blueTeam && m_BlueTeamHomeCapturePoint != 0) { + m_NextPossibleCapturePoint["Blue"] = i - 1; + } + } + + //colorize next possible capturepoint + if (m_NextPossibleCapturePoint["Red"] == capturePointNumber) { + entity["Model"]["Color"] = glm::vec4(1, 1, 0, 1); + } + if (m_NextPossibleCapturePoint["Blue"] == capturePointNumber) { + entity["Model"]["Color"] = glm::vec4(0, 1, 1, 1); + } + //check how many players are standing inside and are healthy for (size_t i = m_ETriggerTouchVector.size(); i > 0; i--) { @@ -51,70 +111,40 @@ void CapturePointSystem::UpdateComponent(World* world, EntityWrapper& entity, Co //check team - spectatorNumber = "no team" int teamNumber = world->GetComponent(playerID, "Team")["Team"]; if (teamNumber == redTeam) { - firstTeamPlayersStandingInside++; + redTeamPlayersStandingInside++; } else if (teamNumber == blueTeam) { - secondTeamPlayersStandingInside++; + blueTeamPlayersStandingInside++; } continue; } } - int ownedBy = teamComponent["Team"]; - //Probably want to distinguish the capturepoint depending on team affiliation. - if (entity.HasComponent("Model")) { - //Now sets team color to the capturepoint, or white if it is uncaptured. - entity["Model"]["Color"] = ownedBy == blueTeam ? glm::vec4(0, 0.2f, 1, 1) : - ownedBy == redTeam ? glm::vec4(1, 0.2f, 0, 1) : - glm::vec4(1, 1, 1, 1); - } - //om ej next satt, förvänta sig att en capturepoint med en viss team färg kommer in... - //sätt isåfall next och kör på.. - //gör inget tills man fått den infon - - /*check what capturePoint can be taken over next: - no capturepoint taken yet for at least one of the teams <-> - at the start of the match the system is unaware of what capturePoint is the first one for each team*/ - if (m_Team1NextPossibleCapturePoint == m_NotACapturePoint && (int)teamComponent["Team"] == redTeam) { - m_Team1HomeCapturePoint = capturePoint["CapturePointNumber"];//needed to calculate next m_Team1NextPossibleCapturePoint - if (m_Team1HomeCapturePoint == 0) { - m_Team1NextPossibleCapturePoint = 1; - } else { - m_Team1NextPossibleCapturePoint = (int)capturePoint["CapturePointNumber"] - 1; - } - } else if (m_Team2NextPossibleCapturePoint == m_NotACapturePoint && (int)teamComponent["Team"] == blueTeam) { - m_Team2HomeCapturePoint = capturePoint["CapturePointNumber"];//needed to calculate next m_Team2NextPossibleCapturePoint - if (m_Team2HomeCapturePoint == 0) { - m_Team2NextPossibleCapturePoint = 1; - } else { - m_Team2NextPossibleCapturePoint = (int)capturePoint["CapturePointNumber"] - 1; - } - } - //at least one capturepoint has been taken over - //do nothing, its being handled inside the next code: - //create data to be used in option B //check so this is the next possible capture point for the take-over team and see if only one team is standing inside it double timerDeltaChange = 0.0; int currentTeam = 0; - if (firstTeamPlayersStandingInside > 0 && secondTeamPlayersStandingInside == 0 - && m_Team1NextPossibleCapturePoint == (int)capturePoint["CapturePointNumber"]) - { - timerDeltaChange = firstTeamPlayersStandingInside*dt; + bool canCapture = false; + if (redTeamPlayersStandingInside > 0 && blueTeamPlayersStandingInside == 0) { + timerDeltaChange = redTeamPlayersStandingInside*dt; currentTeam = redTeam; - } else if (firstTeamPlayersStandingInside == 0 && secondTeamPlayersStandingInside > 0 - && m_Team2NextPossibleCapturePoint == (int)capturePoint["CapturePointNumber"]) - { - timerDeltaChange = -secondTeamPlayersStandingInside*dt; + canCapture = m_NextPossibleCapturePoint["Red"] == capturePointNumber; + } + if (redTeamPlayersStandingInside == 0 && blueTeamPlayersStandingInside > 0) { + timerDeltaChange = -blueTeamPlayersStandingInside*dt; currentTeam = blueTeam; + canCapture = m_NextPossibleCapturePoint["Blue"] == capturePointNumber; } - if (firstTeamPlayersStandingInside == 0 && secondTeamPlayersStandingInside == 0) { + if (redTeamPlayersStandingInside == 0 && blueTeamPlayersStandingInside == 0) { //A.nobodys standing inside //do nothing (?) - } else if (currentTeam == blueTeam || currentTeam == redTeam) { - //B. at most one of the teams have players inside (this means datavariable currentTeam is not 0) + } else if (redTeamPlayersStandingInside > 0 && blueTeamPlayersStandingInside > 0) { + //C.both teams have players inside + //do nothing (?) + } else { + //B. at most one of the teams have players inside //if capturePoint is not owned by the take-over team, just modify the CaptureTimer accordingly - if (ownedBy != currentTeam) { + if (ownedBy != currentTeam && canCapture) { if (abs((double)capturePoint["CaptureTimer"]) < 0.001f) { LOG_DEBUG("Point is being captured by team %i", currentTeam); //Remove when we tested sufficiently. } @@ -126,7 +156,7 @@ void CapturePointSystem::UpdateComponent(World* world, EntityWrapper& entity, Co capturePoint["CaptureTimer"] = (double)capturePoint["CaptureTimer"] + timerDeltaChange; } //check if captureTimer > m_CaptureTimeToTakeOver and if so change owner and publish the eCaptured event - if (abs((double)capturePoint["CaptureTimer"]) > abs(m_CaptureTimeToTakeOver)) { + if (abs((double)capturePoint["CaptureTimer"]) > abs(m_CaptureTimeToTakeOver) && canCapture) { teamComponent["Team"] = currentTeam; capturePoint["CaptureTimer"] = 0.0; //publish Captured event @@ -135,53 +165,17 @@ void CapturePointSystem::UpdateComponent(World* world, EntityWrapper& entity, Co e.CapturePointID = capturePoint.EntityID; e.TeamNumberThatCapturedCapturePoint = currentTeam; m_EventBroker->Publish(e); - //modify nextPossibleCapturePoint, depending on, example: if team 1 has "0" as homebase or team 1 has "7" as homebase - - //0 = false 1 = true - bool team1HasTheZeroCapturePoint = m_Team1HomeCapturePoint < m_Team2HomeCapturePoint; - - if (team1HasTheZeroCapturePoint) { - if (currentTeam == redTeam) { - m_Team1NextPossibleCapturePoint++; - } else { - m_Team2NextPossibleCapturePoint--; - } - //adjust flag for other team if their previous point has just been taken - //this depends on what team has what homepoint ("side") - if (m_Team2NextPossibleCapturePoint == m_Team1NextPossibleCapturePoint - 2) { - m_Team2NextPossibleCapturePoint = m_Team2NextPossibleCapturePoint + 1; - } - if (m_Team1NextPossibleCapturePoint == m_Team2NextPossibleCapturePoint + 2) { - m_Team1NextPossibleCapturePoint = m_Team1NextPossibleCapturePoint - 1; - } - } else { - if (currentTeam == redTeam) { - m_Team1NextPossibleCapturePoint--; - } else { - m_Team2NextPossibleCapturePoint++; - } - //adjust flag for other team if their previous point has just been taken - //this depends on what team has what homepoint ("side") - if (m_Team2NextPossibleCapturePoint == m_Team1NextPossibleCapturePoint + 2) { - m_Team2NextPossibleCapturePoint = m_Team2NextPossibleCapturePoint - 1; - } - if (m_Team1NextPossibleCapturePoint == m_Team2NextPossibleCapturePoint - 2) { - m_Team1NextPossibleCapturePoint = m_Team1NextPossibleCapturePoint + 1; - } - } + //NextPossibleCapturePoint will be calculated in the next update... } - } else if (firstTeamPlayersStandingInside > 0 && secondTeamPlayersStandingInside > 0) { - //C.both teams have players inside - //do nothing (?) } //check for possible winCondition = check if the homebase is owned by the other team bool checkForWinner = false; - if ((int)capturePoint["CapturePointNumber"] == m_Team1HomeCapturePoint && (int)teamComponent["Team"] != redTeam) + if (capturePointNumber == m_RedTeamHomeCapturePoint && ownedBy != redTeam) { checkForWinner = true; } - if ((int)capturePoint["CapturePointNumber"] == m_Team2HomeCapturePoint && (int)teamComponent["Team"] != blueTeam) + if (capturePointNumber == m_BlueTeamHomeCapturePoint && ownedBy != blueTeam) { checkForWinner = true; } @@ -190,7 +184,7 @@ void CapturePointSystem::UpdateComponent(World* world, EntityWrapper& entity, Co { //publish Win event Events::Win e; - e.TeamThatWon = teamComponent["Team"]; + e.TeamThatWon = ownedBy; m_EventBroker->Publish(e); m_WinnerWasFound = true; } @@ -216,3 +210,7 @@ bool CapturePointSystem::OnTriggerLeave(const Events::TriggerLeave& e) } return true; } +bool CapturePointSystem::OnCaptured(const Events::Captured& e) +{ + return true; +} diff --git a/src/Tests/CapturePointTest.cpp b/src/Tests/CapturePointTest.cpp index 9ef7f6a6..76921000 100644 --- a/src/Tests/CapturePointTest.cpp +++ b/src/Tests/CapturePointTest.cpp @@ -80,8 +80,8 @@ bool CapturePointTest::CapturePoint_Game_Loop_OneHundredTimes() { Tick(); NumLoops++; if (TestSucceeded) { - success = true; - break; + //success = true; + //break; } loops--; }