/* * Copyright (C) 2021 Christopher J. Howard * * This file is part of Antkeeper source code. * * Antkeeper source code is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * Antkeeper source code is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with Antkeeper source code. If not, see . */ #include "resource-loader.hpp" #include "resource-manager.hpp" #include "renderer/model.hpp" #include "entity/components/atmosphere.hpp" #include "entity/components/behavior.hpp" #include "entity/components/collision.hpp" #include "entity/components/terrain.hpp" #include "entity/components/transform.hpp" #include "entity/components/model.hpp" #include "entity/components/orbit.hpp" #include "entity/components/blackbody.hpp" #include "entity/components/celestial-body.hpp" #include "entity/archetype.hpp" #include "entity/ebt.hpp" #include "resources/json.hpp" #include static bool load_component_atmosphere(entity::archetype& archetype, const json& element) { entity::component::atmosphere component; component.exosphere_altitude = 0.0; component.index_of_refraction = 0.0; component.rayleigh_density = 0.0; component.mie_density = 0.0; component.rayleigh_scale_height = 0.0; component.mie_scale_height = 0.0; component.mie_anisotropy = 0.0; if (element.contains("exosphere_altitude")) component.exosphere_altitude = element["exosphere_altitude"].get(); if (element.contains("index_of_refraction")) component.index_of_refraction = element["index_of_refraction"].get(); if (element.contains("rayleigh_density")) component.rayleigh_density = element["rayleigh_density"].get(); if (element.contains("mie_density")) component.mie_density = element["mie_density"].get(); if (element.contains("rayleigh_scale_height")) component.rayleigh_scale_height = element["rayleigh_scale_height"].get(); if (element.contains("mie_scale_height")) component.mie_scale_height = element["mie_scale_height"].get(); if (element.contains("mie_anisotropy")) component.mie_anisotropy = element["mie_anisotropy"].get(); archetype.set(component); return true; } static bool load_component_behavior(entity::archetype& archetype, resource_manager& resource_manager, const json& element) { entity::component::behavior component; component.behavior_tree = nullptr; if (element.contains("file")) { component.behavior_tree = resource_manager.load(element["file"].get()); } archetype.set(component); return (component.behavior_tree != nullptr); } static bool load_component_blackbody(entity::archetype& archetype, const json& element) { entity::component::blackbody component; component.temperature = 0.0; if (element.contains("temperature")) component.temperature = element["temperature"].get(); archetype.set(component); return true; } static bool load_component_celestial_body(entity::archetype& archetype, const json& element) { entity::component::celestial_body component; component.radius = 0.0; component.axial_tilt = 0.0; component.axial_rotation = 0.0; component.angular_frequency = 0.0; if (element.contains("radius")) component.radius = element["radius"].get(); if (element.contains("axial_tilt")) component.axial_tilt = element["axial_tilt"].get(); if (element.contains("axial_rotation")) component.axial_rotation = element["axial_rotation"].get(); if (element.contains("angular_frequency")) component.angular_frequency = element["angular_frequency"].get(); archetype.set(component); return true; } static bool load_component_collision(entity::archetype& archetype, resource_manager& resource_manager, const json& element) { entity::component::collision component; component.mesh = nullptr; if (element.contains("file")) { component.mesh = resource_manager.load(element["file"].get()); } archetype.set(component); return (component.mesh != nullptr); } static bool load_component_model(entity::archetype& archetype, resource_manager& resource_manager, const json& element) { entity::component::model component; component.instance_count = 0; component.layers = ~0; if (element.contains("file")) { component.render_model = resource_manager.load(element["file"].get()); } archetype.set(component); return true; } static bool load_component_orbit(entity::archetype& archetype, const json& element) { entity::component::orbit component; component.elements.e = 0.0; component.elements.a = 0.0; component.elements.i = 0.0; component.elements.raan = 0.0; component.elements.w = 0.0; component.elements.ta = 0.0; if (element.contains("e")) component.elements.e = element["e"].get(); if (element.contains("a")) component.elements.a = element["a"].get(); if (element.contains("i")) component.elements.i = element["i"].get(); if (element.contains("raan")) component.elements.raan = element["raan"].get(); if (element.contains("w")) component.elements.w = element["w"].get(); if (element.contains("ta")) component.elements.ta = element["ta"].get(); archetype.set(component); return true; } static bool load_component_transform(entity::archetype& archetype, const json& element) { entity::component::transform component; component.local = math::identity_transform; component.warp = true; if (element.contains("translation")) { auto translation = element["translation"]; component.local.translation.x = translation[0].get(); component.local.translation.y = translation[1].get(); component.local.translation.z = translation[2].get(); } if (element.contains("rotation")) { auto translation = element["rotation"]; component.local.rotation.w = translation[0].get(); component.local.rotation.x = translation[1].get(); component.local.rotation.y = translation[2].get(); component.local.rotation.z = translation[3].get(); } if (element.contains("scale")) { auto translation = element["scale"]; component.local.scale.x = translation[0].get(); component.local.scale.y = translation[1].get(); component.local.scale.z = translation[2].get(); } archetype.set(component); return true; } static bool load_component(entity::archetype& archetype, resource_manager& resource_manager, json::const_iterator element) { if (element.key() == "atmosphere") return load_component_atmosphere(archetype, element.value()); if (element.key() == "behavior") return load_component_behavior(archetype, resource_manager, element.value()); if (element.key() == "blackbody") return load_component_blackbody(archetype, element.value()); if (element.key() == "celestial_body") return load_component_celestial_body(archetype, element.value()); if (element.key() == "collision") return load_component_collision(archetype, resource_manager, element.value()); if (element.key() == "model") return load_component_model(archetype, resource_manager, element.value()); if (element.key() == "orbit") return load_component_orbit(archetype, element.value()); if (element.key() == "transform") return load_component_transform(archetype, element.value()); //throw std::runtime_error("Unknown component type \"" + element.key() + "\""); return true; } template <> entity::archetype* resource_loader::load(resource_manager* resource_manager, PHYSFS_File* file) { // Allocate archetype entity::archetype* archetype = new entity::archetype(resource_manager->get_archetype_registry()); // Read file into buffer std::size_t size = static_cast(PHYSFS_fileLength(file)); std::string buffer; buffer.resize(size); PHYSFS_readBytes(file, &buffer[0], size); // Parse JSON data from file buffer json data = nlohmann::json::parse(buffer, nullptr, true, true); // Load components from table rows for (json::const_iterator element = data.cbegin(); element != data.cend(); ++element) { if (!load_component(*archetype, *resource_manager, element)) { throw std::runtime_error("Failed to load component \"" + element.key() + "\""); } } return archetype; }