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

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