Components.h 3.8 KB

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