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

201 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. if (!component.model)
  79. {
  80. std::string message = std::string("load_model_component(): Failed to load model \"") + filename + std::string("\"");
  81. throw std::runtime_error(message);
  82. }
  83. archetype.set<model_component>(component);
  84. return true;
  85. }
  86. static bool load_nest_component(archetype& archetype, const std::vector<std::string>& parameters)
  87. {
  88. nest_component component;
  89. archetype.set<nest_component>(component);
  90. return true;
  91. }
  92. static bool load_terrain_component(archetype& archetype, const std::vector<std::string>& parameters)
  93. {
  94. if (parameters.size() != 4)
  95. {
  96. throw std::runtime_error("load_terrain_component(): Invalid parameter count.");
  97. }
  98. terrain_component component;
  99. component.subdivisions = std::stoi(parameters[1]);
  100. component.x = std::stoi(parameters[2]);
  101. component.z = std::stoi(parameters[3]);
  102. archetype.set<terrain_component>(component);
  103. return true;
  104. }
  105. static bool load_transform_component(archetype& archetype, const std::vector<std::string>& parameters)
  106. {
  107. if (parameters.size() != 11)
  108. {
  109. throw std::runtime_error("load_transform_component(): Invalid parameter count.");
  110. }
  111. std::stringstream stream;
  112. for (std::size_t i = 1; i < parameters.size(); ++i)
  113. {
  114. stream << parameters[i];
  115. if (i < parameters.size() - 1)
  116. stream << ' ';
  117. }
  118. transform_component component;
  119. stream >> component.transform.translation.x;
  120. stream >> component.transform.translation.y;
  121. stream >> component.transform.translation.z;
  122. stream >> component.transform.rotation.w;
  123. stream >> component.transform.rotation.x;
  124. stream >> component.transform.rotation.y;
  125. stream >> component.transform.rotation.z;
  126. stream >> component.transform.scale.x;
  127. stream >> component.transform.scale.y;
  128. stream >> component.transform.scale.z;
  129. component.warp = true;
  130. archetype.set<transform_component>(component);
  131. return true;
  132. }
  133. static bool load_component(archetype& archetype, resource_manager& resource_manager, const std::vector<std::string>& parameters)
  134. {
  135. if (parameters[0] == "behavior") return load_behavior_component(archetype, resource_manager, parameters);
  136. if (parameters[0] == "collision") return load_collision_component(archetype, resource_manager, parameters);
  137. if (parameters[0] == "model") return load_model_component(archetype, resource_manager, parameters);
  138. if (parameters[0] == "nest") return load_nest_component(archetype, parameters);
  139. if (parameters[0] == "terrain") return load_terrain_component(archetype, parameters);
  140. if (parameters[0] == "transform") return load_transform_component(archetype, parameters);
  141. std::string message = std::string("load_component(): Unknown component type \"") + parameters[0] + std::string("\"");
  142. throw std::runtime_error(message);
  143. }
  144. template <>
  145. archetype* resource_loader<archetype>::load(resource_manager* resource_manager, PHYSFS_File* file)
  146. {
  147. ecs::archetype* archetype = new ecs::archetype(resource_manager->get_archetype_registry());
  148. // Load string table from input stream
  149. string_table* table = resource_loader<string_table>::load(resource_manager, file);
  150. // Ensure table is not empty.
  151. if (!table || table->empty())
  152. {
  153. delete table;
  154. return nullptr;
  155. }
  156. // Load components from table rows
  157. for (const string_table_row& row: *table)
  158. {
  159. // Skip empty rows and comments
  160. if (row.empty() || row[0].empty() || row[0][0] == '#')
  161. {
  162. continue;
  163. }
  164. load_component(*archetype, *resource_manager, row);
  165. }
  166. return archetype;
  167. }