92 lines
2.5 KiB
C++
92 lines
2.5 KiB
C++
#pragma once
|
|
|
|
#include <list>
|
|
#include <unordered_map>
|
|
#include <vector>
|
|
#include <string>
|
|
|
|
#include "RTTI.h"
|
|
#include "Game/Actors/Actor.hpp"
|
|
|
|
class GameInstance;
|
|
|
|
GENERATE_META(World);
|
|
class World : public ISave
|
|
{
|
|
GameInstance& game_instance_;
|
|
|
|
std::unordered_map<std::string, std::shared_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, template<class...> class Container = std::vector>
|
|
Container<std::weak_ptr<T>> GetActorsByClass()
|
|
{
|
|
Container<std::weak_ptr<T>> list;
|
|
for (auto i = actors_map.cbegin(); i != actors_map.cend(); ++i)
|
|
{
|
|
if (RTTI::dyn_cast<T, Actor>(i->second.get()))
|
|
list.push_back(std::static_pointer_cast<T>(i->second));
|
|
}
|
|
return list;
|
|
}
|
|
|
|
template<class T>
|
|
std::weak_ptr<T> SpawnActorFormClass(std::string name, const Vector3D& loc = { 0, 0, 0 }, const Vector3D& rot = { 0, 0, 0 })
|
|
{
|
|
std::shared_ptr<T> object = std::make_shared<T>();
|
|
object->SetActorLocate(loc);
|
|
object->SetActorRotate(rot);
|
|
std::static_pointer_cast<Actor>(object)->world_ = std::static_pointer_cast<World>(shared_from_this());
|
|
std::static_pointer_cast<Actor>(object)->name_ = name;
|
|
actors_map.try_emplace(name, object);
|
|
return object;
|
|
}
|
|
|
|
void DestroyActor(std::weak_ptr<Actor> ptr);
|
|
|
|
template<class T, template<class...> class Container = std::vector>
|
|
Container<std::weak_ptr<T>> GetActorsByTag(std::string tag)
|
|
{
|
|
Container<std::weak_ptr<T>> container;
|
|
|
|
for (auto& i : actors_map)
|
|
{
|
|
if (RTTI::dyn_cast<T, Actor>(i.second.get()))
|
|
{
|
|
const std::list<std::string>& tags = i.second->GetTags();
|
|
for (auto& j : tags)
|
|
{
|
|
if (j == tag)
|
|
container.push_back(i.second);
|
|
}
|
|
}
|
|
}
|
|
|
|
return container;
|
|
}
|
|
|
|
template<class T>
|
|
std::weak_ptr<T> GetActorByName(const std::string& name)
|
|
{
|
|
auto it = actors_map.find(name);
|
|
if (it == actors_map.end() || it->second == nullptr)
|
|
return {};
|
|
|
|
return std::static_pointer_cast<T>(it->second);
|
|
}
|
|
};
|
|
|