💿🐜 Antkeeper source code https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

116 lines
3.0 KiB

  1. /*
  2. * Copyright (C) 2017-2019 Christopher J. Howard
  3. *
  4. * This file is part of Antkeeper Source Code.
  5. *
  6. * Antkeeper Source Code is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * Antkeeper Source Code is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with Antkeeper Source Code. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #ifndef COMPONENT_MANAGER_HPP
  20. #define COMPONENT_MANAGER_HPP
  21. #include "entity-id.hpp"
  22. #include "component.hpp"
  23. #include <list>
  24. #include <map>
  25. class ComponentObserver;
  26. /// Maps component types to components.
  27. typedef std::map<ComponentType, ComponentBase*> ComponentMap;
  28. /// Maps entity IDs to a component map.
  29. typedef std::map<EntityID, ComponentMap> EntityComponentMap;
  30. /**
  31. * Manages the aggregation of components which make up entities.
  32. */
  33. class ComponentManager
  34. {
  35. public:
  36. /**
  37. * Creates an instance of ComponentManager.
  38. */
  39. ComponentManager() = default;
  40. /**
  41. * Destroys an instance of ComponentManager.
  42. */
  43. ~ComponentManager() = default;
  44. /**
  45. * Adds a ComponentObserver.
  46. *
  47. * @param observer Pointer to the observer to add.
  48. */
  49. void addComponentObserver(ComponentObserver* observer);
  50. /**
  51. * Removes a ComponentObserver.
  52. *
  53. * @param observer Pointer to the observer to remove.
  54. */
  55. void removeComponentObserver(ComponentObserver* observer);
  56. /**
  57. * Adds a component to the specified entity.
  58. *
  59. * @param entity Specifies an entity.
  60. * @param component Specifies the component to add.
  61. */
  62. void addComponent(EntityID entity, ComponentBase* component);
  63. /**
  64. * Removes a component from the specified entity.
  65. *
  66. * @param entity Specifies an entity.
  67. * @param type Specifies the type of component.
  68. *
  69. * @return Pointer to the removed component.
  70. */
  71. ComponentBase* removeComponent(EntityID entity, ComponentType type);
  72. /**
  73. * Returns the specified component of an entity.
  74. *
  75. * @param entity Specifies an entity.
  76. * @param type Specifies the type of component.
  77. *
  78. * @return Pointer to the component, or `nullptr` if the specified component was not found.
  79. */
  80. ComponentBase* getComponent(EntityID entity, ComponentType type);
  81. /**
  82. * Returns the component map of the specified entity.
  83. *
  84. * @param entity Specifies an entity.
  85. *
  86. * @return Pointer to the component map.
  87. */
  88. ComponentMap* getComponents(EntityID entity);
  89. private:
  90. ComponentManager(const ComponentManager&) = delete;
  91. ComponentManager& operator=(const ComponentManager&) = delete;
  92. EntityComponentMap entityMap;
  93. std::list<ComponentObserver*> componentObservers;
  94. };
  95. inline ComponentMap* ComponentManager::getComponents(EntityID entity)
  96. {
  97. return &entityMap[entity];
  98. }
  99. #endif // COMPONENT_MANAGER_HPP