Move world on unordered_map and object_ptr
This commit is contained in:
+27
-10
@@ -8,32 +8,49 @@ World::World(GameInstance& game_instance) : game_instance(game_instance)
|
||||
|
||||
World::~World()
|
||||
{
|
||||
actors.clear();
|
||||
actors_map.clear();
|
||||
}
|
||||
|
||||
void World::BeginPlay()
|
||||
{
|
||||
for (auto& i : actors)
|
||||
i->BeginPlay();
|
||||
for (auto& i : actors_map)
|
||||
i.second->BeginPlay();
|
||||
}
|
||||
|
||||
void World::Tick(double delta_time)
|
||||
{
|
||||
for (auto& i : actors)
|
||||
i->Tick(delta_time);
|
||||
for (auto& i : actors_map)
|
||||
i.second->Tick(delta_time);
|
||||
}
|
||||
|
||||
std::shared_ptr<SaveMap> World::save()
|
||||
{
|
||||
std::shared_ptr<SaveMap> save = std::make_shared<SaveMap>("World");
|
||||
save->SaveListObject("Actors", std::move(reinterpret_cast<std::list<UType::object_ptr<ISave>>&>(actors)));
|
||||
return save;
|
||||
}
|
||||
|
||||
void World::load(std::shared_ptr<SaveMap> save)
|
||||
{
|
||||
actors = std::move(reinterpret_cast<std::list<UType::object_ptr<Actor>>&>(save->GetListObject("Actors")));
|
||||
std::vector<std::string> actors_IDs = save->GetVectorString("ActorsIDs");
|
||||
std::list<UType::object_ptr<Actor>> act = std::move(reinterpret_cast<std::list<UType::object_ptr<Actor>>&>(save->GetListObject("Actors")));
|
||||
|
||||
for (auto& i : actors)
|
||||
i->world_ = this;
|
||||
}
|
||||
size_t i_id = 0;
|
||||
for (auto& i : act)
|
||||
{
|
||||
auto[it, flag] = actors_map.try_emplace(actors_IDs[i_id], i);
|
||||
it->second->world_ = this;
|
||||
it->second->name_ = it->first;
|
||||
i_id++;
|
||||
}
|
||||
}
|
||||
|
||||
void World::DestroyActor(UType::object_ptr<Actor> ptr)
|
||||
{
|
||||
if (ptr.get() == nullptr)
|
||||
return;
|
||||
|
||||
std::string name = ptr->GetName();
|
||||
actors_map.erase(name);
|
||||
ptr->OnDestroy();
|
||||
ptr.destroy();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user