#pragma once #include #include #include #include #include #include "../../YoggieEngine/src/Scene/Entity.h" void WriteFile(std::string& emitter, std::filesystem::path path) { std::cout << "Writing Scene file to: " << path.u8string() << std::endl; std::ofstream sceneFile; sceneFile.open(path.u8string()); sceneFile << emitter.c_str(); sceneFile.close(); } YAML::Emitter& operator<< (YAML::Emitter& emitter, glm::vec3& vector) { emitter << YAML::Flow << YAML::BeginSeq << vector.x << vector.y << vector.x << YAML::EndSeq; return emitter; } std::string Serialize( Scene& scene) { YAML::Emitter emitter; emitter << YAML::BeginMap; emitter << YAML::Key << "Scene" << YAML::Value << "test-Scene"; emitter << YAML::Key << "Entities" << YAML::Value << YAML::BeginSeq; scene.getReg().each([&scene, &emitter](auto enttNumber) { Entity entity = Entity(enttNumber, &scene); emitter << YAML::BeginMap; emitter << YAML::Key << "Entity" << YAML::Value << entity.GetComponent().name; if (entity.HasComponent()) { emitter << YAML::Key << "Ident"; emitter << YAML::BeginMap; emitter << YAML::Value << entity.GetComponent().name; emitter << YAML::EndMap; } if (entity.HasComponent()) { emitter << YAML::Key << "Transform" << YAML::Value ; emitter << YAML::BeginMap; emitter << YAML::Key << "Position"; emitter << YAML::Value << entity.GetComponent().Position; emitter << YAML::Key << "Rotation"; emitter << YAML::Value << entity.GetComponent().Rotation; emitter << YAML::Key << "Scale"; emitter << YAML::Value << entity.GetComponent().Scale; emitter << YAML::EndMap; } if (entity.HasComponent()) { emitter << YAML::Key << "Light"; emitter << YAML::Value; emitter << YAML::BeginMap; emitter << YAML::Key << "Color"; emitter << YAML::Value << entity.GetComponent().Color; emitter << YAML::EndMap; } emitter << YAML::EndMap; }); emitter << YAML::EndSeq; emitter << YAML::EndMap; return std::string(emitter.c_str()); } void SaveScene(std::filesystem::path path, Scene& scene) { std::string YAMLString = Serialize(scene); WriteFile(YAMLString, path); } void LoadScene(std::filesystem::path path, Scene& scene) { auto sceneYAML = YAML::LoadFile(path.u8string()); if (!sceneYAML["Scene"]) { spdlog::error("Not a scene file!"); return; } scene.getReg().clear(); std::string SceneName = sceneYAML["Scene"].as(); auto entities = sceneYAML["Entities"]; for (const auto& entity : entities) { std::string entityID = entity["Ident"].as(); YoggieEngine::Entity SE = scene.AddEntity(entityID); if (entity["Transform"]) { TransformComponent tc = SE.GetComponent(); auto positionNode = entity["Transform"]["Position"]; tc.Position = glm::vec3(positionNode[0].as(), positionNode[1].as(), positionNode[2].as()); auto rotationNode = entity["Transform"]["Rotation"]; tc.Rotation = glm::vec3(rotationNode[0].as(), rotationNode[1].as(), rotationNode[2].as()); auto scaleNode = entity["Transform"]["Scale"]; tc.Scale = glm::vec3(scaleNode[0].as(), scaleNode[1].as(), scaleNode[2].as()); } if (entity["Light"]) { LightComponent lc = SE.AddComponent(); lc.Color = glm::vec3(entity["Light"]["Color"][0].as(), entity["Light"]["Color"][1].as(), entity["Light"]["Color"][2].as()); } } }