Components.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. #ifndef COMPONENTS_H
  2. #define COMPONENTS_H
  3. #include "utils/HashMap.h"
  4. #include "utils/List.h"
  5. #include "utils/Types.h"
  6. typedef uint32 Entity;
  7. template<typename T>
  8. class Components final {
  9. HashMap<Entity, int> entityToIndex;
  10. List<Entity> indexToEntity;
  11. List<T> components;
  12. public:
  13. template<typename R>
  14. struct Node {
  15. const Entity& entity;
  16. R& component;
  17. };
  18. template<typename C, typename R>
  19. class EntityIterator final {
  20. C& components;
  21. int index;
  22. public:
  23. EntityIterator(C& components, int index)
  24. : components(components), index(index) {
  25. }
  26. EntityIterator& operator++() {
  27. index++;
  28. return *this;
  29. }
  30. bool operator!=(const EntityIterator& other) const {
  31. return index != other.index;
  32. }
  33. Node<R> operator*() const {
  34. return {components.indexToEntity[index],
  35. components.components[index]};
  36. }
  37. };
  38. template<typename C, typename R>
  39. struct EntityIteratorAdapter {
  40. C& components;
  41. EntityIterator<C, R> begin() {
  42. return EntityIterator<C, R>(components, 0);
  43. }
  44. EntityIterator<C, R> end() {
  45. return EntityIterator<C, R>(components,
  46. components.components.getLength());
  47. }
  48. };
  49. template<typename... Args>
  50. void add(Entity e, Args&&... args) {
  51. int index = components.getLength();
  52. if(entityToIndex.tryEmplace(e, index)) {
  53. return;
  54. }
  55. components.add(std::forward<Args>(args)...);
  56. indexToEntity.add(e);
  57. }
  58. void remove(Entity e) {
  59. int* indexP = entityToIndex.search(e);
  60. if(indexP == nullptr) {
  61. return;
  62. }
  63. int lastIndex = components.getLength() - 1;
  64. int index = *indexP;
  65. entityToIndex.remove(e);
  66. components.removeBySwap(index);
  67. if(index == lastIndex) {
  68. indexToEntity.removeBySwap(index);
  69. return;
  70. }
  71. Entity other = indexToEntity[lastIndex];
  72. indexToEntity.removeBySwap(index);
  73. entityToIndex.add(other, index);
  74. }
  75. T* search(Entity e) {
  76. int* index = entityToIndex.search(e);
  77. if(index == nullptr) {
  78. return nullptr;
  79. }
  80. return &(components[*index]);
  81. }
  82. const T* search(Entity e) const {
  83. const int* index = entityToIndex.search(e);
  84. if(index == nullptr) {
  85. return nullptr;
  86. }
  87. return &(components[*index]);
  88. }
  89. T* begin() {
  90. return components.begin();
  91. }
  92. const T* begin() const {
  93. return components.begin();
  94. }
  95. T* end() {
  96. return components.end();
  97. }
  98. const T* end() const {
  99. return components.end();
  100. }
  101. EntityIteratorAdapter<Components, T> entities() {
  102. return {*this};
  103. }
  104. EntityIteratorAdapter<const Components, const T> entities() const {
  105. return {*this};
  106. }
  107. };
  108. #endif