本文作者原创,首发在本博客;CSDN 上的同名文章已标注为转载并回链到本页:https://blog.csdn.net/qq_30220519/article/details/139297473
#include <entt/entt.hpp>
#include
struct position {
float x;
float y;
};
struct velocity {
float dx;
float dy;
};
void update(entt::registry& registry) {
auto view = registry.view<const position, velocity>();
// use a callback
view.each([](const auto& pos, auto& vel) { /* … */ });
// use an extended callback
view.each([](const auto entity, const auto& pos, auto& vel) { /* … */ });
// use a range-for
for (auto [entity, pos, vel] : view.each()) {
// …
}
// use forward iterators and get only the components of interest
for (auto entity : view) {
auto& vel = view.get
// …
}
//registry.destroy(view.begin(), view.end());
}
struct some_component {
int i = 0;
void init() {
std::cout << “I’ve init’d: “ << i << std::endl;
}
};
static void my_free_function(entt::registry&, entt::entity) {
std::cout << “entity call back” << std::endl;
}
void test() {
entt::registry registry;
some_component component;
registry.on_construct<some_component>().connect<&some_component::init>(&component);
// connects a free function void(entt::registry &, entt::entity);
registry.on_construct
//// connects a member function void(entt::registry &, entt::entity);
//registry.on_construct
// disconnects a free function void(entt::registry &, entt::entity);
registry.on_construct
//// disconnects a member function void(entt::registry &, entt::entity);
//registry.on_construct
for (auto i = 0u; i < 10u; ++i) {
const auto entity = registry.create();
auto& emplacePos = registry.emplace
emplacePos.x = 0.;
emplacePos.y = 0.;
if (i % 2 == 0) { registry.emplace
// returns true if the entity is still valid, false otherwise
bool b = registry.valid(entity);
// gets the actual version for the given entity
auto curr = registry.current(entity);
// gets the version contained in the entity identifier
auto version = entt::to_version(entity);
// replaces the component in-place
registry.patch
// constructs a new instance from a list of arguments and replaces the component
//registry.replace
//auto [pos, vel] = registry.get<position, velocity>(entity); // 不确定是否存在,用try_get
//registry.emplace_or_replace
//if (registry.all_of
// registry.replace
//}
//else {
// registry.emplace
//}
// true if entity has at least one of the given components
bool any = registry.any_of<position, velocity>(entity);
registry.erase
registry.remove
registry.clear
}
update(registry);
//registry.release(entity); // 孤立,只移除,不删除。会更新entity版本号
// destroys an entity and all its components
//registry.destroy(entity);
//// default initialized type assigned by copy to all entities
//registry.insert
//// user-defined instance assigned by copy to all entities
//registry.insert(from, to, position{ 0., 0. });
registry.clear(); //销毁registry中的所有实体
// 组件更新监控
entt::observer observer{ registry, entt::collector.update
//entt::collector.group<position, velocity>(entt::exclude
//entt::collector.update
for (const auto entity : observer)
;
observer.clear();
//registry.sort
// return entt::registry::entity(lhs) < entt::registry::entity(rhs);
// });
//registry.sort<movement, physics>();
//// Dependencies
//registry.on_construct<my_type>().connect<&entt::registry::emplace_or_replace<a_type>>(); // 只要将my_type指定给实体,以下代码就会添加(或替换)组件a_type
//registry.on_construct<my_type>().connect<&entt::registry::remove<a_type>>(); // 只要将my_type分配给实体,下面的代码就会从实体中删除a_type
//registry.on_construct<my_type>().disconnect<&entt::registry::emplace_or_replace<a_type>>();
//registry.on_construct
//entt::sigh_helper{ registry }
// .with
// .on_construct<&a_listener>()
// .on_destroy<&another_listener>()
// .with
// .on_update<yet_another_listener>();
// handle
using my_handle = entt::basic_handle<entt::basic_registry
using my_const_handle = entt::basic_handle<const entt::basic_registry
// Organizer 执行图
entt::organizer organizer;
// adds a free function to the organizer
//organizer.emplace<&free_function, position, velocity>(“func”);
//// adds a member function and an instance on which to invoke it to the organizer
//clazz instance;
//organizer.emplace<&clazz::member_function>(&instance);
//organizer.emplace(+[](const void*, entt::registry&) { /* … */ }, & instance);
// adds a decayed lambda directly
organizer.emplace(+[](const void*, entt::registry&) { /* … */ });
std::vectorentt::organizer::vertex graph = organizer.graph();
for (auto&& node : graph) {
node.prepare(registry); // registry与图一起执行
}
// Context 上下文
struct my_type {
int a;
char b;
};
registry.ctx().emplace<my_type>(42, ‘c’);
my_type refType;
registry.ctx().emplace<my_type&>(refType);
auto& var = registry.ctx().get<my_type>();
registry.ctx().erase<my_type>();
const bool contains = registry.ctx().contains<my_type>();
const my_type* value = registry.ctx().find<const my_type>();
//// 保存快照
//output_archive output;
//entt::snapshot{ registry }
// .entities(output)
// .component<a_component, another_component>(output);
//// 加载快照
//input_archive input;
//entt::snapshot_loader{ registry }
// .entities(input)
// .component<a_component, another_component>(input)
// .orphans();
auto view = registry.view
for (auto it = view.rbegin(), last = view.rend(); it != last; ++it) {
}
//auto group = registry.group
//for (auto entity : group) {
// // a component at a time …
// auto& position = group.get
// auto& velocity = group.get
// // … multiple components …
// auto [pos, vel] = group.get<position, velocity>(entity);
// // … all components at once
// //auto [pos, vel, rend] = group.get(entity);
//}
}
创作不易,小小的支持一下吧!

