86 lines
2.1 KiB
C++
86 lines
2.1 KiB
C++
#pragma once
|
|
|
|
#include <list>
|
|
#include <unordered_map>
|
|
#include <vector>
|
|
#include <string>
|
|
|
|
#include "RTTI.h"
|
|
#include "Game/Actors/Actor.hpp"
|
|
#include "Game/Actors/Mesh/Mesh.hpp"
|
|
#include "Types/object_ptr.hpp"
|
|
|
|
class GameInstance;
|
|
|
|
GENERATE_META(World);
|
|
class World : public ISave
|
|
{
|
|
GameInstance& game_instance;
|
|
|
|
std::unordered_map<std::string, UType::object_ptr<Actor>> actors_map;
|
|
|
|
public:
|
|
World(GameInstance& game_instance);
|
|
~World() override;
|
|
|
|
virtual void BeginPlay();
|
|
virtual void Tick(double delta_time);
|
|
|
|
GameInstance& GetGameInstance() const { return game_instance; }
|
|
|
|
#pragma region ISave
|
|
std::shared_ptr<SaveMap> save() override;
|
|
void load(std::shared_ptr<SaveMap> save) override;
|
|
#pragma endregion
|
|
|
|
template<class T>
|
|
std::vector<UType::object_ptr<T>> GetActorsByClass()
|
|
{
|
|
std::vector<UType::object_ptr<T>> list;
|
|
for (auto i = actors_map.cbegin(); i != actors_map.cend(); ++i)
|
|
{
|
|
if (T* cast_object = RTTI::dyn_cast<T, Actor>(i->second.get()))
|
|
list.push_back(UType::object_ptr<T>(cast_object));
|
|
}
|
|
return list;
|
|
}
|
|
|
|
template<class T>
|
|
UType::object_ptr<T> SpawnActorFormClass(std::string name, const Vector3D& loc = { 0, 0, 0 }, const Vector3D& rot = { 0, 0, 0 })
|
|
{
|
|
UType::object_ptr<T> object(new T());
|
|
object->SetActorLocate(loc);
|
|
object->SetActorRotate(rot);
|
|
static_cast<Actor*>(object)->world_ = this;
|
|
actors_map.try_emplace(name, object);
|
|
return object;
|
|
}
|
|
|
|
void DestroyActor(UType::object_ptr<Actor> ptr);
|
|
|
|
template<class T, class Container = std::vector<T*>>
|
|
Container GetActorsByTag(std::string tag)
|
|
{
|
|
Container container;
|
|
|
|
for (auto& i : actors_map)
|
|
{
|
|
const std::list<std::string>& tags = i.second->GetTags();
|
|
for (auto& j : tags)
|
|
{
|
|
if (j == tag)
|
|
container.push_back(i);
|
|
}
|
|
}
|
|
|
|
return container;
|
|
}
|
|
|
|
template<class T>
|
|
UType::object_ptr<T> GetActorByID(const std::string& id)
|
|
{
|
|
return actors_map[id];
|
|
}
|
|
};
|
|
|