#include "Scene/SceneManager.h" namespace XCEngine { namespace Components { SceneManager& SceneManager::Get() { static SceneManager instance; return instance; } Scene* SceneManager::CreateScene(const std::string& name) { auto scene = std::make_unique(name); Scene* ptr = scene.get(); m_scenes[name] = std::move(scene); if (!m_activeScene) { m_activeScene = ptr; } m_onSceneLoaded.Invoke(ptr); return ptr; } void SceneManager::LoadScene(const std::string& filePath) { auto scene = std::make_unique(); scene->Load(filePath); Scene* ptr = scene.get(); std::string name = filePath; size_t pos = name.find_last_of("/\\"); if (pos != std::string::npos) { name = name.substr(pos + 1); } pos = name.find_last_of('.'); if (pos != std::string::npos) { name = name.substr(0, pos); } m_scenes[name] = std::move(scene); m_onSceneLoaded.Invoke(ptr); } void SceneManager::LoadSceneAsync(const std::string& filePath, std::function callback) { LoadScene(filePath); if (callback) { Scene* scene = GetScene(filePath); callback(scene); } } void SceneManager::UnloadScene(Scene* scene) { if (!scene) { return; } if (m_activeScene == scene) { m_activeScene = nullptr; } auto it = m_scenes.begin(); while (it != m_scenes.end()) { if (it->second.get() == scene) { m_onSceneUnloaded.Invoke(scene); it = m_scenes.erase(it); break; } else { ++it; } } if (m_activeScene == nullptr && !m_scenes.empty()) { m_activeScene = m_scenes.begin()->second.get(); m_onActiveSceneChanged.Invoke(m_activeScene); } } void SceneManager::UnloadScene(const std::string& sceneName) { auto it = m_scenes.find(sceneName); if (it != m_scenes.end()) { UnloadScene(it->second.get()); } } void SceneManager::SetActiveScene(Scene* scene) { if (m_activeScene == scene) { return; } m_activeScene = scene; m_onActiveSceneChanged.Invoke(scene); } void SceneManager::SetActiveScene(const std::string& sceneName) { auto it = m_scenes.find(sceneName); if (it != m_scenes.end()) { SetActiveScene(it->second.get()); } } Scene* SceneManager::GetScene(const std::string& name) const { auto it = m_scenes.find(name); if (it != m_scenes.end()) { return it->second.get(); } return nullptr; } std::vector SceneManager::GetAllScenes() const { std::vector result; for (auto& pair : m_scenes) { result.push_back(pair.second.get()); } return result; } } // namespace Components } // namespace XCEngine