New Event: EShoot. New Components: PrimaryItem,SecondaryItem. New Test: ShootEventTest. Added LeftMouseRelease->Shoot in PlayerSystem
TODO: generalize the test
This commit is contained in:
@@ -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
|
||||
@@ -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<PlayerSystem, Events::TriggerEnter> m_EEnter;
|
||||
bool OnEnter(const Events::TriggerEnter &event);
|
||||
EventRelay<PlayerSystem, Events::TriggerTouch> m_ETouch;
|
||||
bool PlayerSystem::OnTouch(const Events::TriggerTouch &event);
|
||||
EventRelay<PlayerSystem, Events::TriggerLeave> m_ELeave;
|
||||
bool PlayerSystem::OnLeave(const Events::TriggerLeave &event);
|
||||
EventRelay<PlayerSystem, Events::MouseRelease> m_MouseRelease;
|
||||
bool PlayerSystem::OnMouseRelease(const Events::MouseRelease& e);
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -9,4 +9,7 @@
|
||||
<xs:include schemaLocation="Components/AABB.xsd"/>
|
||||
<xs:include schemaLocation="Components/Trigger.xsd"/>
|
||||
<xs:include schemaLocation="Components/Health.xsd"/>
|
||||
<xs:include schemaLocation="Components/PrimaryItem.xsd"/>
|
||||
<xs:include schemaLocation="Components/SecondaryItem.xsd"/>
|
||||
|
||||
</xs:schema>
|
||||
@@ -1,4 +1,5 @@
|
||||
<c:Player>
|
||||
<EquippedItem>0</EquippedItem>
|
||||
<Velocity X="0" Y="0" Z="0"/>
|
||||
<Forward>false</Forward>
|
||||
<Left>false</Left>
|
||||
|
||||
@@ -11,6 +11,7 @@
|
||||
<xs:element name="Left" type="t:bool" minOccurs="0"/>
|
||||
<xs:element name="Back" type="t:bool" minOccurs="0"/>
|
||||
<xs:element name="Right" type="t:bool" minOccurs="0"/>
|
||||
<xs:element name="EquippedItem" type="t:int" minOccurs="0"/>
|
||||
</xs:all>
|
||||
</xs:complexType>
|
||||
</xs:element>
|
||||
|
||||
@@ -0,0 +1,4 @@
|
||||
<c:PrimaryItem>
|
||||
<Ammo>0</Ammo>
|
||||
<CoolDownTimer>0</CoolDownTimer>
|
||||
</c:Player>
|
||||
@@ -0,0 +1,14 @@
|
||||
<?xml version="1.0"?>
|
||||
|
||||
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:t="types">
|
||||
<xs:import schemaLocation="../Types.xsd" namespace="types"/>
|
||||
|
||||
<xs:element name="PrimaryItem">
|
||||
<xs:complexType>
|
||||
<xs:all>
|
||||
<xs:element name="Ammo" type="t:int" minOccurs="0"/>
|
||||
<xs:element name="CoolDownTimer" type="t:double" minOccurs="0"/>
|
||||
</xs:all>
|
||||
</xs:complexType>
|
||||
</xs:element>
|
||||
</xs:schema>
|
||||
@@ -0,0 +1,4 @@
|
||||
<c:SecondaryItem>
|
||||
<Ammo>0</Ammo>
|
||||
<CoolDownTimer>0</CoolDownTimer>
|
||||
</c:Player>
|
||||
@@ -0,0 +1,14 @@
|
||||
<?xml version="1.0"?>
|
||||
|
||||
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:t="types">
|
||||
<xs:import schemaLocation="../Types.xsd" namespace="types"/>
|
||||
|
||||
<xs:element name="SecondaryItem">
|
||||
<xs:complexType>
|
||||
<xs:all>
|
||||
<xs:element name="Ammo" type="t:int" minOccurs="0"/>
|
||||
<xs:element name="CoolDownTimer" type="t:double" minOccurs="0"/>
|
||||
</xs:all>
|
||||
</xs:complexType>
|
||||
</xs:element>
|
||||
</xs:schema>
|
||||
@@ -16,6 +16,8 @@
|
||||
<xs:element ref="c:RaptorCopter" minOccurs="0"/>
|
||||
<xs:element ref="c:Player" minOccurs="0"/>
|
||||
<xs:element ref="c:Health" minOccurs="0"/>
|
||||
<xs:element ref="c:PrimaryItem" minOccurs="0"/>
|
||||
<xs:element ref="c:SecondaryItem" minOccurs="0"/>
|
||||
</xs:all>
|
||||
</xs:complexType>
|
||||
</xs:element>
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
@@ -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
|
||||
Reference in New Issue
Block a user