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

292 lines
11 KiB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
  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/snap-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 "game/components/camera-follow-component.hpp"
  34. #include "game/entity-commands.hpp"
  35. #include "game/game-context.hpp"
  36. #include "game/states/game-states.hpp"
  37. #include "math/math.hpp"
  38. #include "nest.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 "scene/camera.hpp"
  46. #include "scene/directional-light.hpp"
  47. #include "game/systems/control-system.hpp"
  48. #include "game/systems/camera-system.hpp"
  49. #include "game/systems/render-system.hpp"
  50. #include "game/systems/tool-system.hpp"
  51. #include "utility/fundamental-types.hpp"
  52. #include "utility/gamma.hpp"
  53. void play_state_enter(game_context* ctx)
  54. {
  55. logger* logger = ctx->logger;
  56. logger->push_task("Entering play state");
  57. // Set up sky pass
  58. sky_pass* sky_pass = ctx->overworld_sky_pass;
  59. sky_pass->set_enabled(true);
  60. sky_pass->set_sun_angular_radius(math::radians<float>(3.0f));
  61. sky_pass->set_sun_color({2.0f, 2.0f, 2.0f});
  62. sky_pass->set_horizon_color(to_linear(float3{81.0f, 162.0f, 219.0f} / 255.0f));
  63. sky_pass->set_zenith_color(to_linear(float3{7.0f, 134.0f, 206.0f} / 255.0f));
  64. //sky_pass->set_horizon_color(float3{0.002f, 0.158f, 0.250f});
  65. //sky_pass->set_zenith_color(float3{0.002f, 0.158f, 0.250f});
  66. ctx->tool_system->set_sun_direction(ctx->sun_direct->get_direction());
  67. resource_manager* resource_manager = ctx->resource_manager;
  68. entt::registry& ecs_registry = *ctx->ecs_registry;
  69. // Load entity archetypes
  70. ecs::archetype* ant_hill_archetype = resource_manager->load<ecs::archetype>("ant-hill.ent");
  71. ecs::archetype* maple_tree_archetype = resource_manager->load<ecs::archetype>("maple-tree.ent");
  72. ecs::archetype* nest_archetype = resource_manager->load<ecs::archetype>("harvester-nest.ent");
  73. ecs::archetype* samara_archetype = resource_manager->load<ecs::archetype>("samara.ent");
  74. ecs::archetype* forceps_archetype = resource_manager->load<ecs::archetype>("forceps.ent");
  75. ecs::archetype* lens_archetype = resource_manager->load<ecs::archetype>("lens.ent");
  76. ecs::archetype* brush_archetype = resource_manager->load<ecs::archetype>("brush.ent");
  77. ecs::archetype* marker_archetype = resource_manager->load<ecs::archetype>("marker.ent");
  78. ecs::archetype* larva_archetype = resource_manager->load<ecs::archetype>("larva.ent");
  79. ecs::archetype* pebble_archetype = resource_manager->load<ecs::archetype>("pebble.ent");
  80. ecs::archetype* flashlight_archetype = resource_manager->load<ecs::archetype>("flashlight.ent");
  81. ecs::archetype* flashlight_light_cone_archetype = resource_manager->load<ecs::archetype>("flashlight-light-cone.ent");
  82. ecs::archetype* lens_light_cone_archetype = resource_manager->load<ecs::archetype>("lens-light-cone.ent");
  83. // Create tools
  84. forceps_archetype->assign(ecs_registry, ctx->forceps_entity);
  85. lens_archetype->assign(ecs_registry, ctx->lens_entity);
  86. brush_archetype->assign(ecs_registry, ctx->brush_entity);
  87. marker_archetype->assign(ecs_registry, ctx->marker_entity);
  88. // Create flashlight and light cone, set light cone parent to flashlight, and move both to underworld scene
  89. flashlight_archetype->assign(ecs_registry, ctx->flashlight_entity);
  90. auto flashlight_light_cone = flashlight_light_cone_archetype->create(ecs_registry);
  91. ec::parent(ecs_registry, flashlight_light_cone, ctx->flashlight_entity);
  92. ec::assign_render_layers(ecs_registry, ctx->flashlight_entity, 2);
  93. // Make lens tool's model instance unculled, so its shadow is always visible.
  94. model_instance* lens_model_instance = ctx->render_system->get_model_instance(ctx->lens_entity);
  95. if (lens_model_instance)
  96. {
  97. lens_model_instance->set_culling_mask(&ctx->no_cull);
  98. }
  99. // Create lens light cone and set its parent to lens
  100. auto lens_light_cone = lens_light_cone_archetype->create(ecs_registry);
  101. //ec::bind_transform(ecs_registry, lens_light_cone, ctx->lens_entity);
  102. ec::parent(ecs_registry, lens_light_cone, ctx->lens_entity);
  103. // Hide inactive tools
  104. ec::assign_render_layers(ecs_registry, ctx->forceps_entity, 0);
  105. ec::assign_render_layers(ecs_registry, ctx->brush_entity, 0);
  106. ec::assign_render_layers(ecs_registry, ctx->lens_entity, 0);
  107. ec::assign_render_layers(ecs_registry, ctx->marker_entity, 0);
  108. // Activate lens tool
  109. ctx->tool_system->set_active_tool(ctx->lens_entity);
  110. // Create ant-hill
  111. auto ant_hill_entity = ant_hill_archetype->create(ecs_registry);
  112. ec::place(ecs_registry, ant_hill_entity, {0, 0});
  113. // Generate pebbles
  114. float pebble_radius = 300.0f;
  115. int pebble_count = 100;
  116. for (int i = 0; i < pebble_count; ++i)
  117. {
  118. float x = math::random(-pebble_radius, pebble_radius);
  119. float z = math::random(-pebble_radius, pebble_radius);
  120. auto pebble_entity = pebble_archetype->create(ecs_registry);
  121. auto& transform = ecs_registry.get<ecs::transform_component>(pebble_entity);
  122. transform.local = math::identity_transform<float>;
  123. transform.local.rotation = math::angle_axis(math::random(0.0f, math::two_pi<float>), {0, 1, 0});
  124. transform.local.scale = float3{1, 1, 1} * math::random(0.75f, 1.25f);
  125. ec::place(ecs_registry, pebble_entity, {x, z});
  126. }
  127. // Create maple tree
  128. auto maple_tree_entity = maple_tree_archetype->create(ecs_registry);
  129. ec::place(ecs_registry, maple_tree_entity, {300, 200});
  130. // Creat nest
  131. auto nest_entity = nest_archetype->create(ecs_registry);
  132. // Create terrain
  133. int terrain_radius = 4;
  134. for (int x = -terrain_radius; x <= terrain_radius; ++x)
  135. {
  136. for (int z = -terrain_radius; z <= terrain_radius; ++z)
  137. {
  138. ecs::terrain_component terrain_component;
  139. terrain_component.subdivisions = TERRAIN_PATCH_RESOLUTION;
  140. terrain_component.x = x;
  141. terrain_component.z = z;
  142. auto terrain_entity = ecs_registry.create();
  143. ecs_registry.assign<ecs::terrain_component>(terrain_entity, terrain_component);
  144. }
  145. }
  146. // Create samaras
  147. for (int i = 0; i < 15; ++i)
  148. {
  149. auto samara_entity = samara_archetype->create(ecs_registry);
  150. auto& transform = ecs_registry.get<ecs::transform_component>(samara_entity);
  151. float zone = 200.0f;
  152. transform.local = math::identity_transform<float>;
  153. transform.local.translation.x = math::random(-zone, zone);
  154. transform.local.translation.y = math::random(50.0f, 150.0f);
  155. transform.local.translation.z = math::random(-zone, zone);
  156. ecs::samara_component samara_component;
  157. samara_component.angle = math::random(0.0f, math::radians(360.0f));
  158. samara_component.direction = math::normalize(float3{math::random(-1.0f, 1.0f), math::random(-1.0f, -5.0f), math::random(-1.0f, 1.0f)});
  159. samara_component.chirality = (math::random(0.0f, 1.0f) < 0.5f) ? -1.0f : 1.0f;
  160. ecs_registry.assign_or_replace<ecs::samara_component>(samara_entity, samara_component);
  161. }
  162. // Setup camera focal point
  163. ecs::transform_component focal_point_transform;
  164. focal_point_transform.local = math::identity_transform<float>;
  165. focal_point_transform.warp = true;
  166. ecs::camera_follow_component focal_point_follow;
  167. ecs::snap_component focal_point_snap;
  168. focal_point_snap.ray = {float3{0, 10000, 0}, float3{0, -1, 0}};
  169. focal_point_snap.warp = false;
  170. focal_point_snap.relative = true;
  171. focal_point_snap.autoremove = false;
  172. ecs_registry.assign_or_replace<ecs::transform_component>(ctx->focal_point_entity, focal_point_transform);
  173. ecs_registry.assign_or_replace<ecs::camera_follow_component>(ctx->focal_point_entity, focal_point_follow);
  174. ecs_registry.assign_or_replace<ecs::snap_component>(ctx->focal_point_entity, focal_point_snap);
  175. // Setup camera
  176. ctx->overworld_camera->look_at({0, 0, 1}, {0, 0, 0}, {0, 1, 0});
  177. ctx->camera_system->set_camera(ctx->overworld_camera);
  178. ctx->overworld_scene->update_tweens();
  179. // Allocate a nest
  180. nest* nest = new ::nest();
  181. // Setup initial nest parameters
  182. float tunnel_radius = 1.15f;
  183. nest->set_tunnel_radius(tunnel_radius);
  184. nest::shaft* central_shaft = nest->get_central_shaft();
  185. central_shaft->chirality = 1.0f;
  186. central_shaft->rotation = math::radians(0.0f);
  187. central_shaft->depth = {0.0f, 200.0f};
  188. central_shaft->radius = {15.0f, 15.0f};
  189. central_shaft->pitch = {40.0f, 40.0f};
  190. central_shaft->translation = {{{0.0f, 0.0f}, {0.0f, 0.0f}}};
  191. central_shaft->current_depth = 0.0f;
  192. for (std::size_t i = 0; i < 4; ++i)
  193. {
  194. nest::chamber chamber;
  195. chamber.shaft = central_shaft;
  196. chamber.depth = (i + 1) * 50.0f;
  197. chamber.rotation = math::radians(0.0f);
  198. chamber.inner_radius = 4.0f;
  199. chamber.outer_radius = 10.0f;
  200. central_shaft->chambers.push_back(chamber);
  201. }
  202. // Dig nest shafts
  203. float shift = 0.1f;
  204. for (int i = 0; i < 800; ++i)
  205. {
  206. ecs::cavity_component cavity;
  207. cavity.position = nest->extend_shaft(*nest->get_central_shaft());
  208. cavity.position += float3{math::random(-shift, shift), math::random(-shift, shift), math::random(-shift, shift)};
  209. cavity.radius = tunnel_radius * math::random(1.0f, 1.1f);
  210. ecs_registry.assign<ecs::cavity_component>(ecs_registry.create(), cavity);
  211. }
  212. // Dig nest chambers
  213. /*
  214. for (int i = 0; i < central_shaft->chambers.size(); ++i)
  215. {
  216. for (int j = 0; j < 150; ++j)
  217. {
  218. ecs::cavity_component cavity;
  219. cavity.position = nest->expand_chamber(central_shaft->chambers[i]);
  220. cavity.position += float3{math::random(-shift, shift), math::random(-shift, shift), math::random(-shift, shift)};
  221. cavity.radius = tunnel_radius * math::random(1.0f, 1.1f);
  222. ecs_registry.assign<ecs::cavity_component>(ecs_registry.create(), cavity);
  223. }
  224. }
  225. */
  226. // Place larva in chamber
  227. {
  228. auto larva = larva_archetype->create(ecs_registry);
  229. ec::assign_render_layers(ecs_registry, larva, 1);
  230. //ecs::warp_to(ecs_registry, larva, {0, -20, 0});
  231. //auto& transform = ecs_registry.get<ecs::transform_component>(larva_entity);
  232. //transform.transform = math::identity_transform<float>;
  233. //transform.transform.translation = nest->get_shaft_position(*central_shaft, central_shaft->depth[1]);
  234. //transform.transform.translation.y -= 1.0f;
  235. }
  236. control_system* control_system = ctx->control_system;
  237. control_system->update(0.0, 0.0);
  238. control_system->set_nest(nest);
  239. // Start fade in
  240. ctx->fade_transition->transition(1.0f, true, ease<float>::in_quad);
  241. logger->pop_task(EXIT_SUCCESS);
  242. }
  243. void play_state_exit(game_context* ctx)
  244. {
  245. logger* logger = ctx->logger;
  246. logger->push_task("Exiting play state");
  247. logger->pop_task(EXIT_SUCCESS);
  248. }