New Event: EShoot. New Components: PrimaryItem,SecondaryItem. New Test: ShootEventTest. Added LeftMouseRelease->Shoot in PlayerSystem

TODO: generalize the test
This commit is contained in:
verysecrethero
2016-01-08 16:19:18 +01:00
parent 704e8825c4
commit 184af95507
13 changed files with 266 additions and 0 deletions
+43
View File
@@ -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;
}
+108
View File
@@ -0,0 +1,108 @@
#include <boost/test/unit_test.hpp>
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>("ConfigFile");
ResourceManager::RegisterType<EntityXMLFile>("EntityXMLFile");
m_Config = ResourceManager::Load<ConfigFile>("Config.ini");
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();
std::string mapToLoad = m_Config->Get<std::string>("Debug.LoadMap", "");
if (!mapToLoad.empty()) {
ResourceManager::Load<EntityXMLFile>(mapToLoad)->PopulateWorld(m_World);
}
// Create system pipeline
m_SystemPipeline = new SystemPipeline(m_EventBroker);
m_SystemPipeline->AddSystem<PlayerSystem>(0);
m_SystemPipeline->AddSystem<HealthSystem>(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;
}
+43
View File
@@ -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