From a3de58f3c153e5f35a34ebb635b8c65add6071b6 Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Sun, 18 Jun 2023 00:40:21 +0300 Subject: [PATCH] Support custom lookup functions for retrieving setups in Spawner --- source/game/spawner.cpp | 2 +- source/game/spawner.h | 34 ++++++++++++++++++++++------------ 2 files changed, 23 insertions(+), 13 deletions(-) diff --git a/source/game/spawner.cpp b/source/game/spawner.cpp index f696d4d..9c0c3e6 100644 --- a/source/game/spawner.cpp +++ b/source/game/spawner.cpp @@ -45,7 +45,7 @@ void Spawner::spawn(const SpawnableType &type, const string &setup_name, const T if(i==spawn_infos.end()) i = spawn_infos.emplace(spawn_infos.end(), make_unique(this, type.type.get_name(), setup_name)); - Owned entity = (this->*type.create_func)(setup_name, tf); + Owned entity = type.create_func(setup_name, tf); if(Handle zygote = entity->get_component()) zygote->set_spawn_info(**i); handler.spawned(move(entity)); diff --git a/source/game/spawner.h b/source/game/spawner.h index 95ac903..0473e71 100644 --- a/source/game/spawner.h +++ b/source/game/spawner.h @@ -2,6 +2,7 @@ #define MSP_GAME_SPAWNER_H_ #include +#include #include "messages.h" #include "owned.h" @@ -28,11 +29,17 @@ public: class MSPGAME_API Spawner { +public: + template + using LookupFunc = std::function; + private: + using CreateFunc = std::function(const std::string &, const TransformValues &)>; + struct SpawnableType { const Reflection::ClassBase &type; - Owned (Spawner::*create_func)(const std::string &, const TransformValues &) const; + CreateFunc create_func; }; DataFile::Collection &resources; @@ -50,6 +57,10 @@ public: requires std::is_base_of_v void add_spawnable_type(); + template + requires std::is_base_of_v + void add_spawnable_type(LookupFunc); + private: void add_to_replicator(const std::string &); @@ -64,20 +75,26 @@ public: private: void spawn(const SpawnableType &, const std::string &, const TransformValues &); - - template - Owned create(const std::string &, const TransformValues &) const; }; template requires std::is_base_of_v void Spawner::add_spawnable_type() +{ + add_spawnable_type([this](const std::string &n) -> const typename T::Setup & { return resources.get(n); }); +} + +template + requires std::is_base_of_v +void Spawner::add_spawnable_type(LookupFunc lookup_func) { const Reflection::ClassBase &type = reflector.get_or_create_class(); /* There's assumed to be only a few types per spawner, so sorting is not necessary */ - spawnable_types.emplace_back(type, &Spawner::create); + spawnable_types.emplace_back(type, [this, lookup_func=std::move(lookup_func)](const std::string &sn, const TransformValues &tf){ + return Owned(parent, lookup_func(sn), tf); + }); add_to_replicator(type.get_name()); } @@ -93,13 +110,6 @@ void Spawner::spawn(const std::string &sname, const TransformValues &tf) spawn(*i, sname, tf); } -template -Owned Spawner::create(const std::string &setup_name, const TransformValues &tf) const -{ - const typename T::Setup &setup = resources.get(setup_name); - return Owned(parent, setup, tf); -} - } // namespace Msp::Game #endif -- 2.45.2