From 31c1c0ac8ddbdc1cb42992b97243ebdb265577fa Mon Sep 17 00:00:00 2001 From: verysecrethero Date: Fri, 8 Jan 2016 16:19:18 +0100 Subject: [PATCH] New Event: EShoot. New Components: PrimaryItem,SecondaryItem. New Test: ShootEventTest. Added LeftMouseRelease->Shoot in PlayerSystem TODO: generalize the test --- include/Engine/Core/EShoot.h | 22 ++++ include/Game/PlayerSystem.h | 7 ++ resources/Schema/Components.xsd | 3 + resources/Schema/Components/Player.xml | 1 + resources/Schema/Components/Player.xsd | 1 + resources/Schema/Components/PrimaryItem.xml | 4 + resources/Schema/Components/PrimaryItem.xsd | 14 +++ resources/Schema/Components/SecondaryItem.xml | 4 + resources/Schema/Components/SecondaryItem.xsd | 14 +++ resources/Schema/Types/Entity.xsd | 2 + src/Game/PlayerSystem.cpp | 43 +++++++ src/Tests/ShootEventTest.cpp | 108 ++++++++++++++++++ src/Tests/ShootEventTest.h | 43 +++++++ 13 files changed, 266 insertions(+) create mode 100644 include/Engine/Core/EShoot.h create mode 100644 resources/Schema/Components/PrimaryItem.xml create mode 100644 resources/Schema/Components/PrimaryItem.xsd create mode 100644 resources/Schema/Components/SecondaryItem.xml create mode 100644 resources/Schema/Components/SecondaryItem.xsd create mode 100644 src/Tests/ShootEventTest.cpp create mode 100644 src/Tests/ShootEventTest.h diff --git a/include/Engine/Core/EShoot.h b/include/Engine/Core/EShoot.h new file mode 100644 index 00000000..d9b9e20b --- /dev/null +++ b/include/Engine/Core/EShoot.h @@ -0,0 +1,22 @@ +#ifndef EShoot_h__ +#define EShoot_h__ + +#include "EventBroker.h" +#include "../Core/Entity.h" +#include "Engine/GLM.h" + +namespace Events +{ + +struct Shoot : Event +{ + //shotgun etc has different amounts of damage probably (a sniper shot might one-shot) + //also different weapons will have different spread + std::string weaponType; + //currentAimingPoint must be sent, in case the camera is moved while the event is being processed + glm::vec2 currentAimingPoint; +}; + +} + +#endif \ No newline at end of file diff --git a/include/Game/PlayerSystem.h b/include/Game/PlayerSystem.h index 577fbbb0..87dc6f5d 100644 --- a/include/Game/PlayerSystem.h +++ b/include/Game/PlayerSystem.h @@ -7,6 +7,8 @@ #include "Common.h" #include "Core/System.h" #include "Collision/ETrigger.h" +#include "Core\EMouseRelease.h" +#include "Core\EShoot.h" class PlayerSystem : public PureSystem { @@ -17,17 +19,22 @@ public: EVENT_SUBSCRIBE_MEMBER(m_ETouch, &PlayerSystem::OnTouch); EVENT_SUBSCRIBE_MEMBER(m_EEnter, &PlayerSystem::OnEnter); EVENT_SUBSCRIBE_MEMBER(m_ELeave, &PlayerSystem::OnLeave); + EVENT_SUBSCRIBE_MEMBER(m_MouseRelease, &PlayerSystem::OnMouseRelease); } virtual void UpdateComponent(World* world, ComponentWrapper& player, double dt) override; private: float m_Speed = 5; + bool leftMouseWasReleased = false; + glm::vec2 aimingCoordinates; EventRelay m_EEnter; bool OnEnter(const Events::TriggerEnter &event); EventRelay m_ETouch; bool PlayerSystem::OnTouch(const Events::TriggerTouch &event); EventRelay m_ELeave; bool PlayerSystem::OnLeave(const Events::TriggerLeave &event); + EventRelay m_MouseRelease; + bool PlayerSystem::OnMouseRelease(const Events::MouseRelease& e); }; #endif \ No newline at end of file diff --git a/resources/Schema/Components.xsd b/resources/Schema/Components.xsd index 7fcdd565..33714638 100644 --- a/resources/Schema/Components.xsd +++ b/resources/Schema/Components.xsd @@ -9,4 +9,7 @@ + + + \ No newline at end of file diff --git a/resources/Schema/Components/Player.xml b/resources/Schema/Components/Player.xml index 190f2ed0..cd3d1620 100644 --- a/resources/Schema/Components/Player.xml +++ b/resources/Schema/Components/Player.xml @@ -1,4 +1,5 @@ + 0 false false diff --git a/resources/Schema/Components/Player.xsd b/resources/Schema/Components/Player.xsd index 76a6a8fb..fcf07879 100644 --- a/resources/Schema/Components/Player.xsd +++ b/resources/Schema/Components/Player.xsd @@ -11,6 +11,7 @@ + diff --git a/resources/Schema/Components/PrimaryItem.xml b/resources/Schema/Components/PrimaryItem.xml new file mode 100644 index 00000000..540a1518 --- /dev/null +++ b/resources/Schema/Components/PrimaryItem.xml @@ -0,0 +1,4 @@ + + 0 + 0 + \ No newline at end of file diff --git a/resources/Schema/Components/PrimaryItem.xsd b/resources/Schema/Components/PrimaryItem.xsd new file mode 100644 index 00000000..193b9213 --- /dev/null +++ b/resources/Schema/Components/PrimaryItem.xsd @@ -0,0 +1,14 @@ + + + + + + + + + + + + + + \ No newline at end of file diff --git a/resources/Schema/Components/SecondaryItem.xml b/resources/Schema/Components/SecondaryItem.xml new file mode 100644 index 00000000..0fae1402 --- /dev/null +++ b/resources/Schema/Components/SecondaryItem.xml @@ -0,0 +1,4 @@ + + 0 + 0 + \ No newline at end of file diff --git a/resources/Schema/Components/SecondaryItem.xsd b/resources/Schema/Components/SecondaryItem.xsd new file mode 100644 index 00000000..44e23611 --- /dev/null +++ b/resources/Schema/Components/SecondaryItem.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 6562f3ed..4b67276a 100644 --- a/resources/Schema/Types/Entity.xsd +++ b/resources/Schema/Types/Entity.xsd @@ -16,6 +16,8 @@ + + diff --git a/src/Game/PlayerSystem.cpp b/src/Game/PlayerSystem.cpp index 17b9a7f5..f5819ea1 100644 --- a/src/Game/PlayerSystem.cpp +++ b/src/Game/PlayerSystem.cpp @@ -21,6 +21,38 @@ void PlayerSystem::UpdateComponent(World * world, ComponentWrapper & player, dou ComponentWrapper& transform = world->GetComponent(player.EntityID, "Transform"); (glm::vec3&)transform["Position"] += (glm::vec3)player["Velocity"]; } + + //do shootEvent: if left mouse was released, and ammo/weaponcooldown/playeralive/shootingcooldown are ok + if (leftMouseWasReleased) { + leftMouseWasReleased = false; + //get the health component linked to the playerId + double currentHealth = (double)world->GetComponent(player.EntityID, "Health")["Health"]; + int currentAmmo = 0; + double currentCoolDownTimer = 0.0f; + + if ((int)player["EquippedItem"] == 1) { + ComponentWrapper& currentItem = world->GetComponent(player.EntityID, "PrimaryItem"); + currentAmmo = currentItem["Ammo"]; + //subtract 1 ammo - the ItemSystem will probably listen to eShoot and handle the CoolDownTimer + currentItem["Ammo"] = (int)currentItem["Ammo"] -1; + int test = (int)currentItem["Ammo"]; + currentCoolDownTimer = (double)world->GetComponent(player.EntityID, "PrimaryItem")["CoolDownTimer"]; + } + if ((int)player["EquippedItem"] == 2) { + ComponentWrapper& currentItem = world->GetComponent(player.EntityID, "SecondaryItem"); + currentAmmo = currentItem["Ammo"]; + //subtract 1 ammo - the ItemSystem will probably listen to eShoot and handle the CoolDownTimer + currentItem["Ammo"] = (int)currentItem["Ammo"] - 1; + currentCoolDownTimer = (double)world->GetComponent(player.EntityID, "SecondaryItem")["CoolDownTimer"]; + } + if (currentHealth > 0.0f && currentAmmo > 0 && currentCoolDownTimer < 0.001f) { + //create and publish the shoot event + Events::Shoot eShoot; + eShoot.currentAimingPoint = aimingCoordinates; + eShoot.weaponType = (int)player["EquippedItem"]; + m_EventBroker->Publish(eShoot); + } + } } bool PlayerSystem::OnTouch(const Events::TriggerTouch &event) @@ -39,4 +71,15 @@ bool PlayerSystem::OnLeave(const Events::TriggerLeave &event) { LOG_INFO("Player entity %i left widget (entity %i).", event.Entity, event.Trigger); return false; +} + +bool PlayerSystem::OnMouseRelease(const Events::MouseRelease& e) +{ + //kolla ammoleft, cooldowntimer shooting + //kolla om left mouse varit nere + if (e.Button != GLFW_MOUSE_BUTTON_LEFT) + return false; + aimingCoordinates = glm::vec2(e.X, e.Y); + leftMouseWasReleased = true; + return true; } \ No newline at end of file diff --git a/src/Tests/ShootEventTest.cpp b/src/Tests/ShootEventTest.cpp new file mode 100644 index 00000000..481772c1 --- /dev/null +++ b/src/Tests/ShootEventTest.cpp @@ -0,0 +1,108 @@ +#include +using boost::unit_test_framework::test_suite; +using boost::unit_test_framework::test_case; + +#include "ShootEventTest.h" +#include "Core\EPlayerDamage.h"; +#include "Core\EPlayerHealthPickup.h"; +#include "Core\EPlayerDeath.h"; +#include "Game/HealthSystem.h" + +BOOST_AUTO_TEST_SUITE(ShootEventTestSuite) + +//AShootEventTest != ShootEventTest -> else it confuses names! +BOOST_AUTO_TEST_CASE(AShootEventTest) +{ + ShootEventTest game; + //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() + +ShootEventTest::ShootEventTest() +{ + ResourceManager::RegisterType("ConfigFile"); + ResourceManager::RegisterType("EntityXMLFile"); + + m_Config = ResourceManager::Load("Config.ini"); + 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(); + std::string mapToLoad = m_Config->Get("Debug.LoadMap", ""); + if (!mapToLoad.empty()) { + ResourceManager::Load(mapToLoad)->PopulateWorld(m_World); + } + + // Create system pipeline + m_SystemPipeline = new SystemPipeline(m_EventBroker); + m_SystemPipeline->AddSystem(0); + m_SystemPipeline->AddSystem(0); + + //The Test + //create entity which has transorm,player,model,health in it. i.e. is a player + 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"); + playersID = playerID; + //attach 2x weaps + ComponentWrapper& pItem = m_World->AttachComponent(playerID, "PrimaryItem"); + ComponentWrapper& sItem= m_World->AttachComponent(playerID, "SecondaryItem"); + //set currentweap + player["EquippedItem"] = 1; + //set ammo set cooldown + pItem["Ammo"] = 100; + pItem["CoolDownTimer"] = 0.0f; + + //trigger event leftmousedown + Events::MouseRelease eMouseRelease; + eMouseRelease.Button = GLFW_MOUSE_BUTTON_LEFT; + eMouseRelease.X = 1.0f; + eMouseRelease.Y = 1.0f; + m_EventBroker->Publish(eMouseRelease); + +} + +ShootEventTest::~ShootEventTest() +{ + delete m_SystemPipeline; + delete m_World; + delete m_EventBroker; +} + +void ShootEventTest::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(); + + //if ammocount reaches 99 we know the test has succeeded, i.e. a shot has been fired + int currentAmmo = (int)m_World->GetComponent(playersID, "PrimaryItem")["Ammo"]; + if (currentAmmo ==99) + TestSucceeded = true; +} diff --git a/src/Tests/ShootEventTest.h b/src/Tests/ShootEventTest.h new file mode 100644 index 00000000..49206ceb --- /dev/null +++ b/src/Tests/ShootEventTest.h @@ -0,0 +1,43 @@ +#ifndef ShootEventTest_h__ +#define ShootEventTest_h__ + +#include "Core/ResourceManager.h" +#include "Core/ConfigFile.h" +#include "Core/EventBroker.h" +#include "Rendering/Renderer.h" +#include "Core/InputManager.h" +#include "GUI/Frame.h" +#include "Core/World.h" +#include "Rendering/RenderQueueFactory.h" +#include "Input/InputProxy.h" +#include "Input/KeyboardInputHandler.h" +#include "Input/MouseInputHandler.h" +#include "Core/EKeyDown.h" +#include "Core/EntityXMLFile.h" +#include "Core/SystemPipeline.h" +#include "RaptorCopterSystem.h" +#include "PlayerSystem.h" +#include "Editor/EditorSystem.h" + +#include "Core\EMouseRelease.h" +#include "Core\EShoot.h" + +class ShootEventTest +{ +public: + ShootEventTest(); + ~ShootEventTest(); + + void Tick(); + bool TestSucceeded = false; + +private: + double m_LastTime; + ConfigFile* m_Config = nullptr; + EventBroker* m_EventBroker; + World* m_World; + SystemPipeline* m_SystemPipeline; + int playersID; +}; + +#endif