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

264 lines
9.2 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 "application-states.hpp"
  20. #include "configuration.hpp"
  21. #include "application.hpp"
  22. #include "animation/screen-transition.hpp"
  23. #include "scene/model-instance.hpp"
  24. #include "resources/resource-manager.hpp"
  25. #include "renderer/model.hpp"
  26. #include "renderer/material.hpp"
  27. #include "renderer/passes/sky-pass.hpp"
  28. #include "systems/control-system.hpp"
  29. #include "entity/components/model-component.hpp"
  30. #include "entity/components/transform-component.hpp"
  31. #include "entity/components/terrain-component.hpp"
  32. #include "entity/components/samara-component.hpp"
  33. #include "entity/components/cavity-component.hpp"
  34. #include "entity/components/tool-component.hpp"
  35. #include "entity/components/placement-component.hpp"
  36. #include "entity/components/copy-transform-component.hpp"
  37. #include "entity/components/copy-translation-component.hpp"
  38. #include "entity/archetype.hpp"
  39. #include "nest.hpp"
  40. #include "math.hpp"
  41. #include "geometry/mesh-accelerator.hpp"
  42. #include "behavior/ebt.hpp"
  43. #include "animation/ease.hpp"
  44. #include <iostream>
  45. using namespace vmq::operators;
  46. void enter_play_state(application* app)
  47. {
  48. logger* logger = app->get_logger();
  49. logger->push_task("Entering play state");
  50. // Enable sky pass
  51. app->get_sky_pass()->set_enabled(true);
  52. resource_manager* resource_manager = app->get_resource_manager();
  53. entt::registry& ecs_registry = app->get_ecs_registry();
  54. // Load entity archetypes
  55. ecs::archetype* ant_hill_archetype = resource_manager->load<ecs::archetype>("ant-hill.ent");
  56. ecs::archetype* maple_tree_archetype = resource_manager->load<ecs::archetype>("maple-tree.ent");
  57. ecs::archetype* nest_archetype = resource_manager->load<ecs::archetype>("harvester-nest.ent");
  58. ecs::archetype* samara_archetype = resource_manager->load<ecs::archetype>("samara.ent");
  59. ecs::archetype* forceps_archetype = resource_manager->load<ecs::archetype>("forceps.ent");
  60. ecs::archetype* larva_archetype = resource_manager->load<ecs::archetype>("larva.ent");
  61. ecs::archetype* pebble_archetype = resource_manager->load<ecs::archetype>("pebble.ent");
  62. ecs::placement_component placement;
  63. auto ant_hill_entity = ant_hill_archetype->create(ecs_registry);
  64. placement.ray.origin = {0, 10000, 0};
  65. placement.ray.direction = {0, -1, 0};
  66. ecs_registry.assign<ecs::placement_component>(ant_hill_entity, placement);
  67. float pebble_radius = 300.0f;
  68. int pebble_count = 100;
  69. for (int i = 0; i < pebble_count; ++i)
  70. {
  71. float x = frand(-pebble_radius, pebble_radius);
  72. float z = frand(-pebble_radius, pebble_radius);
  73. auto pebble_entity = pebble_archetype->create(ecs_registry);
  74. auto& transform = ecs_registry.get<ecs::transform_component>(pebble_entity);
  75. transform.transform = vmq::identity_transform<float>;
  76. transform.transform.rotation = vmq::angle_axis(frand(0.0f, vmq::two_pi<float>), {0, 1, 0});
  77. transform.transform.scale = float3{1, 1, 1} * frand(0.75f, 1.25f);
  78. placement.ray.origin = {x, 10000, z};
  79. ecs_registry.assign<ecs::placement_component>(pebble_entity, placement);
  80. }
  81. auto maple_tree_entity = maple_tree_archetype->create(ecs_registry);
  82. placement.ray.origin = {300, 10000, 200};
  83. placement.ray.direction = {0, -1, 0};
  84. ecs_registry.assign<ecs::placement_component>(maple_tree_entity, placement);
  85. auto nest_entity = nest_archetype->create(ecs_registry);
  86. int terrain_radius = 2;
  87. for (int x = -terrain_radius; x <= terrain_radius; ++x)
  88. {
  89. for (int z = -terrain_radius; z <= terrain_radius; ++z)
  90. {
  91. ecs::terrain_component terrain_component;
  92. terrain_component.subdivisions = TERRAIN_PATCH_RESOLUTION;
  93. terrain_component.x = x;
  94. terrain_component.z = z;
  95. auto terrain_entity = ecs_registry.create();
  96. ecs_registry.assign<ecs::terrain_component>(terrain_entity, terrain_component);
  97. }
  98. }
  99. for (int i = 0; i < 15; ++i)
  100. {
  101. auto samara_entity = samara_archetype->create(ecs_registry);
  102. auto& transform = ecs_registry.get<ecs::transform_component>(samara_entity);
  103. float zone = 200.0f;
  104. transform.transform = vmq::identity_transform<float>;
  105. transform.transform.translation.x = frand(-zone, zone);
  106. transform.transform.translation.y = frand(50.0f, 150.0f);
  107. transform.transform.translation.z = frand(-zone, zone);
  108. ecs::samara_component samara_component;
  109. samara_component.angle = frand(0.0f, vmq::radians(360.0f));
  110. samara_component.direction = vmq::normalize(float3{frand(-1, 1), frand(-1, -5), frand(-1, 1)});
  111. samara_component.chirality = (frand(0, 1) < 0.5f) ? -1.0f : 1.0f;
  112. ecs_registry.assign_or_replace<ecs::samara_component>(samara_entity, samara_component);
  113. }
  114. /*
  115. ecs::archetype* grass_archetype = resource_manager->load<ecs::archetype>("grassland-grass.ent");
  116. auto grass_entity_1 = grass_archetype->create(ecs_registry);
  117. auto grass_entity_2 = grass_archetype->create(ecs_registry);
  118. ecs_registry.get<ecs::transform_component>(grass_entity_2).transform.rotation = vmq::angle_axis(vmq::radians(120.0f), float3{0, 1, 0});
  119. */
  120. // Setup overworld camera
  121. camera* camera = app->get_overworld_camera();
  122. orbit_cam* orbit_cam = app->get_orbit_cam();
  123. orbit_cam->attach(camera);
  124. orbit_cam->set_target_focal_point({0, 0, 0});
  125. orbit_cam->set_target_focal_distance(15.0f);
  126. orbit_cam->set_target_elevation(vmq::radians(25.0f));
  127. orbit_cam->set_target_azimuth(0.0f);
  128. orbit_cam->set_focal_point(orbit_cam->get_target_focal_point());
  129. orbit_cam->set_focal_distance(orbit_cam->get_target_focal_distance());
  130. orbit_cam->set_elevation(orbit_cam->get_target_elevation());
  131. orbit_cam->set_azimuth(orbit_cam->get_target_azimuth());
  132. // Create forceps tool
  133. auto forceps_entity = forceps_archetype->create(ecs_registry);
  134. ecs::tool_component forceps_tool_component;
  135. forceps_tool_component.active = true;
  136. ecs_registry.assign<ecs::tool_component>(forceps_entity, forceps_tool_component);
  137. // Add copy transform constraint to ant-hill
  138. ecs::copy_translation_component constraint;
  139. constraint.target = forceps_entity;
  140. constraint.use_x = true;
  141. constraint.use_y = true;
  142. constraint.use_z = true;
  143. constraint.invert_x = true;
  144. constraint.invert_y = false;
  145. constraint.invert_z = true;
  146. ecs_registry.assign<ecs::copy_translation_component>(ant_hill_entity, constraint);
  147. app->get_scene().update_tweens();
  148. // Allocate a nest
  149. nest* nest = new ::nest();
  150. // Setup initial nest parameters
  151. float tunnel_radius = 1.15f;
  152. nest->set_tunnel_radius(tunnel_radius);
  153. nest::shaft* central_shaft = nest->get_central_shaft();
  154. central_shaft->chirality = 1.0f;
  155. central_shaft->rotation = vmq::radians(0.0f);
  156. central_shaft->depth = {0.0f, 200.0f};
  157. central_shaft->radius = {15.0f, 15.0f};
  158. central_shaft->pitch = {40.0f, 40.0f};
  159. central_shaft->translation = {{{0.0f, 0.0f}, {0.0f, 0.0f}}};
  160. central_shaft->current_depth = 0.0f;
  161. for (std::size_t i = 0; i < 4; ++i)
  162. {
  163. nest::chamber chamber;
  164. chamber.shaft = central_shaft;
  165. chamber.depth = (i + 1) * 50.0f;
  166. chamber.rotation = vmq::radians(0.0f);
  167. chamber.inner_radius = 4.0f;
  168. chamber.outer_radius = 10.0f;
  169. central_shaft->chambers.push_back(chamber);
  170. }
  171. // Dig nest shafts
  172. float shift = 0.1f;
  173. for (int i = 0; i < 800; ++i)
  174. {
  175. ecs::cavity_component cavity;
  176. cavity.position = nest->extend_shaft(*nest->get_central_shaft());
  177. cavity.position += float3{frand(-shift, shift), frand(-shift, shift), frand(-shift, shift)};
  178. cavity.radius = tunnel_radius * frand(1.0f, 1.1f);
  179. ecs_registry.assign<ecs::cavity_component>(ecs_registry.create(), cavity);
  180. }
  181. // Dig nest chambers
  182. /*
  183. for (int i = 0; i < central_shaft->chambers.size(); ++i)
  184. {
  185. for (int j = 0; j < 150; ++j)
  186. {
  187. ecs::cavity_component cavity;
  188. cavity.position = nest->expand_chamber(central_shaft->chambers[i]);
  189. cavity.position += float3{frand(-shift, shift), frand(-shift, shift), frand(-shift, shift)};
  190. cavity.radius = tunnel_radius * frand(1.0f, 1.1f);
  191. ecs_registry.assign<ecs::cavity_component>(ecs_registry.create(), cavity);
  192. }
  193. }
  194. */
  195. // Place larva in chamber
  196. /*
  197. {
  198. auto larva_entity = larva_archetype->create(ecs_registry);
  199. auto& transform = ecs_registry.get<ecs::transform_component>(larva_entity);
  200. transform.transform = vmq::identity_transform<float>;
  201. transform.transform.translation = nest->get_shaft_position(*central_shaft, central_shaft->depth[1]);
  202. //transform.transform.translation.y -= 1.0f;
  203. }
  204. */
  205. control_system* control_system = app->get_control_system();
  206. control_system->update(0.0f);
  207. control_system->set_nest(nest);
  208. orbit_cam->update(0.0f);
  209. // Start fade in
  210. app->get_fade_transition()->transition(1.0f, true, ease<float>::in_quad);
  211. logger->pop_task(EXIT_SUCCESS);
  212. }
  213. void exit_play_state(application* app)
  214. {
  215. logger* logger = app->get_logger();
  216. logger->push_task("Exiting play state");
  217. logger->pop_task(EXIT_SUCCESS);
  218. }