From a0f4f3b00a84417bdab9e991f0c3265861c704d4 Mon Sep 17 00:00:00 2001 From: Diren D Bharwani Date: Sun, 20 Nov 2022 15:32:33 +0800 Subject: [PATCH] Added collision tag names file --- Assets/CollisionTags.SHConfig | 20 +++ .../src/Physics/Collision/SHCollisionTags.cpp | 135 +++++++++++++++++- .../src/Physics/Collision/SHCollisionTags.h | 9 +- .../src/Physics/System/SHPhysicsSystem.cpp | 11 ++ .../src/Physics/System/SHPhysicsSystem.h | 1 + 5 files changed, 167 insertions(+), 9 deletions(-) create mode 100644 Assets/CollisionTags.SHConfig diff --git a/Assets/CollisionTags.SHConfig b/Assets/CollisionTags.SHConfig new file mode 100644 index 00000000..18a339dd --- /dev/null +++ b/Assets/CollisionTags.SHConfig @@ -0,0 +1,20 @@ +0 1 +1 2 +2 3 +3 4 +4 5 +5 6 +6 7 +7 8 +8 9 +9 10 +10 11 +11 12 +12 13 +13 14 +14 15 +15 16 + +note: +All collision tags should follow the above format "indextag name". +If it fails to follow this, the default tag names will be used. \ No newline at end of file diff --git a/SHADE_Engine/src/Physics/Collision/SHCollisionTags.cpp b/SHADE_Engine/src/Physics/Collision/SHCollisionTags.cpp index 85072e5e..7275b618 100644 --- a/SHADE_Engine/src/Physics/Collision/SHCollisionTags.cpp +++ b/SHADE_Engine/src/Physics/Collision/SHCollisionTags.cpp @@ -9,12 +9,12 @@ ****************************************************************************************/ #include +#include // Primary Header #include "SHCollisionTags.h" // Project Headers -#include "SHCollisionTags.h" - +#include "Tools/FileIO/SHFileIO.h" #include "Tools/Utilities/SHUtilities.h" namespace SHADE @@ -136,15 +136,140 @@ namespace SHADE void SHCollisionTag::Init(const std::filesystem::path& tagNameFilePath) noexcept { - // Read tag names from given file. + /** + * I HATE FILE IO + * + * Each line in the file should be "indextag name". + * If the line fails to follow this format, use the default tag name (index + 1) + */ + + // Populate tag names with default + for (int i = 0; i < NUM_TAGS; ++i) + tagNames[i] = std::to_string(i + 1); + + std::ifstream collisionTagNamesFile { tagNameFilePath }; + + if (!collisionTagNamesFile.is_open()) + { + SHLOG_ERROR("Failed to open file for Collision Tag Names! Default tag names used!") + return; + } + + std::stringstream ss; + std::string line; + + int linesRead = 0; + while (std::getline(collisionTagNamesFile, line)) + { + // Do not read anything beyond the first 16 lines + if (linesRead >= 16) + break; + + ss << line; + ++linesRead; + + // First element is index. + int tagIndex; + ss >> tagIndex; + + // Next element is name of the tag + std::string tagName; + ss >> tagName; + + // If no tag name read, use default. + if (tagName.empty()) + { + SHLOG_ERROR + ( + "Collision tag file line {} does not match the required format of 'indextag name'. Default tag used for index {}" + , linesRead + 1 + , tagIndex + ) + + // Use default since + tagNames[tagIndex] = std::to_string(tagIndex + 1); + continue; + } + + tagNames[tagIndex] = tagName; + + ss.clear(); + } + + collisionTagNamesFile.close(); } void SHCollisionTag::Exit(const std::filesystem::path& tagNameFilePath) noexcept { - // Write current set of names to file. - // This ensures any names changed is reflected on the file for the next run of the application. + std::ofstream collisionTagNamesFile { tagNameFilePath }; + + if (!collisionTagNamesFile.is_open()) + { + SHLOG_ERROR("Failed to open file for Collision Tag Names! Tag names not saved!") + return; + } + + for (int i = 0; i < NUM_TAGS; ++i) + collisionTagNamesFile << i << " " << tagNames[i] << std::endl; + + collisionTagNamesFile.close(); } + void SHCollisionTag::UpdateTagNamesFromFile(const std::filesystem::path& tagNameFilePath) noexcept + { + std::ifstream collisionTagNamesFile { tagNameFilePath }; + + if (!collisionTagNamesFile.is_open()) + { + SHLOG_ERROR("Failed to open file for Collision Tag Names! Default tag names used!") + return; + } + + std::stringstream ss; + std::string line; + + int linesRead = 0; + while (std::getline(collisionTagNamesFile, line)) + { + // Do not read anything beyond the first 16 lines + if (linesRead >= 16) + break; + + ss << line; + ++linesRead; + + // First element is index. + int tagIndex; + ss >> tagIndex; + + // Next element is name of the tag + std::string tagName; + ss >> tagName; + + // If no tag name read, use default. + if (tagName.empty()) + { + SHLOG_ERROR + ( + "Collision tag file line {} does not match the required format of 'indextag name'. Name left unchanged for index {}" + , linesRead + 1 + , tagIndex + ) + + // Use default since + tagNames[tagIndex] = std::to_string(tagIndex + 1); + continue; + } + + tagNames[tagIndex] = tagName; + + ss.clear(); + } + + collisionTagNamesFile.close(); + } + + void SHCollisionTag::RemoveTagName(int tagIndex) { if (tagIndex < 0 || tagIndex > NUM_TAGS) diff --git a/SHADE_Engine/src/Physics/Collision/SHCollisionTags.h b/SHADE_Engine/src/Physics/Collision/SHCollisionTags.h index e43aae2d..1b709837 100644 --- a/SHADE_Engine/src/Physics/Collision/SHCollisionTags.h +++ b/SHADE_Engine/src/Physics/Collision/SHCollisionTags.h @@ -95,11 +95,12 @@ namespace SHADE /* Function Members */ /*---------------------------------------------------------------------------------*/ - static void Init (const std::filesystem::path& tagNameFilePath) noexcept; - static void Exit (const std::filesystem::path& tagNameFilePath) noexcept; + static void Init (const std::filesystem::path& tagNameFilePath) noexcept; + static void Exit (const std::filesystem::path& tagNameFilePath) noexcept; + static void UpdateTagNamesFromFile(const std::filesystem::path& tagNameFilePath) noexcept; - static void SetTagName (const std::string& newTagName, int tagIndex); - static void RemoveTagName (int tagIndex); + static void SetTagName (const std::string& newTagName, int tagIndex); + static void RemoveTagName (int tagIndex); [[nodiscard]] static const std::string& GetTagName (int tagIndex); diff --git a/SHADE_Engine/src/Physics/System/SHPhysicsSystem.cpp b/SHADE_Engine/src/Physics/System/SHPhysicsSystem.cpp index f3513ffb..c343d506 100644 --- a/SHADE_Engine/src/Physics/System/SHPhysicsSystem.cpp +++ b/SHADE_Engine/src/Physics/System/SHPhysicsSystem.cpp @@ -14,6 +14,7 @@ #include "SHPhysicsSystem.h" // Project Headers +#include "Assets/SHAssetMacros.h" #include "ECS_Base/Managers/SHComponentManager.h" #include "ECS_Base/Managers/SHEntityManager.h" #include "ECS_Base/Managers/SHSystemManager.h" @@ -90,6 +91,11 @@ namespace SHADE void SHPhysicsSystem::Init() { + // Initialise collision tags + std::filesystem::path defaultCollisionTagNameFilePath { ASSET_ROOT }; + defaultCollisionTagNameFilePath.append("CollisionTags.SHConfig"); + SHCollisionTag::Init(defaultCollisionTagNameFilePath); + // Subscribe to component events const std::shared_ptr ADD_COMPONENT_RECEIVER { std::make_shared>(this, &SHPhysicsSystem::addPhysicsComponent) }; const ReceiverPtr ADD_COMPONENT_RECEIVER_PTR = std::dynamic_pointer_cast(ADD_COMPONENT_RECEIVER); @@ -122,6 +128,11 @@ namespace SHADE void SHPhysicsSystem::Exit() { worldState.DestroyWorld(factory); + + // Write collision tag names to file + std::filesystem::path defaultCollisionTagNameFilePath { ASSET_ROOT }; + defaultCollisionTagNameFilePath.append("CollisionTags.SHConfig"); + SHCollisionTag::Exit(defaultCollisionTagNameFilePath); } void SHPhysicsSystem::ForceUpdate() diff --git a/SHADE_Engine/src/Physics/System/SHPhysicsSystem.h b/SHADE_Engine/src/Physics/System/SHPhysicsSystem.h index f92be4cd..89ecb51e 100644 --- a/SHADE_Engine/src/Physics/System/SHPhysicsSystem.h +++ b/SHADE_Engine/src/Physics/System/SHPhysicsSystem.h @@ -69,6 +69,7 @@ namespace SHADE void SetFixedUpdateRate (double fixedUpdateRate) noexcept; void SetWorldSettings (const SHPhysicsWorldState::WorldSettings& settings) noexcept; + void SetCollisionTagFile (const std::filesystem::path& tagNamesFilePath) noexcept; /*---------------------------------------------------------------------------------*/ /* Function Members */