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

338 lines
14 KiB

3 years ago
3 years ago
3 years ago
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 "rasterizer/texture-2d.hpp"
  41. #include "rasterizer/texture-filter.hpp"
  42. #include "rasterizer/texture-wrapping.hpp"
  43. #include "renderer/model.hpp"
  44. #include "renderer/passes/sky-pass.hpp"
  45. #include "resources/resource-manager.hpp"
  46. #include "scene/model-instance.hpp"
  47. #include "scene/scene.hpp"
  48. #include "scene/camera.hpp"
  49. #include "scene/ambient-light.hpp"
  50. #include "scene/directional-light.hpp"
  51. #include "scene/directional-light.hpp"
  52. #include "game/systems/control-system.hpp"
  53. #include "game/systems/camera-system.hpp"
  54. #include "game/systems/render-system.hpp"
  55. #include "game/systems/tool-system.hpp"
  56. #include "game/systems/weather-system.hpp"
  57. #include "game/biome.hpp"
  58. #include "utility/fundamental-types.hpp"
  59. #include "utility/gamma.hpp"
  60. void play_state_enter(game_context* ctx)
  61. {
  62. logger* logger = ctx->logger;
  63. logger->push_task("Entering play state");
  64. // Load biome
  65. if (ctx->option_biome.has_value())
  66. {
  67. ctx->biome = ctx->resource_manager->load<biome>(ctx->option_biome.value() + ".bio");
  68. }
  69. else
  70. {
  71. ctx->biome = ctx->resource_manager->load<biome>("grassland.bio");
  72. }
  73. // Apply biome parameters to scene
  74. ctx->sun_indirect->set_color(ctx->biome->ambient_color);
  75. ctx->sun_indirect->set_intensity(ctx->biome->ambient_intensity);
  76. math::quaternion<float> sun_azimuth_rotation = math::angle_axis(ctx->biome->sun_azimuth, float3{0, 1, 0});
  77. math::quaternion<float> sun_elevation_rotation = math::angle_axis(ctx->biome->sun_elevation, float3{-1, 0, 0});
  78. math::quaternion<float> sun_rotation = math::normalize(sun_azimuth_rotation * sun_elevation_rotation);
  79. ctx->sun_direct->set_rotation(sun_rotation);
  80. ctx->sun_direct->set_color(ctx->biome->sun_color);
  81. ctx->sun_direct->set_intensity(ctx->biome->sun_intensity);
  82. sky_pass* sky_pass = ctx->overworld_sky_pass;
  83. sky_pass->set_enabled(true);
  84. sky_pass->set_sun_angular_radius(ctx->biome->sun_angular_radius);
  85. sky_pass->set_sun_color(ctx->biome->sun_color * ctx->biome->sun_intensity);
  86. sky_pass->set_horizon_color(ctx->biome->horizon_color);
  87. sky_pass->set_zenith_color(ctx->biome->zenith_color);
  88. texture_2d* sky_palette = ctx->resource_manager->load<texture_2d>("sky-palette.png");
  89. sky_palette->set_wrapping(texture_wrapping::clamp, texture_wrapping::clamp);
  90. sky_palette->set_filters(texture_min_filter::nearest, texture_mag_filter::nearest);
  91. sky_pass->set_sky_palette(sky_palette);
  92. ctx->weather_system->set_time_of_day(6.0f * 60.0f * 60.0f);
  93. resource_manager* resource_manager = ctx->resource_manager;
  94. entt::registry& ecs_registry = *ctx->ecs_registry;
  95. // Load entity archetypes
  96. ecs::archetype* ant_hill_archetype = resource_manager->load<ecs::archetype>("ant-hill.ent");
  97. ecs::archetype* maple_tree_archetype = resource_manager->load<ecs::archetype>("maple-tree.ent");
  98. ecs::archetype* nest_archetype = resource_manager->load<ecs::archetype>("harvester-nest.ent");
  99. ecs::archetype* samara_archetype = resource_manager->load<ecs::archetype>("samara.ent");
  100. ecs::archetype* forceps_archetype = resource_manager->load<ecs::archetype>("forceps.ent");
  101. ecs::archetype* lens_archetype = resource_manager->load<ecs::archetype>("lens.ent");
  102. ecs::archetype* brush_archetype = resource_manager->load<ecs::archetype>("brush.ent");
  103. ecs::archetype* marker_archetype = resource_manager->load<ecs::archetype>("marker.ent");
  104. ecs::archetype* container_archetype = resource_manager->load<ecs::archetype>("container.ent");
  105. ecs::archetype* twig_archetype = resource_manager->load<ecs::archetype>("twig.ent");
  106. ecs::archetype* larva_archetype = resource_manager->load<ecs::archetype>("larva.ent");
  107. ecs::archetype* pebble_archetype = resource_manager->load<ecs::archetype>("pebble.ent");
  108. ecs::archetype* flashlight_archetype = resource_manager->load<ecs::archetype>("flashlight.ent");
  109. ecs::archetype* flashlight_light_cone_archetype = resource_manager->load<ecs::archetype>("flashlight-light-cone.ent");
  110. ecs::archetype* lens_light_cone_archetype = resource_manager->load<ecs::archetype>("lens-light-cone.ent");
  111. ecs::archetype* ant_head_archetype = resource_manager->load<ecs::archetype>("ant-head.ent");
  112. ecs::archetype* dandelion_plant_archetype = resource_manager->load<ecs::archetype>("dandelion-plant.ent");
  113. // Create tools
  114. forceps_archetype->assign(ecs_registry, ctx->forceps_entity);
  115. lens_archetype->assign(ecs_registry, ctx->lens_entity);
  116. brush_archetype->assign(ecs_registry, ctx->brush_entity);
  117. marker_archetype->assign(ecs_registry, ctx->marker_entity);
  118. container_archetype->assign(ecs_registry, ctx->container_entity);
  119. twig_archetype->assign(ecs_registry, ctx->twig_entity);
  120. // Create flashlight and light cone, set light cone parent to flashlight, and move both to underworld scene
  121. flashlight_archetype->assign(ecs_registry, ctx->flashlight_entity);
  122. auto flashlight_light_cone = flashlight_light_cone_archetype->create(ecs_registry);
  123. ec::parent(ecs_registry, flashlight_light_cone, ctx->flashlight_entity);
  124. ec::assign_render_layers(ecs_registry, ctx->flashlight_entity, 2);
  125. // Make lens tool's model instance unculled, so its shadow is always visible.
  126. model_instance* lens_model_instance = ctx->render_system->get_model_instance(ctx->lens_entity);
  127. if (lens_model_instance)
  128. {
  129. lens_model_instance->set_culling_mask(&ctx->no_cull);
  130. }
  131. // Create lens light cone and set its parent to lens
  132. auto lens_light_cone = lens_light_cone_archetype->create(ecs_registry);
  133. //ec::bind_transform(ecs_registry, lens_light_cone, ctx->lens_entity);
  134. ec::parent(ecs_registry, lens_light_cone, ctx->lens_entity);
  135. // Hide inactive tools
  136. ec::assign_render_layers(ecs_registry, ctx->forceps_entity, 0);
  137. ec::assign_render_layers(ecs_registry, ctx->brush_entity, 0);
  138. ec::assign_render_layers(ecs_registry, ctx->lens_entity, 0);
  139. ec::assign_render_layers(ecs_registry, ctx->marker_entity, 0);
  140. ec::assign_render_layers(ecs_registry, ctx->container_entity, 0);
  141. ec::assign_render_layers(ecs_registry, ctx->twig_entity, 0);
  142. // Activate brush tool
  143. ctx->tool_system->set_active_tool(ctx->brush_entity);
  144. // Create ant-hill
  145. auto ant_hill_entity = ant_hill_archetype->create(ecs_registry);
  146. ec::place(ecs_registry, ant_hill_entity, {0, 0});
  147. // Generate pebbles
  148. float pebble_radius = 300.0f;
  149. int pebble_count = 100;
  150. for (int i = 0; i < pebble_count; ++i)
  151. {
  152. float x = math::random(-pebble_radius, pebble_radius);
  153. float z = math::random(-pebble_radius, pebble_radius);
  154. auto pebble_entity = ant_head_archetype->create(ecs_registry);
  155. auto& transform = ecs_registry.get<ecs::transform_component>(pebble_entity);
  156. transform.local = math::identity_transform<float>;
  157. transform.local.rotation = math::angle_axis(math::random(0.0f, math::two_pi<float>), {0, 1, 0});
  158. transform.local.scale = float3{1, 1, 1} * math::random(0.75f, 1.25f);
  159. ec::place(ecs_registry, pebble_entity, {x, z});
  160. }
  161. // Create maple tree
  162. auto maple_tree_entity = maple_tree_archetype->create(ecs_registry);
  163. ec::place(ecs_registry, maple_tree_entity, {300, 200});
  164. // Creat nest
  165. auto nest_entity = nest_archetype->create(ecs_registry);
  166. // Create terrain
  167. int terrain_radius = 2;
  168. for (int x = -terrain_radius; x <= terrain_radius; ++x)
  169. {
  170. for (int z = -terrain_radius; z <= terrain_radius; ++z)
  171. {
  172. ecs::terrain_component terrain_component;
  173. terrain_component.subdivisions = TERRAIN_PATCH_RESOLUTION;
  174. terrain_component.x = x;
  175. terrain_component.z = z;
  176. auto terrain_entity = ecs_registry.create();
  177. ecs_registry.assign<ecs::terrain_component>(terrain_entity, terrain_component);
  178. }
  179. }
  180. // Create samaras
  181. for (int i = 0; i < 15; ++i)
  182. {
  183. auto samara_entity = samara_archetype->create(ecs_registry);
  184. auto& transform = ecs_registry.get<ecs::transform_component>(samara_entity);
  185. float zone = 200.0f;
  186. transform.local = math::identity_transform<float>;
  187. transform.local.translation.x = math::random(-zone, zone);
  188. transform.local.translation.y = math::random(50.0f, 150.0f);
  189. transform.local.translation.z = math::random(-zone, zone);
  190. ecs::samara_component samara_component;
  191. samara_component.angle = math::random(0.0f, math::radians(360.0f));
  192. samara_component.direction = math::normalize(float3{math::random(-1.0f, 1.0f), math::random(-1.0f, -5.0f), math::random(-1.0f, 1.0f)});
  193. samara_component.chirality = (math::random(0.0f, 1.0f) < 0.5f) ? -1.0f : 1.0f;
  194. ecs_registry.assign_or_replace<ecs::samara_component>(samara_entity, samara_component);
  195. }
  196. // Setup camera focal point
  197. ecs::transform_component focal_point_transform;
  198. focal_point_transform.local = math::identity_transform<float>;
  199. focal_point_transform.warp = true;
  200. ecs::camera_follow_component focal_point_follow;
  201. ecs::snap_component focal_point_snap;
  202. focal_point_snap.ray = {float3{0, 10000, 0}, float3{0, -1, 0}};
  203. focal_point_snap.warp = false;
  204. focal_point_snap.relative = true;
  205. focal_point_snap.autoremove = false;
  206. ecs_registry.assign_or_replace<ecs::transform_component>(ctx->focal_point_entity, focal_point_transform);
  207. ecs_registry.assign_or_replace<ecs::camera_follow_component>(ctx->focal_point_entity, focal_point_follow);
  208. ecs_registry.assign_or_replace<ecs::snap_component>(ctx->focal_point_entity, focal_point_snap);
  209. // Setup camera
  210. ctx->overworld_camera->look_at({0, 0, 1}, {0, 0, 0}, {0, 1, 0});
  211. ctx->camera_system->set_camera(ctx->overworld_camera);
  212. auto ant_head = ant_head_archetype->create(ecs_registry);
  213. ec::place(ecs_registry, ant_head, {50, 0});
  214. ctx->overworld_scene->update_tweens();
  215. // Allocate a nest
  216. nest* nest = new ::nest();
  217. // Setup initial nest parameters
  218. float tunnel_radius = 1.15f;
  219. nest->set_tunnel_radius(tunnel_radius);
  220. nest::shaft* central_shaft = nest->get_central_shaft();
  221. central_shaft->chirality = 1.0f;
  222. central_shaft->rotation = math::radians(0.0f);
  223. central_shaft->depth = {0.0f, 200.0f};
  224. central_shaft->radius = {15.0f, 15.0f};
  225. central_shaft->pitch = {40.0f, 40.0f};
  226. central_shaft->translation = {{{0.0f, 0.0f}, {0.0f, 0.0f}}};
  227. central_shaft->current_depth = 0.0f;
  228. for (std::size_t i = 0; i < 4; ++i)
  229. {
  230. nest::chamber chamber;
  231. chamber.shaft = central_shaft;
  232. chamber.depth = (i + 1) * 50.0f;
  233. chamber.rotation = math::radians(0.0f);
  234. chamber.inner_radius = 4.0f;
  235. chamber.outer_radius = 10.0f;
  236. central_shaft->chambers.push_back(chamber);
  237. }
  238. // Dig nest shafts
  239. float shift = 0.1f;
  240. for (int i = 0; i < 800; ++i)
  241. {
  242. ecs::cavity_component cavity;
  243. cavity.position = nest->extend_shaft(*nest->get_central_shaft());
  244. cavity.position += float3{math::random(-shift, shift), math::random(-shift, shift), math::random(-shift, shift)};
  245. cavity.radius = tunnel_radius * math::random(1.0f, 1.1f);
  246. ecs_registry.assign<ecs::cavity_component>(ecs_registry.create(), cavity);
  247. }
  248. // Dig nest chambers
  249. /*
  250. for (int i = 0; i < central_shaft->chambers.size(); ++i)
  251. {
  252. for (int j = 0; j < 150; ++j)
  253. {
  254. ecs::cavity_component cavity;
  255. cavity.position = nest->expand_chamber(central_shaft->chambers[i]);
  256. cavity.position += float3{math::random(-shift, shift), math::random(-shift, shift), math::random(-shift, shift)};
  257. cavity.radius = tunnel_radius * math::random(1.0f, 1.1f);
  258. ecs_registry.assign<ecs::cavity_component>(ecs_registry.create(), cavity);
  259. }
  260. }
  261. */
  262. // Place larva in chamber
  263. {
  264. auto larva = larva_archetype->create(ecs_registry);
  265. ec::assign_render_layers(ecs_registry, larva, 1);
  266. ec::warp_to(ecs_registry, larva, {50, 0.1935f, 10});
  267. //auto& transform = ecs_registry.get<ecs::transform_component>(larva_entity);
  268. //transform.transform = math::identity_transform<float>;
  269. //transform.transform.translation = nest->get_shaft_position(*central_shaft, central_shaft->depth[1]);
  270. //transform.transform.translation.y -= 1.0f;
  271. }
  272. auto dandelion_plant = dandelion_plant_archetype->create(ecs_registry);
  273. ec::place(ecs_registry, dandelion_plant, {55, -30});
  274. control_system* control_system = ctx->control_system;
  275. control_system->update(0.0, 0.0);
  276. control_system->set_nest(nest);
  277. // Start fade in
  278. ctx->fade_transition->transition(1.0f, true, ease<float>::in_quad);
  279. logger->pop_task(EXIT_SUCCESS);
  280. std::string biome_name = (*ctx->strings)[ctx->biome->name];
  281. logger->log("Entered biome \"" + biome_name + "\"");
  282. }
  283. void play_state_exit(game_context* ctx)
  284. {
  285. logger* logger = ctx->logger;
  286. logger->push_task("Exiting play state");
  287. logger->pop_task(EXIT_SUCCESS);
  288. }