Added Collision Tags #233

Merged
direnbharwani merged 10 commits from SP3-2-Physics into main 2022-11-20 22:40:53 +08:00
5 changed files with 167 additions and 9 deletions
Showing only changes of commit a0f4f3b00a - Show all commits

View File

@ -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 "index<space>tag name".
If it fails to follow this, the default tag names will be used.

View File

@ -9,12 +9,12 @@
****************************************************************************************/
#include <SHpch.h>
#include <fstream>
// 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 "index<space>tag 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 'index<space>tag 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 'index<space>tag 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)

View File

@ -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);

View File

@ -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<SHEventReceiverSpec<SHPhysicsSystem>>(this, &SHPhysicsSystem::addPhysicsComponent) };
const ReceiverPtr ADD_COMPONENT_RECEIVER_PTR = std::dynamic_pointer_cast<SHEventReceiver>(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()

View File

@ -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 */