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

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