|
| 1 | +#include <spaced/game/asset_list.hpp> |
| 2 | +#include <spaced/game/asset_loader.hpp> |
| 3 | +#include <spaced/services/resources.hpp> |
| 4 | + |
| 5 | +namespace spaced { |
| 6 | +using bave::Loader; |
| 7 | + |
| 8 | +AssetList::AssetList(Loader loader, Services const& services) : m_loader(std::move(loader)), m_resources(&services.get<Resources>()) {} |
| 9 | + |
| 10 | +auto AssetList::add_texture(std::string uri, bool const mip_map) -> AssetList& { |
| 11 | + if (uri.empty()) { return *this; } |
| 12 | + m_textures.insert(Tex{.uri = std::move(uri), .mip_map = mip_map}); |
| 13 | + return *this; |
| 14 | +} |
| 15 | + |
| 16 | +auto AssetList::add_font(std::string uri) -> AssetList& { |
| 17 | + if (uri.empty()) { return *this; } |
| 18 | + m_fonts.insert(std::move(uri)); |
| 19 | + return *this; |
| 20 | +} |
| 21 | + |
| 22 | +auto AssetList::add_particle_emitter(std::string uri) -> AssetList& { |
| 23 | + if (uri.empty()) { return *this; } |
| 24 | + |
| 25 | + auto const json = m_loader.load_json(uri); |
| 26 | + if (!json) { return *this; } |
| 27 | + |
| 28 | + // emitters require textures (stage 0) to be loaded, and must be loaded in stage 1 |
| 29 | + if (auto const& texture = json["texture"]) { add_texture(texture.as<std::string>()); } |
| 30 | + m_emitters.insert(std::move(uri)); |
| 31 | + return *this; |
| 32 | +} |
| 33 | + |
| 34 | +auto AssetList::read_world_spec(std::string_view const uri) -> WorldSpec { |
| 35 | + if (uri.empty()) { return {}; } |
| 36 | + |
| 37 | + auto const json = m_loader.load_json(uri); |
| 38 | + if (!json) { return {}; } |
| 39 | + |
| 40 | + auto ret = WorldSpec{}; |
| 41 | + ret.name = json["name"].as_string(); |
| 42 | + ret.background_tint = json["background_tint"].as_string(); |
| 43 | + |
| 44 | + if (auto const& player = json["player"]) { |
| 45 | + ret.player.tint = player["tint"].as_string(); |
| 46 | + ret.player.exhaust_emitter = player["exhaust_emitter"].as_string(); |
| 47 | + add_particle_emitter(ret.player.exhaust_emitter); |
| 48 | + } |
| 49 | + |
| 50 | + for (auto const& enemy_factory : json["enemy_factories"].array_view()) { |
| 51 | + add_particle_emitter(enemy_factory["death_emitter"].as<std::string>()); |
| 52 | + ret.enemy_factories.push_back(enemy_factory); |
| 53 | + } |
| 54 | + |
| 55 | + return ret; |
| 56 | +} |
| 57 | + |
| 58 | +auto AssetList::build_task_stages() const -> std::vector<AsyncExec::Stage> { |
| 59 | + auto ret = std::vector<AsyncExec::Stage>{}; |
| 60 | + ret.reserve(2); |
| 61 | + auto asset_loader = AssetLoader{m_loader, m_resources}; |
| 62 | + ret.push_back(build_stage_0(asset_loader)); |
| 63 | + ret.push_back(build_stage_1(asset_loader)); |
| 64 | + return ret; |
| 65 | +} |
| 66 | + |
| 67 | +auto AssetList::build_stage_0(AssetLoader& asset_loader) const -> AsyncExec::Stage { |
| 68 | + auto ret = AsyncExec::Stage{}; |
| 69 | + for (auto const& texture : m_textures) { ret.push_back(asset_loader.make_load_texture(texture.uri, texture.mip_map)); } |
| 70 | + for (auto const& font : m_fonts) { ret.push_back(asset_loader.make_load_font(font)); } |
| 71 | + return ret; |
| 72 | +} |
| 73 | + |
| 74 | +auto AssetList::build_stage_1(AssetLoader& asset_loader) const -> AsyncExec::Stage { |
| 75 | + auto ret = AsyncExec::Stage{}; |
| 76 | + for (auto const& emitter : m_emitters) { ret.push_back(asset_loader.make_load_particle_emitter(emitter)); } |
| 77 | + return ret; |
| 78 | +} |
| 79 | +} // namespace spaced |
0 commit comments