Move world on unordered_map and object_ptr
This commit is contained in:
@@ -991,7 +991,7 @@ RenderEngine::~RenderEngine()
|
|||||||
void RenderEngine::start()
|
void RenderEngine::start()
|
||||||
{
|
{
|
||||||
World* world = core_.GetGameInstance()->GetWorld();
|
World* world = core_.GetGameInstance()->GetWorld();
|
||||||
std::vector<Mesh*> meshes = world->GetActorsByClass<Mesh>();
|
std::vector<UType::object_ptr<Mesh>> meshes = world->GetActorsByClass<Mesh>();
|
||||||
for (auto& i : meshes)
|
for (auto& i : meshes)
|
||||||
i->load_model(i->GetModelName());
|
i->load_model(i->GetModelName());
|
||||||
|
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ void Actor::OnDestroy()
|
|||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
Actor::Actor()
|
Actor::Actor() : loc(new Vector3D), rot(new Vector3D), scale(new Vector3D)
|
||||||
{
|
{
|
||||||
SetType(Classes::Actor);
|
SetType(Classes::Actor);
|
||||||
}
|
}
|
||||||
@@ -60,9 +60,3 @@ void Actor::RemoveTag(const std::string& tag) noexcept
|
|||||||
{
|
{
|
||||||
tags.remove(tag);
|
tags.remove(tag);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Actor::Destroy()
|
|
||||||
{
|
|
||||||
OnDestroy();
|
|
||||||
//world_->actors.remove(this);
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -15,6 +15,7 @@ GENERATE_META(Actor)
|
|||||||
class Actor : public ISave, public IRTTI
|
class Actor : public ISave, public IRTTI
|
||||||
{
|
{
|
||||||
World* world_;
|
World* world_;
|
||||||
|
std::string name_;
|
||||||
|
|
||||||
UType::object_ptr<Vector3D> loc, rot, scale;
|
UType::object_ptr<Vector3D> loc, rot, scale;
|
||||||
|
|
||||||
@@ -53,9 +54,9 @@ public:
|
|||||||
void AddTag(const std::string& tag) noexcept;
|
void AddTag(const std::string& tag) noexcept;
|
||||||
void RemoveTag(const std::string& tag) noexcept;
|
void RemoveTag(const std::string& tag) noexcept;
|
||||||
const std::list<std::string>& GetTags() const { return tags; }
|
const std::list<std::string>& GetTags() const { return tags; }
|
||||||
void Destroy();
|
|
||||||
|
|
||||||
World* GetWorld() const { return world_; }
|
World* GetWorld() const { return world_; }
|
||||||
|
const std::string& GetName() const { return name_; }
|
||||||
|
|
||||||
friend class World;
|
friend class World;
|
||||||
};
|
};
|
||||||
|
|||||||
+27
-10
@@ -8,32 +8,49 @@ World::World(GameInstance& game_instance) : game_instance(game_instance)
|
|||||||
|
|
||||||
World::~World()
|
World::~World()
|
||||||
{
|
{
|
||||||
actors.clear();
|
actors_map.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
void World::BeginPlay()
|
void World::BeginPlay()
|
||||||
{
|
{
|
||||||
for (auto& i : actors)
|
for (auto& i : actors_map)
|
||||||
i->BeginPlay();
|
i.second->BeginPlay();
|
||||||
}
|
}
|
||||||
|
|
||||||
void World::Tick(double delta_time)
|
void World::Tick(double delta_time)
|
||||||
{
|
{
|
||||||
for (auto& i : actors)
|
for (auto& i : actors_map)
|
||||||
i->Tick(delta_time);
|
i.second->Tick(delta_time);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::shared_ptr<SaveMap> World::save()
|
std::shared_ptr<SaveMap> World::save()
|
||||||
{
|
{
|
||||||
std::shared_ptr<SaveMap> save = std::make_shared<SaveMap>("World");
|
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;
|
return save;
|
||||||
}
|
}
|
||||||
|
|
||||||
void World::load(std::shared_ptr<SaveMap> 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)
|
size_t i_id = 0;
|
||||||
i->world_ = this;
|
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();
|
||||||
|
}
|
||||||
|
|||||||
+21
-13
@@ -1,6 +1,7 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <list>
|
#include <list>
|
||||||
|
#include <unordered_map>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
@@ -16,8 +17,7 @@ class World : public ISave
|
|||||||
{
|
{
|
||||||
GameInstance& game_instance;
|
GameInstance& game_instance;
|
||||||
|
|
||||||
// Actors only
|
std::unordered_map<std::string, UType::object_ptr<Actor>> actors_map;
|
||||||
std::list<UType::object_ptr<Actor>> actors;
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
World(GameInstance& game_instance);
|
World(GameInstance& game_instance);
|
||||||
@@ -33,37 +33,39 @@ public:
|
|||||||
void load(std::shared_ptr<SaveMap> save) override;
|
void load(std::shared_ptr<SaveMap> save) override;
|
||||||
#pragma endregion
|
#pragma endregion
|
||||||
|
|
||||||
template<class T, class Container = std::vector<T*>>
|
template<class T>
|
||||||
Container GetActorsByClass()
|
std::vector<UType::object_ptr<T>> GetActorsByClass()
|
||||||
{
|
{
|
||||||
Container list;
|
std::vector<UType::object_ptr<T>> list;
|
||||||
for (auto i = actors.cbegin(); i != actors.cend(); ++i)
|
for (auto i = actors_map.cbegin(); i != actors_map.cend(); ++i)
|
||||||
{
|
{
|
||||||
if (T* cast_object = RTTI::dyn_cast<T, Actor>((*i).get()))
|
if (T* cast_object = RTTI::dyn_cast<T, Actor>(i->second.get()))
|
||||||
list.push_back(cast_object);
|
list.push_back(UType::object_ptr<T>(cast_object));
|
||||||
}
|
}
|
||||||
return list;
|
return list;
|
||||||
}
|
}
|
||||||
|
|
||||||
template<class T>
|
template<class T>
|
||||||
T* SpawnActorFormClass(const Vector3D& loc = { 0, 0, 0 }, const Vector3D& rot = { 0, 0, 0 })
|
UType::object_ptr<T> SpawnActorFormClass(std::string name, const Vector3D& loc = { 0, 0, 0 }, const Vector3D& rot = { 0, 0, 0 })
|
||||||
{
|
{
|
||||||
T* object = new T();
|
UType::object_ptr<T> object(new T());
|
||||||
object->SetActorLocate(loc);
|
object->SetActorLocate(loc);
|
||||||
object->SetActorRotate(rot);
|
object->SetActorRotate(rot);
|
||||||
static_cast<Actor*>(object)->world_ = this;
|
static_cast<Actor*>(object)->world_ = this;
|
||||||
actors.push_back(object);
|
actors_map.try_emplace(name, object);
|
||||||
return object;
|
return object;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void DestroyActor(UType::object_ptr<Actor> ptr);
|
||||||
|
|
||||||
template<class T, class Container = std::vector<T*>>
|
template<class T, class Container = std::vector<T*>>
|
||||||
Container GetActorsByTag(std::string tag)
|
Container GetActorsByTag(std::string tag)
|
||||||
{
|
{
|
||||||
Container container;
|
Container container;
|
||||||
|
|
||||||
for (auto& i : actors)
|
for (auto& i : actors_map)
|
||||||
{
|
{
|
||||||
const std::list<std::string>& tags = i->GetTags();
|
const std::list<std::string>& tags = i.second->GetTags();
|
||||||
for (auto& j : tags)
|
for (auto& j : tags)
|
||||||
{
|
{
|
||||||
if (j == tag)
|
if (j == tag)
|
||||||
@@ -73,5 +75,11 @@ public:
|
|||||||
|
|
||||||
return container;
|
return container;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template<class T>
|
||||||
|
UType::object_ptr<T> GetActorByID(const std::string& id)
|
||||||
|
{
|
||||||
|
return actors_map[id];
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -3,7 +3,7 @@
|
|||||||
"dtime": 5.000000,
|
"dtime": 5.000000,
|
||||||
"Parent parameters": {
|
"Parent parameters": {
|
||||||
"Class name":"World",
|
"Class name":"World",
|
||||||
"vsActorsIDs": ["Mesh"],
|
"vsActorsIDs": ["TestMesh1"],
|
||||||
"loActors": [
|
"loActors": [
|
||||||
{
|
{
|
||||||
"Class name": "StaticMesh",
|
"Class name": "StaticMesh",
|
||||||
@@ -14,9 +14,9 @@
|
|||||||
"Class name":"Actor",
|
"Class name":"Actor",
|
||||||
"oloc": {
|
"oloc": {
|
||||||
"Class name":"Vector3D",
|
"Class name":"Vector3D",
|
||||||
"dx":0.0,
|
"dx":1.0,
|
||||||
"dy":0.0,
|
"dy":-1.0,
|
||||||
"dz":0.0
|
"dz":1.0
|
||||||
},
|
},
|
||||||
"orot": {
|
"orot": {
|
||||||
"Class name":"Vector3D",
|
"Class name":"Vector3D",
|
||||||
@@ -26,9 +26,9 @@
|
|||||||
},
|
},
|
||||||
"oscale": {
|
"oscale": {
|
||||||
"Class name": "Vector3D",
|
"Class name": "Vector3D",
|
||||||
"dx": 0.0,
|
"dx": 1.0,
|
||||||
"dy": 0.0,
|
"dy": 1.0,
|
||||||
"dz": 0.0
|
"dz": 1.0
|
||||||
},
|
},
|
||||||
"tags":[]
|
"tags":[]
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -21,6 +21,13 @@ void TestWorld::Tick(double delta_time)
|
|||||||
if (time >= 10.0)
|
if (time >= 10.0)
|
||||||
{
|
{
|
||||||
Message("TestWorld: Time out");
|
Message("TestWorld: Time out");
|
||||||
|
std::vector<UType::object_ptr<Mesh>> meshes = GetActorsByClass<Mesh>();
|
||||||
|
if (!meshes.empty())
|
||||||
|
{
|
||||||
|
DestroyActor(static_cast<UType::object_ptr<Actor>>(meshes[0]));
|
||||||
|
time = 7.0;
|
||||||
|
return;
|
||||||
|
}
|
||||||
GetGameInstance().quit();
|
GetGameInstance().quit();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,21 @@
|
|||||||
#include <gtest/gtest.h>
|
#include <gtest/gtest.h>
|
||||||
#include "Types/object_ptr.hpp"
|
#include "Types/object_ptr.hpp"
|
||||||
|
|
||||||
|
class TestClass
|
||||||
|
{
|
||||||
|
int* test_ptr_;
|
||||||
|
public:
|
||||||
|
TestClass(int* test_ptr) : test_ptr_(test_ptr)
|
||||||
|
{
|
||||||
|
*test_ptr_ = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
~TestClass()
|
||||||
|
{
|
||||||
|
*test_ptr_ = 2;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
TEST(object_ptr_test, base_test)
|
TEST(object_ptr_test, base_test)
|
||||||
{
|
{
|
||||||
int* test = new int(123);
|
int* test = new int(123);
|
||||||
@@ -23,4 +38,22 @@ TEST(object_ptr_test, check_destry)
|
|||||||
ptr.destroy();
|
ptr.destroy();
|
||||||
EXPECT_EQ(ptr.get(), nullptr);
|
EXPECT_EQ(ptr.get(), nullptr);
|
||||||
EXPECT_EQ(ptr2.get(), nullptr);
|
EXPECT_EQ(ptr2.get(), nullptr);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(object_ptr_test, check_destroy)
|
||||||
|
{
|
||||||
|
int* test = new int(0);
|
||||||
|
{
|
||||||
|
UType::object_ptr<TestClass> ptr(new TestClass(test));
|
||||||
|
EXPECT_EQ(*test, 1);
|
||||||
|
}
|
||||||
|
EXPECT_EQ(*test, 2);
|
||||||
|
|
||||||
|
*test = 0;
|
||||||
|
{
|
||||||
|
UType::object_ptr<TestClass> ptr(new TestClass(test));
|
||||||
|
UType::object_ptr<TestClass> ptr2(ptr);
|
||||||
|
EXPECT_EQ(*test, 1);
|
||||||
|
}
|
||||||
|
EXPECT_EQ(*test, 2);
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user