Antkeeper  0.0.1
ocelli-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/ocelli.hpp"
25 #include <engine/render/model.hpp>
26 #include <stdexcept>
27 
28 using namespace ::ant;
29 
30 static void deserialize_ocelli_phene(phene::ocelli& phene, const json& phene_element, resource_manager* resource_manager)
31 {
32  phene.lateral_ocelli_present = false;
33  phene.median_ocellus_present = false;
34  phene.lateral_ocelli_model = nullptr;
35  phene.median_ocellus_model = nullptr;
36  phene.width = 0.0f;
37  phene.height = 0.0f;
38 
39  // Parse lateral ocelli present
40  if (auto element = phene_element.find("lateral_ocelli_present"); element != phene_element.end())
41  phene.lateral_ocelli_present = element->get<bool>();
42 
43  // Parse median ocelli present
44  if (auto element = phene_element.find("median_ocellus_present"); element != phene_element.end())
45  phene.median_ocellus_present = element->get<bool>();
46 
47  // Parse width
48  if (auto element = phene_element.find("width"); element != phene_element.end())
49  phene.width = element->get<float>();
50 
51  // Parse height
52  if (auto element = phene_element.find("height"); element != phene_element.end())
53  phene.height = element->get<float>();
54 
55  // Load lateral ocelli model, if present
56  if (phene.lateral_ocelli_present)
57  {
58  if (auto element = phene_element.find("lateral_ocelli_model"); element != phene_element.end())
59  phene.lateral_ocelli_model = resource_manager->load<render::model>(element->get<std::string>());
60  }
61 
62  // Load median ocellus model, if present
63  if (phene.median_ocellus_present)
64  {
65  if (auto element = phene_element.find("median_ocellus_model"); element != phene_element.end())
66  phene.median_ocellus_model = resource_manager->load<render::model>(element->get<std::string>());
67  }
68 }
69 
70 template <>
71 gene::ocelli* resource_loader<gene::ocelli>::load(resource_manager* resource_manager, PHYSFS_File* file, const std::filesystem::path& path)
72 {
73  // Load JSON data
75 
76  // Validate gene file
77  auto ocelli_element = data->find("ocelli");
78  if (ocelli_element == data->end())
79  throw std::runtime_error("Invalid ocelli gene.");
80 
81  // Allocate gene
83 
84  // Deserialize gene
85  gene::deserialize_gene(*ocelli, &deserialize_ocelli_phene, *ocelli_element, resource_manager);
86 
87  // Free JSON data
88  delete data;
89 
90  return ocelli;
91 }
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::ocelli > ocelli
Polyphenic ocelli gene.
Definition: gene/ocelli.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