💿🐜 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.

69 lines
1.6 KiB

  1. /*
  2. * Copyright (C) 2015 Christopher J. Howard
  3. *
  4. * This file is part of Ecosys.
  5. *
  6. * Ecosys 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. * Ecosys 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 Ecosys. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include "entity-manager.hpp"
  20. #include "component-manager.hpp"
  21. EntityManager::EntityManager(ComponentManager* componentManager):
  22. componentManager(componentManager)
  23. {}
  24. EntityManager::~EntityManager()
  25. {}
  26. EntityID EntityManager::createEntity()
  27. {
  28. return idPool.reserveNextID();
  29. }
  30. bool EntityManager::createEntity(EntityID id)
  31. {
  32. if (idPool.isReserved(id))
  33. {
  34. return false;
  35. }
  36. idPool.reserveID(id);
  37. return true;
  38. }
  39. bool EntityManager::destroyEntity(EntityID id)
  40. {
  41. if (!idPool.isReserved(id))
  42. {
  43. return false;
  44. }
  45. // Delete components
  46. ComponentMap* components = componentManager->getComponents(id);
  47. for (auto it = components->begin(); it != components->end(); it = components->begin())
  48. {
  49. ComponentBase* component = it->second;
  50. componentManager->removeComponent(id, component->getComponentType());
  51. delete component;
  52. }
  53. // Free ID
  54. idPool.freeID(id);
  55. return true;
  56. }