Guest User

Untitled

a guest
Feb 22nd, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.89 KB | None | 0 0
  1. 
  2. #include <entt/entt.hpp>
  3. #include <cstdint>
  4. #include <deque>
  5.  
  6. namespace entt
  7. {
  8. template<typename Entity> using Group = Registry<Entity>;
  9. template<typename Entity> class GroupRegistry
  10. {
  11. public:
  12. using size_type = std::size_t;
  13. using group_id = size_type;
  14. using group_type = Group<Entity>;
  15. public:
  16. GroupRegistry() = default;
  17.  
  18. GroupRegistry(const GroupRegistry &) = delete;
  19. GroupRegistry(GroupRegistry &&) = default;
  20.  
  21. GroupRegistry & operator=(const GroupRegistry &) = delete;
  22. GroupRegistry & operator=(GroupRegistry &&) = default;
  23. public:
  24. group_id create() noexcept
  25. {
  26. group_id group = 0;
  27.  
  28. if (freed.empty())
  29. {
  30. group = groups.size();
  31.  
  32. groups.emplace_back(group_type());
  33. }
  34. else
  35. {
  36. group = freed.front();
  37.  
  38. freed.pop_front();
  39. }
  40.  
  41. return group;
  42. }
  43.  
  44. bool valid(group_id group) const noexcept
  45. {
  46. return group < groups.size();
  47. }
  48.  
  49. bool has(group_id group) const noexcept
  50. {
  51. assert(valid(group));
  52.  
  53. for (auto& idx : freed)
  54. {
  55. if (idx == group)
  56. {
  57. return false;
  58. }
  59. }
  60.  
  61. return true;
  62. }
  63.  
  64. group_type& get(group_id group) noexcept
  65. {
  66. return groups[group];
  67. }
  68.  
  69. bool remove(group_id group)
  70. {
  71. if (has(group))
  72. {
  73. get(group).reset();
  74.  
  75. freed.emplace_back(group);
  76.  
  77. return true;
  78. }
  79. else
  80. {
  81. return false;
  82. }
  83. }
  84.  
  85. void reset()
  86. {
  87. groups.clear();
  88. freed.clear();
  89. }
  90.  
  91. size_type size() const noexcept
  92. {
  93. return groups.size() - freed.size();
  94. }
  95.  
  96. size_type capacity() const noexcept
  97. {
  98. return groups.size();
  99. }
  100.  
  101. bool empty() const noexcept
  102. {
  103. return groups.size() == freed.size();
  104. }
  105. private:
  106. std::deque<group_type> groups;
  107. std::deque<group_id> freed;
  108. };
  109.  
  110. using DefaultGroup = Group<std::uint32_t>;
  111. using DefaultGroupRegistry = GroupRegistry<std::uint32_t>;
  112. }
  113.  
  114.  
  115. struct Position {
  116. float x;
  117. float y;
  118. };
  119.  
  120. struct Velocity {
  121. float dx;
  122. float dy;
  123. };
  124.  
  125. struct GroupEntry
  126. {
  127. entt::DefaultGroupRegistry::group_id id;
  128. };
  129.  
  130. struct LinkInGroup
  131. {
  132. entt::DefaultGroupRegistry::group_id group;
  133. entt::DefaultRegistry::entity_type entity;
  134. };
  135.  
  136. void visit(
  137. entt::DefaultGroupRegistry &groups,
  138. entt::DefaultGroupRegistry::group_id id,
  139. const std::function<bool(entt::DefaultGroup&)>& fn)
  140. {
  141. auto& registry = groups.get(id);
  142.  
  143. if (fn && !fn(registry))
  144. {
  145. return;
  146. }
  147.  
  148. auto view = registry.view<GroupEntry>();
  149.  
  150. for (auto entity : view)
  151. {
  152. auto& group = view.get(entity);
  153.  
  154. visit(groups, group.id, fn);
  155. }
  156. }
  157.  
  158. void update(entt::DefaultRegistry &registry) {
  159.  
  160. auto view = registry.view<Position, Velocity>();
  161.  
  162. for (auto entity : view) {
  163. // gets only the components that are going to be used ...
  164.  
  165. auto &velocity = view.get<Velocity>(entity);
  166.  
  167. velocity.dx = 0.;
  168. velocity.dy = 0.;
  169.  
  170. // ...
  171. }
  172. }
  173.  
  174. void update(std::uint64_t dt, entt::DefaultRegistry &registry) {
  175. registry.view<Position, Velocity>().each([dt](auto entity, auto &position, auto &velocity) {
  176. // gets all the components of the view at once ...
  177.  
  178. position.x += velocity.dx * dt;
  179. position.y += velocity.dy * dt;
  180.  
  181. // ...
  182. });
  183. }
  184.  
  185.  
  186. int main() {
  187. entt::DefaultGroupRegistry rex;
  188.  
  189. rex.remove(rex.create());
  190.  
  191. auto root = rex.create();
  192. auto child = rex.create();
  193. {
  194. auto& reg = rex.get(root);
  195.  
  196. for (auto i = 0; i < 3; ++i)
  197. {
  198. auto entity = reg.create(Position{ i * 2.f, i * 1.f });
  199. if (i == 0) { reg.assign<GroupEntry>(entity, child); }
  200. }
  201. }
  202. auto leaf = rex.create();
  203. {
  204. auto& reg = rex.get(child);
  205.  
  206. for (auto i = 0; i < 5; ++i)
  207. {
  208. auto entity = reg.create(Position{ i * 1.f, i * 3.f });
  209. if (i == 0) { reg.assign<GroupEntry>(entity, leaf); }
  210. }
  211. }
  212. {
  213. auto& reg = rex.get(leaf);
  214.  
  215. for (auto i = 0; i < 10; ++i)
  216. {
  217. auto entity = reg.create(Position{ i * 4.f, i * 4.f });
  218. if (i % 2 == 0) { reg.assign<Velocity>(entity, i * .1f, i * .1f); }
  219. }
  220. }
  221.  
  222. visit(rex, root, [](entt::DefaultGroup& reg) {
  223.  
  224. update(reg);
  225.  
  226. return true;
  227. });
  228.  
  229. visit(rex, root, [](entt::DefaultGroup& reg) {
  230.  
  231. update(10, reg);
  232.  
  233. return true;
  234. });
  235.  
  236. return 0;
  237. }
Add Comment
Please, Sign In to add comment