From 0e8b97dbbade6bb39c6f2b8af6c1cfc43ce67cde Mon Sep 17 00:00:00 2001 From: sippeangelo Date: Thu, 3 Mar 2016 01:35:32 +0100 Subject: [PATCH] Made AmmunitionHUD into TextFieldReader which reads any component field on a parent and updates a Text component with the value! --- include/Engine/Core/EntityWrapper.h | 1 + include/Game/Systems/AmmunitionHUDSystem.h | 17 ---- include/Game/Systems/TextFieldReader.h | 19 +++++ resources/Schema/Components.xsd | 2 +- resources/Schema/Components/AmmunitionHUD.xml | 3 - resources/Schema/Components/AmmunitionHUD.xsd | 10 --- .../Schema/Components/TextFieldReader.xml | 6 ++ .../Schema/Components/TextFieldReader.xsd | 21 +++++ .../Schema/Entities/DefenderWeaponView.xml | 15 +++- resources/Schema/Entities/Player.xml | 82 +++++++++---------- .../Schema/Entities/SidearmWeaponView.xml | 13 ++- resources/Schema/Types/Entity.xsd | 2 +- src/Engine/Core/EntityWrapper.cpp | 12 +++ src/Game/Game.cpp | 6 +- src/Game/Systems/AmmunitionHUDSystem.cpp | 36 -------- src/Game/Systems/TextFieldReader.cpp | 46 +++++++++++ 16 files changed, 174 insertions(+), 117 deletions(-) delete mode 100644 include/Game/Systems/AmmunitionHUDSystem.h create mode 100644 include/Game/Systems/TextFieldReader.h delete mode 100644 resources/Schema/Components/AmmunitionHUD.xml delete mode 100644 resources/Schema/Components/AmmunitionHUD.xsd create mode 100644 resources/Schema/Components/TextFieldReader.xml create mode 100644 resources/Schema/Components/TextFieldReader.xsd delete mode 100644 src/Game/Systems/AmmunitionHUDSystem.cpp create mode 100644 src/Game/Systems/TextFieldReader.cpp diff --git a/include/Engine/Core/EntityWrapper.h b/include/Engine/Core/EntityWrapper.h index 8ece8e59..12029720 100644 --- a/include/Engine/Core/EntityWrapper.h +++ b/include/Engine/Core/EntityWrapper.h @@ -27,6 +27,7 @@ struct EntityWrapper bool HasComponent(const std::string& componentType); void AttachComponent(const char* componentName); EntityWrapper Parent(); + EntityWrapper FirstParentByName(const std::string& parentEntityName); EntityWrapper FirstChildByName(const std::string& name); EntityWrapper FirstParentWithComponent(const std::string& componentType); EntityWrapper Clone(EntityWrapper parent = EntityWrapper::Invalid); diff --git a/include/Game/Systems/AmmunitionHUDSystem.h b/include/Game/Systems/AmmunitionHUDSystem.h deleted file mode 100644 index b22a85b5..00000000 --- a/include/Game/Systems/AmmunitionHUDSystem.h +++ /dev/null @@ -1,17 +0,0 @@ -#ifndef AmmunitionHUDSystem_h__ -#define AmmunitionHUDSystem_h__ - -#include "../../Engine/Core/System.h" -#include "../../Engine/GLM.h" - -class AmmunitionHUDSystem : public ImpureSystem -{ -public: - AmmunitionHUDSystem(SystemParams params) - : System(params) - { } - - virtual void Update(double dt) override; -}; - -#endif \ No newline at end of file diff --git a/include/Game/Systems/TextFieldReader.h b/include/Game/Systems/TextFieldReader.h new file mode 100644 index 00000000..1ea8e966 --- /dev/null +++ b/include/Game/Systems/TextFieldReader.h @@ -0,0 +1,19 @@ +#ifndef AmmunitionHUDSystem_h__ +#define AmmunitionHUDSystem_h__ + +#include +#include "../../Engine/Core/System.h" +#include "../../Engine/GLM.h" + +class TextFieldReader : public PureSystem +{ +public: + TextFieldReader(SystemParams params) + : System(params) + , PureSystem("TextFieldReader") + { } + + virtual void UpdateComponent(EntityWrapper& entity, ComponentWrapper& cTextFieldReader, double dt) override; +}; + +#endif \ No newline at end of file diff --git a/resources/Schema/Components.xsd b/resources/Schema/Components.xsd index 711f4999..ee8bc31d 100644 --- a/resources/Schema/Components.xsd +++ b/resources/Schema/Components.xsd @@ -45,7 +45,7 @@ - + diff --git a/resources/Schema/Components/AmmunitionHUD.xml b/resources/Schema/Components/AmmunitionHUD.xml deleted file mode 100644 index 63b86150..00000000 --- a/resources/Schema/Components/AmmunitionHUD.xml +++ /dev/null @@ -1,3 +0,0 @@ - - - \ No newline at end of file diff --git a/resources/Schema/Components/AmmunitionHUD.xsd b/resources/Schema/Components/AmmunitionHUD.xsd deleted file mode 100644 index 1a48d8d1..00000000 --- a/resources/Schema/Components/AmmunitionHUD.xsd +++ /dev/null @@ -1,10 +0,0 @@ - - - - - - - Hud element for tracking ammunition from parent with AssaultWeapon component. Child with the name "MagazineAmmo" tracks clip ammunition. Child with the name "Ammo" tracks ammo. - - - \ No newline at end of file diff --git a/resources/Schema/Components/TextFieldReader.xml b/resources/Schema/Components/TextFieldReader.xml new file mode 100644 index 00000000..52430804 --- /dev/null +++ b/resources/Schema/Components/TextFieldReader.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/resources/Schema/Components/TextFieldReader.xsd b/resources/Schema/Components/TextFieldReader.xsd new file mode 100644 index 00000000..7c77890d --- /dev/null +++ b/resources/Schema/Components/TextFieldReader.xsd @@ -0,0 +1,21 @@ + + + + + + Reads a value from a specific field of a compoent of parent entity and writes it to the Text component on this entity. + + + + The name of the parent entity to read the component field from. Leave empty to read from this entity. + + + The component type to read the field value from. + + + The field name to read the value from. + + + + + \ No newline at end of file diff --git a/resources/Schema/Entities/DefenderWeaponView.xml b/resources/Schema/Entities/DefenderWeaponView.xml index b65194f9..0886ac67 100755 --- a/resources/Schema/Entities/DefenderWeaponView.xml +++ b/resources/Schema/Entities/DefenderWeaponView.xml @@ -33,7 +33,6 @@ - @@ -61,8 +60,13 @@ + + Player + DefenderWeapon + MagazineAmmo + - 32 + 0 Fonts/DroidSans.ttf,64 @@ -74,8 +78,13 @@ + + Player + DefenderWeapon + Ammo + - 360 + 0 Fonts/DroidSans.ttf,64 diff --git a/resources/Schema/Entities/Player.xml b/resources/Schema/Entities/Player.xml index 5c5efc07..e0230582 100644 --- a/resources/Schema/Entities/Player.xml +++ b/resources/Schema/Entities/Player.xml @@ -7,9 +7,7 @@ - - 102.85760837900634 - + @@ -26,7 +24,7 @@ - + @@ -66,8 +64,8 @@ Textures/Weapons/Crosshair/SmallThickHoleDot.png - false + false @@ -90,42 +88,22 @@ - - - - 1 - - - - 100/100 - Fonts/DroidSans.ttf,64 - - - - - - - - - - - 1 + Textures/HealthHUD3.png - - + @@ -142,8 +120,8 @@ Textures/Core/White.png - false + false @@ -160,8 +138,8 @@ Textures/Core/White.png - false + false @@ -178,8 +156,8 @@ Textures/Core/White.png - false + false @@ -193,6 +171,26 @@ + + + + 1 + + + + 100/100 + Fonts/DroidSans.ttf,64 + + + + + + + + + + + @@ -229,8 +227,8 @@ - + @@ -263,8 +261,8 @@ - + @@ -298,8 +296,8 @@ - + @@ -332,8 +330,8 @@ - + @@ -365,8 +363,8 @@ - + @@ -442,8 +440,8 @@ - + @@ -490,9 +488,8 @@ R_Arm_Weapon_Joint - AssaultWeapon - SidearmWeapon + Schema/Entities/SidearmWeaponView.xml @@ -575,7 +572,10 @@ Schema/Entities/SidearmWeaponWorld.xml - + + + + @@ -617,8 +617,8 @@ - + @@ -627,8 +627,8 @@ Textures/Icons/Arrow.png - false + false diff --git a/resources/Schema/Entities/SidearmWeaponView.xml b/resources/Schema/Entities/SidearmWeaponView.xml index f01ffdf7..d3dcda66 100644 --- a/resources/Schema/Entities/SidearmWeaponView.xml +++ b/resources/Schema/Entities/SidearmWeaponView.xml @@ -24,7 +24,11 @@ - + + + + + @@ -52,6 +56,11 @@ + + Player + SidearmWeapon + MagazineAmmo + 16 Fonts/DroidSans.ttf,64 @@ -73,8 +82,8 @@ - + diff --git a/resources/Schema/Types/Entity.xsd b/resources/Schema/Types/Entity.xsd index f9870a51..b4ceb6d2 100644 --- a/resources/Schema/Types/Entity.xsd +++ b/resources/Schema/Types/Entity.xsd @@ -43,7 +43,7 @@ - + diff --git a/src/Engine/Core/EntityWrapper.cpp b/src/Engine/Core/EntityWrapper.cpp index b3bef55a..3c217353 100644 --- a/src/Engine/Core/EntityWrapper.cpp +++ b/src/Engine/Core/EntityWrapper.cpp @@ -34,6 +34,18 @@ EntityWrapper EntityWrapper::Parent() } } +EntityWrapper EntityWrapper::FirstParentByName(const std::string& parentEntityName) +{ + EntityWrapper entity = *this; + while (entity.Parent().Valid()) { + entity = entity.Parent(); + if (entity.Name() == parentEntityName) { + return entity; + } + } + return EntityWrapper::Invalid; +} + EntityWrapper EntityWrapper::FirstChildByName(const std::string& name) { return firstChildByNameRecursive(name, this->ID); diff --git a/src/Game/Game.cpp b/src/Game/Game.cpp index 7607efec..10107ab1 100644 --- a/src/Game/Game.cpp +++ b/src/Game/Game.cpp @@ -27,7 +27,7 @@ #include "../Engine/Core/UniformScaleSystem.h" #include "Rendering/AnimationSystem.h" #include "Network/MultiplayerSnapshotFilter.h" -#include "Game/Systems/AmmunitionHUDSystem.h" +#include "Game/Systems/TextFieldReader.h" #include "Game/Systems/CapturePointArrowHUDSystem.h" #include "Game/Systems/KillFeedSystem.h" #include "Game/Systems/BoostSystem.h" @@ -136,7 +136,7 @@ Game::Game(int argc, char* argv[]) m_SystemPipeline->AddSystem(updateOrderLevel); m_SystemPipeline->AddSystem(updateOrderLevel); m_SystemPipeline->AddSystem(updateOrderLevel); - m_SystemPipeline->AddSystem(updateOrderLevel); + m_SystemPipeline->AddSystem(updateOrderLevel); m_SystemPipeline->AddSystem(updateOrderLevel); m_SystemPipeline->AddSystem(updateOrderLevel); m_SystemPipeline->AddSystem(updateOrderLevel); @@ -145,7 +145,7 @@ Game::Game(int argc, char* argv[]) m_SystemPipeline->AddSystem(updateOrderLevel); m_SystemPipeline->AddSystem(updateOrderLevel); m_SystemPipeline->AddSystem(updateOrderLevel); - m_SystemPipeline->AddSystem(updateOrderLevel); + m_SystemPipeline->AddSystem(updateOrderLevel); m_SystemPipeline->AddSystem(updateOrderLevel); m_SystemPipeline->AddSystem(updateOrderLevel); m_SystemPipeline->AddSystem(updateOrderLevel, m_Renderer); diff --git a/src/Game/Systems/AmmunitionHUDSystem.cpp b/src/Game/Systems/AmmunitionHUDSystem.cpp deleted file mode 100644 index c9d87072..00000000 --- a/src/Game/Systems/AmmunitionHUDSystem.cpp +++ /dev/null @@ -1,36 +0,0 @@ -#include "Game/Systems/AmmunitionHUDSystem.h" - -void AmmunitionHUDSystem::Update(double dt) -{ - //Hud element for tracking ammunition from parent with AssaultWeapon component.Child with the name "MagazineAmmo" tracks clip ammunition.Child with the name "Ammo" tracks ammo. - - auto ammunitionHUDs = m_World->GetComponents("AmmunitionHUD"); - if (ammunitionHUDs == nullptr) { - return; - } - - for (auto& ammunitionHUDComponent : *ammunitionHUDs) { - EntityWrapper entity = EntityWrapper(m_World, ammunitionHUDComponent.EntityID); - - EntityWrapper playerEntity = entity.FirstParentWithComponent("AssaultWeapon"); - - if (!playerEntity.Valid()) { - return; - } - - - EntityWrapper magazineAmmo = entity.FirstChildByName("MagazineAmmo"); - if(magazineAmmo.Valid()) { - if(magazineAmmo.HasComponent("Text")) { - (std::string&)magazineAmmo["Text"]["Content"] = std::to_string((int)playerEntity["AssaultWeapon"]["MagazineAmmo"]); - } - } - - EntityWrapper ammo = entity.FirstChildByName("Ammo"); - if (ammo.Valid()) { - if (ammo.HasComponent("Text")) { - (std::string&)ammo["Text"]["Content"] = std::to_string((int)playerEntity["AssaultWeapon"]["Ammo"]); - } - } - } -} diff --git a/src/Game/Systems/TextFieldReader.cpp b/src/Game/Systems/TextFieldReader.cpp new file mode 100644 index 00000000..8712a61f --- /dev/null +++ b/src/Game/Systems/TextFieldReader.cpp @@ -0,0 +1,46 @@ +#include "Game/Systems/TextFieldReader.h" + +void TextFieldReader::UpdateComponent(EntityWrapper& entity, ComponentWrapper& cAmmunitionHUD, double dt) +{ + if (!entity.HasComponent("Text")) { + return; + } + + // Find the entity to read from + const std::string& parentEntityName = cAmmunitionHUD["ParentEntityName"]; + EntityWrapper readEntity = entity; + if (!parentEntityName.empty()) { + readEntity = entity.FirstParentByName(parentEntityName); + if (!readEntity.Valid()) { + return; + } + } + + // Find the component to read from + const std::string& componentType = cAmmunitionHUD["ComponentType"]; + if (componentType.empty() || !readEntity.HasComponent(componentType)) { + return; + } + ComponentWrapper component = readEntity[componentType]; + + // Find the field to read from + const std::string& fieldName = cAmmunitionHUD["Field"]; + if (fieldName.empty() || component.Info.Fields.count(fieldName) == 0) { + return; + } + const ComponentInfo::Field_t& field = component.Info.Fields.at(fieldName); + + std::string& text = entity["Text"]["Content"]; + + if (field.Type == "int") { + text = boost::lexical_cast((const int&)component[fieldName]); + } else if (field.Type == "float") { + text = boost::lexical_cast((const float&)component[fieldName]); + } else if (field.Type == "double") { + text = boost::lexical_cast((const double&)component[fieldName]); + } else if (field.Type == "bool") { + text = boost::lexical_cast((const bool&)component[fieldName]); + } else if (field.Type == "string") { + text = (const std::string&)component[fieldName]; + } +}