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

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