fixes api names

This commit is contained in:
2019-04-12 21:23:48 +07:00
parent a9afcbc245
commit ac5457931f
2 changed files with 41 additions and 32 deletions

47
ecs.hpp
View File

@@ -1094,7 +1094,7 @@ namespace ecs_hpp
public: public:
virtual ~component_applier_base() = default; virtual ~component_applier_base() = default;
virtual component_applier_uptr clone() const = 0; virtual component_applier_uptr clone() const = 0;
virtual void apply(entity& ent, bool override) const = 0; virtual void apply_to(entity& ent, bool override) const = 0;
}; };
template < typename T, typename... Args > template < typename T, typename... Args >
@@ -1103,7 +1103,7 @@ namespace ecs_hpp
component_applier(std::tuple<Args...>&& args); component_applier(std::tuple<Args...>&& args);
component_applier(const std::tuple<Args...>& args); component_applier(const std::tuple<Args...>& args);
component_applier_uptr clone() const override; component_applier_uptr clone() const override;
void apply(entity& ent, bool override) const override; void apply_to(entity& ent, bool override) const override;
private: private:
std::tuple<Args...> args_; std::tuple<Args...> args_;
}; };
@@ -1124,15 +1124,18 @@ namespace ecs_hpp
bool empty() const noexcept; bool empty() const noexcept;
void swap(prototype& other) noexcept; void swap(prototype& other) noexcept;
template < typename T, typename... Args > template < typename T >
prototype& assign_component(Args&&... args) &; bool has_component() const noexcept;
template < typename T, typename... Args >
prototype&& assign_component(Args&&... args) &&;
prototype& merge(const prototype& other, bool override) &; template < typename T, typename... Args >
prototype&& merge(const prototype& other, bool override) &&; prototype& component(Args&&... args) &;
template < typename T, typename... Args >
prototype&& component(Args&&... args) &&;
void apply(entity& ent, bool override) const; prototype& merge_with(const prototype& other, bool override) &;
prototype&& merge_with(const prototype& other, bool override) &&;
void apply_to(entity& ent, bool override) const;
private: private:
detail::sparse_map< detail::sparse_map<
family_id, family_id,
@@ -1798,7 +1801,7 @@ namespace ecs_hpp
} }
template < typename T, typename... Args > template < typename T, typename... Args >
void component_applier<T,Args...>::apply(entity& ent, bool override) const { void component_applier<T,Args...>::apply_to(entity& ent, bool override) const {
detail::tiny_tuple_apply([&ent, override](const Args&... args){ detail::tiny_tuple_apply([&ent, override](const Args&... args){
if ( override || !ent.exists_component<T>() ) { if ( override || !ent.exists_component<T>() ) {
ent.assign_component<T>(args...); ent.assign_component<T>(args...);
@@ -1845,8 +1848,14 @@ namespace ecs_hpp
swap(appliers_, other.appliers_); swap(appliers_, other.appliers_);
} }
template < typename T >
bool prototype::has_component() const noexcept {
const auto family = detail::type_family<T>::id();
return appliers_.has(family);
}
template < typename T, typename... Args > template < typename T, typename... Args >
prototype& prototype::assign_component(Args&&... args) & { prototype& prototype::component(Args&&... args) & {
using applier_t = detail::component_applier< using applier_t = detail::component_applier<
T, T,
std::decay_t<Args>...>; std::decay_t<Args>...>;
@@ -1858,12 +1867,12 @@ namespace ecs_hpp
} }
template < typename T, typename... Args > template < typename T, typename... Args >
prototype&& prototype::assign_component(Args&&... args) && { prototype&& prototype::component(Args&&... args) && {
assign_component<T>(std::forward<Args>(args)...); component<T>(std::forward<Args>(args)...);
return std::move(*this); return std::move(*this);
} }
inline prototype& prototype::merge(const prototype& other, bool override) & { inline prototype& prototype::merge_with(const prototype& other, bool override) & {
for ( const auto family_id : other.appliers_ ) { for ( const auto family_id : other.appliers_ ) {
if ( override || !appliers_.has(family_id) ) { if ( override || !appliers_.has(family_id) ) {
appliers_.insert_or_assign( appliers_.insert_or_assign(
@@ -1874,14 +1883,14 @@ namespace ecs_hpp
return *this; return *this;
} }
inline prototype&& prototype::merge(const prototype& other, bool override) && { inline prototype&& prototype::merge_with(const prototype& other, bool override) && {
merge(other, override); merge_with(other, override);
return std::move(*this); return std::move(*this);
} }
inline void prototype::apply(entity& ent, bool override) const { inline void prototype::apply_to(entity& ent, bool override) const {
for ( const auto family_id : appliers_ ) { for ( const auto family_id : appliers_ ) {
appliers_.get(family_id)->apply(ent, override); appliers_.get(family_id)->apply_to(ent, override);
} }
} }
@@ -2035,7 +2044,7 @@ namespace ecs_hpp
inline entity registry::create_entity(const prototype& proto) { inline entity registry::create_entity(const prototype& proto) {
auto ent = create_entity(); auto ent = create_entity();
try { try {
proto.apply(ent, true); proto.apply_to(ent, true);
} catch (...) { } catch (...) {
destroy_entity(ent); destroy_entity(ent);
throw; throw;

View File

@@ -522,7 +522,7 @@ TEST_CASE("registry") {
SECTION("prototypes") { SECTION("prototypes") {
{ {
ecs::prototype p; ecs::prototype p;
p.assign_component<position_c>(1, 2); p.component<position_c>(1, 2);
ecs::registry w; ecs::registry w;
const auto e1 = w.create_entity(p); const auto e1 = w.create_entity(p);
@@ -540,8 +540,8 @@ TEST_CASE("registry") {
} }
{ {
const auto p = ecs::prototype() const auto p = ecs::prototype()
.assign_component<position_c>(1,2) .component<position_c>(1,2)
.assign_component<velocity_c>(3,4); .component<velocity_c>(3,4);
ecs::registry w; ecs::registry w;
const auto e1 = w.create_entity(p); const auto e1 = w.create_entity(p);
@@ -561,8 +561,8 @@ TEST_CASE("registry") {
} }
{ {
const auto p1 = ecs::prototype() const auto p1 = ecs::prototype()
.assign_component<position_c>(1,2) .component<position_c>(1,2)
.assign_component<velocity_c>(3,4); .component<velocity_c>(3,4);
ecs::prototype p2 = p1; ecs::prototype p2 = p1;
ecs::prototype p3; ecs::prototype p3;
@@ -575,20 +575,20 @@ TEST_CASE("registry") {
} }
{ {
const auto p1 = ecs::prototype() const auto p1 = ecs::prototype()
.assign_component<position_c>(1,2) .component<position_c>(1,2)
.merge(ecs::prototype().assign_component<position_c>(3,4), false); .merge_with(ecs::prototype().component<position_c>(3,4), false);
const auto p2 = ecs::prototype() const auto p2 = ecs::prototype()
.assign_component<position_c>(1,2) .component<position_c>(1,2)
.merge(ecs::prototype().assign_component<position_c>(3,4), true); .merge_with(ecs::prototype().component<position_c>(3,4), true);
const auto p3 = ecs::prototype() const auto p3 = ecs::prototype()
.assign_component<position_c>(1,2) .component<position_c>(1,2)
.merge(ecs::prototype().assign_component<velocity_c>(3,4), false); .merge_with(ecs::prototype().component<velocity_c>(3,4), false);
const auto p4 = ecs::prototype() const auto p4 = ecs::prototype()
.assign_component<position_c>(1,2) .component<position_c>(1,2)
.merge(ecs::prototype().assign_component<velocity_c>(3,4), true); .merge_with(ecs::prototype().component<velocity_c>(3,4), true);
ecs::registry w; ecs::registry w;