Antkeeper  0.0.1
wings-loader.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2023 Christopher J. Howard
3  *
4  * This file is part of Antkeeper source code.
5  *
6  * Antkeeper source code is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * Antkeeper source code is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with Antkeeper source code. If not, see <http://www.gnu.org/licenses/>.
18  */
19 
24 #include "game/ant/gene/wings.hpp"
25 #include <engine/render/model.hpp>
26 #include <stdexcept>
27 
28 using namespace ::ant;
29 
30 static void deserialize_wings_phene(phene::wings& phene, const json& phene_element, resource_manager* resource_manager)
31 {
32  phene.present = false;
33  phene.forewings_model = nullptr;
34  phene.hindwings_model = nullptr;
35  phene.forewing_length = 0.0f;
36  phene.forewing_width = 0.0f;
37  phene.forewing_venation = 0.0f;
38  phene.hindwing_length = 0.0f;
39  phene.hindwing_width = 0.0f;
40  phene.hindwing_venation = 0.0f;
41 
42  // Parse present
43  if (auto element = phene_element.find("present"); element != phene_element.end())
44  phene.present = element->get<bool>();
45 
46  if (phene.present)
47  {
48  // Load forewings model
49  if (auto element = phene_element.find("forewings_model"); element != phene_element.end())
50  phene.forewings_model = resource_manager->load<render::model>(element->get<std::string>());
51 
52  // Load hindwings model
53  if (auto element = phene_element.find("hindwings_model"); element != phene_element.end())
54  phene.hindwings_model = resource_manager->load<render::model>(element->get<std::string>());
55 
56  // Parse forewing length
57  if (auto element = phene_element.find("forewing_length"); element != phene_element.end())
58  phene.forewing_length = element->get<float>();
59 
60  // Parse forewing width
61  if (auto element = phene_element.find("forewing_width"); element != phene_element.end())
62  phene.forewing_width = element->get<float>();
63 
64  // Parse forewing venation
65  if (auto element = phene_element.find("forewing_venation"); element != phene_element.end())
66  phene.forewing_venation = element->get<float>();
67 
68  // Parse hindwing length
69  if (auto element = phene_element.find("hindwing_length"); element != phene_element.end())
70  phene.hindwing_length = element->get<float>();
71 
72  // Parse hindwing width
73  if (auto element = phene_element.find("hindwing_width"); element != phene_element.end())
74  phene.hindwing_width = element->get<float>();
75 
76  // Parse hindwing venation
77  if (auto element = phene_element.find("hindwing_venation"); element != phene_element.end())
78  phene.hindwing_venation = element->get<float>();
79  }
80 }
81 
82 template <>
83 gene::wings* resource_loader<gene::wings>::load(resource_manager* resource_manager, PHYSFS_File* file, const std::filesystem::path& path)
84 {
85  // Load JSON data
87 
88  // Validate gene file
89  auto wings_element = data->find("wings");
90  if (wings_element == data->end())
91  throw std::runtime_error("Invalid wings gene.");
92 
93  // Allocate gene
94  gene::wings* wings = new gene::wings();
95 
96  // Deserialize gene
97  gene::deserialize_gene(*wings, &deserialize_wings_phene, *wings_element, resource_manager);
98 
99  // Free JSON data
100  delete data;
101 
102  return wings;
103 }
static T * load(resource_manager *resourceManager, PHYSFS_File *file, const std::filesystem::path &path)
Loads resource data.
Loads resources.
T * load(const std::filesystem::path &path)
Loads the requested resource.
nlohmann::json json
JSON data.
Definition: json.hpp:26
polyphenic_gene< phene::wings > wings
Polyphenic wings gene.
Definition: gene/wings.hpp:30
void deserialize_gene(monophenic_gene< T > &gene, void(*deserialize_phene)(T &, const json &, resource_manager *), const json &gene_element, resource_manager *resource_manager)
Deserializes a gene.
Definition: gene-loader.hpp:43
Definition: caste.hpp:25