💿🐜 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.

202 lines
6.3 KiB

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