Сделал World потокобезопасным

This commit is contained in:
Jiga228
2026-06-28 20:29:19 +07:00
parent 8e09b93f25
commit c95aa557c2
2 changed files with 24 additions and 15 deletions
+11 -8
View File
@@ -8,31 +8,32 @@ World::World(GameInstance& game_instance) : game_instance_(game_instance)
World::~World() World::~World()
{ {
for (auto& i : actors_map) for (auto& i : actors_map_)
i.second->OnDestroy(); i.second->OnDestroy();
actors_map.clear(); actors_map_.clear();
} }
void World::BeginPlay() void World::BeginPlay()
{ {
for (auto& i : actors_map) for (auto& i : actors_map_)
i.second->BeginPlay(); i.second->BeginPlay();
} }
void World::Tick(double delta_time) void World::Tick(double delta_time)
{ {
for (auto& i : actors_map) for (auto& i : actors_map_)
i.second->Tick(delta_time); i.second->Tick(delta_time);
} }
std::shared_ptr<SaveMap> World::save() std::shared_ptr<SaveMap> World::save()
{ {
std::scoped_lock lock(m_actors_map_);
std::shared_ptr<SaveMap> save = std::make_shared<SaveMap>("World"); std::shared_ptr<SaveMap> save = std::make_shared<SaveMap>("World");
std::vector<std::string> ActorsIDs; std::vector<std::string> ActorsIDs;
ActorsIDs.reserve(actors_map.size()); ActorsIDs.reserve(actors_map_.size());
std::list<std::shared_ptr<ISave>> actors; std::list<std::shared_ptr<ISave>> actors;
for (auto[it, flag] : actors_map) for (auto[it, flag] : actors_map_)
{ {
ActorsIDs.push_back(it); ActorsIDs.push_back(it);
actors.push_back(flag); actors.push_back(flag);
@@ -45,13 +46,14 @@ std::shared_ptr<SaveMap> World::save()
void World::load(std::shared_ptr<SaveMap> save) void World::load(std::shared_ptr<SaveMap> save)
{ {
std::scoped_lock lock(m_actors_map_);
std::vector<std::string> actors_IDs = save->GetVectorString("ActorsIDs"); std::vector<std::string> actors_IDs = save->GetVectorString("ActorsIDs");
std::list<std::shared_ptr<ISave>> act = save->GetListObject("Actors"); std::list<std::shared_ptr<ISave>> act = save->GetListObject("Actors");
size_t i_id = 0; size_t i_id = 0;
for (auto& i : act) for (auto& i : act)
{ {
auto[it, flag] = actors_map.try_emplace(actors_IDs[i_id], std::static_pointer_cast<Actor>(i)); auto[it, flag] = actors_map_.try_emplace(actors_IDs[i_id], std::static_pointer_cast<Actor>(i));
it->second->world_ = std::static_pointer_cast<World>(shared_from_this()); it->second->world_ = std::static_pointer_cast<World>(shared_from_this());
it->second->name_ = it->first; it->second->name_ = it->first;
i_id++; i_id++;
@@ -60,11 +62,12 @@ void World::load(std::shared_ptr<SaveMap> save)
void World::DestroyActor(std::weak_ptr<Actor> ptr) void World::DestroyActor(std::weak_ptr<Actor> ptr)
{ {
std::scoped_lock lock(m_actors_map_);
std::shared_ptr<Actor> actor = ptr.lock(); std::shared_ptr<Actor> actor = ptr.lock();
if (actor == nullptr) if (actor == nullptr)
return; return;
std::string name = actor->GetName(); std::string name = actor->GetName();
actor->OnDestroy(); actor->OnDestroy();
actors_map.erase(name); actors_map_.erase(name);
} }
+12 -6
View File
@@ -4,6 +4,7 @@
#include <unordered_map> #include <unordered_map>
#include <vector> #include <vector>
#include <string> #include <string>
#include <mutex>
#include "RTTI.h" #include "RTTI.h"
#include "Game/Actors/Actor.hpp" #include "Game/Actors/Actor.hpp"
@@ -15,7 +16,8 @@ class World : public ISave
{ {
GameInstance& game_instance_; GameInstance& game_instance_;
std::unordered_map<std::string, std::shared_ptr<Actor>> actors_map; std::unordered_map<std::string, std::shared_ptr<Actor>> actors_map_;
std::mutex m_actors_map_;
public: public:
World(GameInstance& game_instance); World(GameInstance& game_instance);
@@ -34,8 +36,9 @@ public:
template<class T, template<class...> class Container = std::vector> template<class T, template<class...> class Container = std::vector>
Container<std::weak_ptr<T>> GetActorsByClass() Container<std::weak_ptr<T>> GetActorsByClass()
{ {
std::scoped_lock lock(m_actors_map_);
Container<std::weak_ptr<T>> list; Container<std::weak_ptr<T>> list;
for (auto i = actors_map.cbegin(); i != actors_map.cend(); ++i) for (auto i = actors_map_.cbegin(); i != actors_map_.cend(); ++i)
{ {
if (RTTI::dyn_cast<T, Actor>(i->second.get())) if (RTTI::dyn_cast<T, Actor>(i->second.get()))
list.push_back(std::static_pointer_cast<T>(i->second)); list.push_back(std::static_pointer_cast<T>(i->second));
@@ -46,12 +49,13 @@ public:
template<class T> template<class T>
std::weak_ptr<T> SpawnActorFormClass(std::string name, const Vector3D& loc = { 0, 0, 0 }, const Vector3D& rot = { 0, 0, 0 }) std::weak_ptr<T> SpawnActorFormClass(std::string name, const Vector3D& loc = { 0, 0, 0 }, const Vector3D& rot = { 0, 0, 0 })
{ {
std::scoped_lock lock(m_actors_map_);
std::shared_ptr<T> object = std::make_shared<T>(); std::shared_ptr<T> object = std::make_shared<T>();
object->SetActorLocate(loc); object->SetActorLocate(loc);
object->SetActorRotate(rot); object->SetActorRotate(rot);
std::static_pointer_cast<Actor>(object)->world_ = std::static_pointer_cast<World>(shared_from_this()); std::static_pointer_cast<Actor>(object)->world_ = std::static_pointer_cast<World>(shared_from_this());
std::static_pointer_cast<Actor>(object)->name_ = name; std::static_pointer_cast<Actor>(object)->name_ = name;
actors_map.try_emplace(name, object); actors_map_.try_emplace(name, object);
return object; return object;
} }
@@ -60,9 +64,10 @@ public:
template<class T, template<class...> class Container = std::vector> template<class T, template<class...> class Container = std::vector>
Container<std::weak_ptr<T>> GetActorsByTag(std::string tag) Container<std::weak_ptr<T>> GetActorsByTag(std::string tag)
{ {
std::scoped_lock lock(m_actors_map_);
Container<std::weak_ptr<T>> container; Container<std::weak_ptr<T>> container;
for (auto& i : actors_map) for (auto& i : actors_map_)
{ {
if (RTTI::dyn_cast<T, Actor>(i.second.get())) if (RTTI::dyn_cast<T, Actor>(i.second.get()))
{ {
@@ -81,8 +86,9 @@ public:
template<class T> template<class T>
std::weak_ptr<T> GetActorByName(const std::string& name) std::weak_ptr<T> GetActorByName(const std::string& name)
{ {
auto it = actors_map.find(name); std::scoped_lock lock(m_actors_map_);
if (it == actors_map.end() || it->second == nullptr) auto it = actors_map_.find(name);
if (it == actors_map_.end() || it->second == nullptr)
return {}; return {};
return std::static_pointer_cast<T>(it->second); return std::static_pointer_cast<T>(it->second);