Support ecs behavior forbidden components that prevent applying it to a certain species

This commit is contained in:
Nikita Lisitsa 2022-08-19 09:49:14 +03:00
parent ce287c0416
commit 34ee3f0a6e

View file

@ -89,6 +89,8 @@ namespace psemek::util
template <typename Behavior, typename ... Args> template <typename Behavior, typename ... Args>
void apply(Behavior & behavior, Args const & ... args) void apply(Behavior & behavior, Args const & ... args)
{ {
if (!check_forbidden(behavior, 0))
return;
apply_impl(behavior, static_cast<typename Behavior::component_types *>(nullptr), std::make_index_sequence<std::tuple_size_v<typename Behavior::component_types>>{}, args...); apply_impl(behavior, static_cast<typename Behavior::component_types *>(nullptr), std::make_index_sequence<std::tuple_size_v<typename Behavior::component_types>>{}, args...);
} }
@ -103,6 +105,24 @@ namespace psemek::util
std::string name_; std::string name_;
species_handle id_; species_handle id_;
template <typename Behavior>
bool check_forbidden(Behavior &, typename Behavior::forbidden_component_types * helper)
{
return check_forbidden_impl(helper);
}
template <typename Behavior>
bool check_forbidden(Behavior &, ...)
{
return true;
}
template <typename ... Components>
bool check_forbidden_impl(std::tuple<Components...> *)
{
return (!get_species_component<Components>() && ...);
}
template <typename Behavior, typename ... Components, std::size_t ... Is, typename ... Args> template <typename Behavior, typename ... Components, std::size_t ... Is, typename ... Args>
void apply_impl(Behavior & behavior, std::tuple<Components...> *, std::index_sequence<Is...>, Args const & ... args) void apply_impl(Behavior & behavior, std::tuple<Components...> *, std::index_sequence<Is...>, Args const & ... args)
{ {
@ -395,6 +415,12 @@ namespace psemek::util
friend auto operator <=> (entity_handle const &, entity_handle const &) = default; friend auto operator <=> (entity_handle const &, entity_handle const &) = default;
}; };
template <typename ... Components>
struct without
{
using forbidden_component_types = std::tuple<Components...>;
};
template <typename ... Components> template <typename ... Components>
struct behavior struct behavior
{ {