#ifndef World_h__ #define World_h__ #include #include #include #include #include "Renderer.h" #include "Entity.h" #include "Component.h" #include "Factory.h" #include "Components/Transform.h" #include "Components/Input.h" #include "Components/DirectionalLight.h" #include "System.h" #include "Systems/CollisionSystem.h" class World { public: World(); ~World(); virtual void RegisterComponents(); virtual void RegisterSystems(); EntityID CreateEntity(EntityID parent = 0); void RemoveEntity(EntityID entity); bool ValidEntity(EntityID entity); EntityID GetEntityParent(EntityID entity); template std::shared_ptr AddComponent(EntityID entity, std::string componentType) { std::shared_ptr component = std::shared_ptr(static_cast(m_ComponentFactory.Create(componentType))); if (component == nullptr) { LOG_ERROR("Failed to attach invalid component \"%s\" to entity #%i", componentType.c_str(), entity); return nullptr; } component->Entity = entity; m_ComponentsOfType[componentType].push_back(component); m_EntityComponents[entity][componentType] = component; for (auto system : m_Systems) { system->OnComponentCreated(componentType, component); } return component; } std::shared_ptr AddComponent(EntityID entity, std::string componentType) { return AddComponent(entity, componentType); } template std::shared_ptr GetComponent(EntityID entity, std::string componentType) { return std::static_pointer_cast(m_EntityComponents[entity][componentType]); } /*std::vector GetEntityChildren(EntityID entity);*/ void AddSystem(System* system); void Update(double dt); // Recursively update through the scene graph void RecursiveUpdate(std::shared_ptr system, double dt, EntityID parentEntity); private: EntityID m_LastEntityID; std::stack m_RecycledEntityIDs; // A bottom to top tree. A map of child entities to parent entities. std::unordered_map m_EntityParents; ComponentFactory m_ComponentFactory; std::unordered_map>> m_ComponentsOfType; std::unordered_map>> m_EntityComponents; std::vector> m_Systems; EntityID GenerateEntityID(); void RecycleEntityID(EntityID id); }; #endif // World_h__