#pragma once #include #include #include #include #include #include #include namespace XCEngine { namespace Components { class Scene { public: using GameObjectID = uint64_t; static constexpr GameObjectID INVALID_GAMEOBJECT_ID = 0; Scene(); explicit Scene(const std::string& name); ~Scene(); const std::string& GetName() const { return m_name; } void SetName(const std::string& name) { m_name = name; } bool IsActive() const { return m_active; } void SetActive(bool active) { m_active = active; } GameObject* CreateGameObject(const std::string& name, GameObject* parent = nullptr); void DestroyGameObject(GameObject* gameObject); GameObject* Find(const std::string& name) const; GameObject* FindByID(GameObjectID id) const; GameObject* FindGameObjectWithTag(const std::string& tag) const; template T* FindObjectOfType() const { for (auto* go : GetRootGameObjects()) { if (T* comp = go->GetComponent()) { return comp; } if (T* comp = FindInChildren(go)) { return comp; } } return nullptr; } template std::vector FindObjectsOfType() const { std::vector results; for (auto* go : GetRootGameObjects()) { if (T* comp = go->GetComponent()) { results.push_back(comp); } FindInChildren(go, results); } return results; } std::vector GetRootGameObjects() const; void Update(float deltaTime); void FixedUpdate(float fixedDeltaTime); void LateUpdate(float deltaTime); void Save(const std::string& filePath); void Load(const std::string& filePath); std::string SerializeToString() const; void DeserializeFromString(const std::string& data); Core::Event& OnGameObjectCreated() { return m_onGameObjectCreated; } Core::Event& OnGameObjectDestroyed() { return m_onGameObjectDestroyed; } private: GameObject* FindInChildren(GameObject* parent, const std::string& name) const; template T* FindInChildren(GameObject* parent) const { for (size_t i = 0; i < parent->GetChildCount(); ++i) { GameObject* child = parent->GetChild(i); if (T* comp = child->GetComponent()) { return comp; } if (T* comp = FindInChildren(child)) { return comp; } } return nullptr; } template void FindInChildren(GameObject* parent, std::vector& results) const { for (size_t i = 0; i < parent->GetChildCount(); ++i) { GameObject* child = parent->GetChild(i); if (T* comp = child->GetComponent()) { results.push_back(comp); } FindInChildren(child, results); } } std::string m_name; bool m_active = true; std::unordered_map> m_gameObjects; std::vector m_rootGameObjects; std::unordered_set m_gameObjectIDs; Core::Event m_onGameObjectCreated; Core::Event m_onGameObjectDestroyed; friend class GameObject; friend class SceneManager; }; } // namespace Components } // namespace XCEngine