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
This commit is contained in:
@@ -0,0 +1,91 @@
|
||||
#include "CapturePointSystem.h"
|
||||
#include <algorithm>
|
||||
|
||||
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;
|
||||
}
|
||||
@@ -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<CollisionSystem>(updateOrderLevel);
|
||||
m_SystemPipeline->AddSystem<TriggerSystem>(updateOrderLevel);
|
||||
m_SystemPipeline->AddSystem<CapturePointSystem>(updateOrderLevel);
|
||||
|
||||
// Invoke network
|
||||
if (m_Config->Get<bool>("Networking.StartNetwork", false)) {
|
||||
|
||||
@@ -0,0 +1,130 @@
|
||||
#include <boost/test/unit_test.hpp>
|
||||
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>("ConfigFile");
|
||||
ResourceManager::RegisterType<EntityFile>("EntityFile");
|
||||
|
||||
m_Config = ResourceManager::Load<ConfigFile>("Config.ini");
|
||||
std::string mapToLoad = m_Config->Get<std::string>("Debug.LoadMap", "");
|
||||
LOG_LEVEL = static_cast<_LOG_LEVEL>(m_Config->Get<int>("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<PlayerSystem>(0);
|
||||
m_SystemPipeline->AddSystem<HealthSystem>(0);
|
||||
m_SystemPipeline->AddSystem<CollisionSystem>(1);
|
||||
m_SystemPipeline->AddSystem<TriggerSystem>(1);
|
||||
m_SystemPipeline->AddSystem<CapturePointSystem>(1);
|
||||
|
||||
if (!mapToLoad.empty()) {
|
||||
auto file = ResourceManager::Load<EntityFile>(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();
|
||||
|
||||
}
|
||||
@@ -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
|
||||
Reference in New Issue
Block a user