Guest User

EntityManager.h

a guest
Feb 22nd, 2020
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.84 KB | None | 0 0
  1. //EntityManager.h
  2. #pragma once
  3. #include "ServiceLocator.h"
  4. #include <type_traits>
  5. #include "EntitySystemTypes.h"
  6. #include "ComponentContainer.h"
  7.  
  8. class EntityManager
  9. {
  10. public:
  11.  
  12. EntityManager() {};
  13. ~EntityManager() {};
  14.  
  15. template<typename... Args>
  16. constexpr std::size_t length(Args...)
  17. {
  18. std::cout << sizeof...(Args) << '\n';
  19. return sizeof...(Args);
  20. }
  21.  
  22. template <typename FirstComponent, typename... Components>
  23. EntityTypeId CreateEntityType() {
  24.  
  25. typedef ComponentContainer<FirstComponent, Components...> Container;
  26.  
  27. EntityTypeId id = std::numeric_limits<EntityTypeId>::max();
  28.  
  29. if (ServiceLocator<Container>::GetService() == nullptr) {
  30.  
  31. id = numberOfEntityTypes;
  32.  
  33. Container* container = new Container();
  34. ServiceLocator<Container>::SetService(container);
  35. numberOfEntityTypes++;
  36. }
  37. else {
  38. std::cout << "Type already exists when CreatingEntityType " << '\n';
  39. }
  40. return id;
  41. }
  42.  
  43. template <typename FirstComponent, typename... Components>
  44. EntityId CreateEntityOfType() {
  45.  
  46. typedef ComponentContainer<FirstComponent, Components...> Container;
  47.  
  48. Container* container = ServiceLocator<Container>::GetService();
  49.  
  50. if (container == nullptr) {
  51. return std::numeric_limits<EntityId>::max();
  52. }
  53. container->AddElements();
  54.  
  55. EntityId entityId = std::numeric_limits<EntityTypeId>::max();
  56.  
  57. entityId = container->get<0>().size() - 1;
  58.  
  59. return entityId;
  60. }
  61.  
  62. template <typename Component, typename FirstComponent, typename... Components>
  63. Component& GetComponent(EntityId p_entityId) {
  64.  
  65. typedef ComponentContainer<FirstComponent, Components...> Container;
  66.  
  67. Container* container = ServiceLocator<Container>::GetService();
  68.  
  69. return container->GetComponent<Component>(p_entityId);
  70.  
  71. }
  72.  
  73. private:
  74. EntityTypeId numberOfEntityTypes;
  75. EntityId numberOfEntities;
  76.  
  77. };
Advertisement
Add Comment
Please, Sign In to add comment