💿🐜 Antkeeper source code https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

215 lines
6.9 KiB

  1. /*
  2. * Copyright (C) 2021 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. #include "resource-loader.hpp"
  20. #include "resource-manager.hpp"
  21. #include "string-table.hpp"
  22. #include "renderer/model.hpp"
  23. #include "entity/components/behavior.hpp"
  24. #include "entity/components/collision.hpp"
  25. #include "entity/components/terrain.hpp"
  26. #include "entity/components/transform.hpp"
  27. #include "entity/components/model.hpp"
  28. #include "entity/components/nest.hpp"
  29. #include "entity/components/marker.hpp"
  30. #include "entity/components/brush.hpp"
  31. #include "entity/archetype.hpp"
  32. #include "entity/ebt.hpp"
  33. #include <sstream>
  34. #include <stdexcept>
  35. static bool load_component_behavior(entity::archetype& archetype, resource_manager& resource_manager, const std::vector<std::string>& parameters)
  36. {
  37. if (parameters.size() != 2)
  38. {
  39. throw std::runtime_error("load_component_behavior(): Invalid parameter count.");
  40. }
  41. std::string filename = parameters[1];
  42. entity::component::behavior component;
  43. component.behavior_tree = resource_manager.load<entity::ebt::node>(filename);
  44. if (!component.behavior_tree)
  45. {
  46. std::string message = std::string("load_component_behavior(): Failed to load behavior tree \"") + filename + std::string("\"");
  47. throw std::runtime_error(message);
  48. }
  49. archetype.set<entity::component::behavior>(component);
  50. return true;
  51. }
  52. static bool load_component_collision(entity::archetype& archetype, resource_manager& resource_manager, const std::vector<std::string>& parameters)
  53. {
  54. if (parameters.size() != 2)
  55. {
  56. throw std::runtime_error("load_component_collision(): Invalid parameter count.");
  57. }
  58. std::string filename = parameters[1];
  59. entity::component::collision component;
  60. component.mesh = resource_manager.load<geom::mesh>(filename);
  61. if (!component.mesh)
  62. {
  63. std::string message = std::string("load_component_collision(): Failed to load model \"") + filename + std::string("\"");
  64. throw std::runtime_error(message);
  65. }
  66. archetype.set<entity::component::collision>(component);
  67. return true;
  68. }
  69. static bool load_component_model(entity::archetype& archetype, resource_manager& resource_manager, const std::vector<std::string>& parameters)
  70. {
  71. if (parameters.size() != 2)
  72. {
  73. throw std::runtime_error("load_component_model(): Invalid parameter count.");
  74. }
  75. std::string filename = parameters[1];
  76. entity::component::model component;
  77. component.render_model = resource_manager.load<model>(filename);
  78. component.instance_count = 0;
  79. component.layers = 1;
  80. if (!component.render_model)
  81. {
  82. std::string message = std::string("load_component_model(): Failed to load model \"") + filename + std::string("\"");
  83. throw std::runtime_error(message);
  84. }
  85. archetype.set<entity::component::model>(component);
  86. return true;
  87. }
  88. static bool load_component_nest(entity::archetype& archetype, const std::vector<std::string>& parameters)
  89. {
  90. entity::component::nest component;
  91. archetype.set<entity::component::nest>(component);
  92. return true;
  93. }
  94. static bool load_component_marker(entity::archetype& archetype, const std::vector<std::string>& parameters)
  95. {
  96. if (parameters.size() != 2)
  97. {
  98. throw std::runtime_error("load_component_marker(): Invalid parameter count.");
  99. }
  100. entity::component::marker component;
  101. component.color = std::stoi(parameters[1]);
  102. archetype.set<entity::component::marker>(component);
  103. return true;
  104. }
  105. static bool load_component_brush(entity::archetype& archetype, const std::vector<std::string>& parameters)
  106. {
  107. if (parameters.size() != 2)
  108. {
  109. throw std::runtime_error("load_component_brush(): Invalid parameter count.");
  110. }
  111. entity::component::brush component;
  112. component.radius = std::stof(parameters[1]);
  113. archetype.set<entity::component::brush>(component);
  114. return true;
  115. }
  116. static bool load_component_transform(entity::archetype& archetype, const std::vector<std::string>& parameters)
  117. {
  118. if (parameters.size() != 11)
  119. {
  120. throw std::runtime_error("load_component_transform(): Invalid parameter count.");
  121. }
  122. std::stringstream stream;
  123. for (std::size_t i = 1; i < parameters.size(); ++i)
  124. {
  125. stream << parameters[i];
  126. if (i < parameters.size() - 1)
  127. stream << ' ';
  128. }
  129. entity::component::transform component;
  130. stream >> component.local.translation.x;
  131. stream >> component.local.translation.y;
  132. stream >> component.local.translation.z;
  133. stream >> component.local.rotation.w;
  134. stream >> component.local.rotation.x;
  135. stream >> component.local.rotation.y;
  136. stream >> component.local.rotation.z;
  137. stream >> component.local.scale.x;
  138. stream >> component.local.scale.y;
  139. stream >> component.local.scale.z;
  140. component.warp = true;
  141. archetype.set<entity::component::transform>(component);
  142. return true;
  143. }
  144. static bool load_component(entity::archetype& archetype, resource_manager& resource_manager, const std::vector<std::string>& parameters)
  145. {
  146. if (parameters[0] == "behavior") return load_component_behavior(archetype, resource_manager, parameters);
  147. if (parameters[0] == "collision") return load_component_collision(archetype, resource_manager, parameters);
  148. if (parameters[0] == "model") return load_component_model(archetype, resource_manager, parameters);
  149. if (parameters[0] == "nest") return load_component_nest(archetype, parameters);
  150. if (parameters[0] == "transform") return load_component_transform(archetype, parameters);
  151. if (parameters[0] == "marker") return load_component_marker(archetype, parameters);
  152. if (parameters[0] == "brush") return load_component_brush(archetype, parameters);
  153. std::string message = std::string("load_component(): Unknown component type \"") + parameters[0] + std::string("\"");
  154. throw std::runtime_error(message);
  155. }
  156. template <>
  157. entity::archetype* resource_loader<entity::archetype>::load(resource_manager* resource_manager, PHYSFS_File* file)
  158. {
  159. entity::archetype* archetype = new entity::archetype(resource_manager->get_archetype_registry());
  160. // Load string table from input stream
  161. string_table* table = resource_loader<string_table>::load(resource_manager, file);
  162. // Ensure table is not empty.
  163. if (!table || table->empty())
  164. {
  165. delete table;
  166. return nullptr;
  167. }
  168. // Load components from table rows
  169. for (const string_table_row& row: *table)
  170. {
  171. // Skip empty rows and comments
  172. if (row.empty() || row[0].empty() || row[0][0] == '#')
  173. {
  174. continue;
  175. }
  176. load_component(*archetype, *resource_manager, row);
  177. }
  178. return archetype;
  179. }