if(i==spawn_infos.end())
i = spawn_infos.emplace(spawn_infos.end(), make_unique<SpawnInfo>(this, type.type.get_name(), setup_name));
- Owned<Entity> entity = (this->*type.create_func)(setup_name, tf);
+ Owned<Entity> entity = type.create_func(setup_name, tf);
if(Handle<Zygote> zygote = entity->get_component<Zygote>())
zygote->set_spawn_info(**i);
handler.spawned(move(entity));
#define MSP_GAME_SPAWNER_H_
#include <algorithm>
+#include <functional>
#include "messages.h"
#include "owned.h"
class MSPGAME_API Spawner
{
+public:
+ template<typename T>
+ using LookupFunc = std::function<const T &(const std::string &)>;
+
private:
+ using CreateFunc = std::function<Owned<Entity>(const std::string &, const TransformValues &)>;
+
struct SpawnableType
{
const Reflection::ClassBase &type;
- Owned<Entity> (Spawner::*create_func)(const std::string &, const TransformValues &) const;
+ CreateFunc create_func;
};
DataFile::Collection &resources;
requires std::is_base_of_v<Entity, T>
void add_spawnable_type();
+ template<typename T>
+ requires std::is_base_of_v<Entity, T>
+ void add_spawnable_type(LookupFunc<typename T::Setup>);
+
private:
void add_to_replicator(const std::string &);
private:
void spawn(const SpawnableType &, const std::string &, const TransformValues &);
-
- template<typename T>
- Owned<Entity> create(const std::string &, const TransformValues &) const;
};
template<typename T>
requires std::is_base_of_v<Entity, T>
void Spawner::add_spawnable_type()
+{
+ add_spawnable_type<T>([this](const std::string &n) -> const typename T::Setup & { return resources.get<typename T::Setup>(n); });
+}
+
+template<typename T>
+ requires std::is_base_of_v<Entity, T>
+void Spawner::add_spawnable_type(LookupFunc<typename T::Setup> lookup_func)
{
const Reflection::ClassBase &type = reflector.get_or_create_class<T>();
/* There's assumed to be only a few types per spawner, so sorting is not
necessary */
- spawnable_types.emplace_back(type, &Spawner::create<T>);
+ spawnable_types.emplace_back(type, [this, lookup_func=std::move(lookup_func)](const std::string &sn, const TransformValues &tf){
+ return Owned<T>(parent, lookup_func(sn), tf);
+ });
add_to_replicator(type.get_name());
}
spawn(*i, sname, tf);
}
-template<typename T>
-Owned<Entity> Spawner::create(const std::string &setup_name, const TransformValues &tf) const
-{
- const typename T::Setup &setup = resources.get<typename T::Setup>(setup_name);
- return Owned<T>(parent, setup, tf);
-}
-
} // namespace Msp::Game
#endif