Copperfield Engine 0.1
C++ Game Engine
Loading...
Searching...
No Matches
lists_iterator.hpp
1#pragma once
2
8template <typename T, typename... Types>
10 public:
14 ComponentIterator(T& main, Types&... list)
15 : lists_(std::make_tuple(&list...)),
16 main_list_(&main),
17 main_it_(main.begin()) {}
18
23 bool next() {
24 auto all_exist = [this](auto&... lists) -> bool {
25 return (true && ... && lists->at(main_it_.pos()).valid());
26 };
27 auto it_end = main_list_->end();
28 while (main_it_ != it_end) {
29 if (main_it_.valid()) {
30 if (std::apply(all_exist, lists_)) {
31 return true;
32 }
33 }
34 ++main_it_;
35 }
36 return false;
37 }
38
42 auto get() {
43 auto res = std::apply(
44 [this](auto&... lists) {
45 return std::make_tuple(
46 std::ref(main_it_.component()),
47 std::ref(lists->at(main_it_.pos()).component())...);
48 },
49 lists_);
50 ++main_it_;
51 return res;
52 }
53
61 auto t = main_it_;
62 --t;
63 Entity e = Entity(t.pos());
64 return e;
65 }
66
68 void reset() { main_it_ = main_list_->begin(); }
69
70 private:
71 std::tuple<Types*...> lists_;
72 T* main_list_;
73 typename T::Iterator main_it_;
74};
Helper to iterate several component lists simultaneously.
Definition: lists_iterator.hpp:9
Entity getEntity()
retrieves the entity being currently iterated
Definition: lists_iterator.hpp:60
auto get()
Retrieve the components of the last entity found using next.
Definition: lists_iterator.hpp:42
bool next()
Iterates all lists until an entity containing all components is found.
Definition: lists_iterator.hpp:23
ComponentIterator(T &main, Types &... list)
Constructor.
Definition: lists_iterator.hpp:14
void reset()
Resets the internal state, so that you can iterate the lists again.
Definition: lists_iterator.hpp:68
Identifies an entity. This class can only be modified by the engine.
Definition: component_manager.hpp:16