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

407 lines
16 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/components/orbit-component.hpp"
  35. #include "game/entity-commands.hpp"
  36. #include "game/game-context.hpp"
  37. #include "game/states/game-states.hpp"
  38. #include "math/math.hpp"
  39. #include "nest.hpp"
  40. #include "renderer/material.hpp"
  41. #include "rasterizer/texture-2d.hpp"
  42. #include "rasterizer/texture-filter.hpp"
  43. #include "rasterizer/texture-wrapping.hpp"
  44. #include "renderer/model.hpp"
  45. #include "renderer/passes/sky-pass.hpp"
  46. #include "resources/resource-manager.hpp"
  47. #include "scene/model-instance.hpp"
  48. #include "scene/scene.hpp"
  49. #include "scene/camera.hpp"
  50. #include "scene/ambient-light.hpp"
  51. #include "scene/directional-light.hpp"
  52. #include "scene/directional-light.hpp"
  53. #include "game/systems/control-system.hpp"
  54. #include "game/systems/camera-system.hpp"
  55. #include "game/systems/render-system.hpp"
  56. #include "game/systems/tool-system.hpp"
  57. #include "game/systems/weather-system.hpp"
  58. #include "game/systems/astronomy-system.hpp"
  59. #include "game/biome.hpp"
  60. #include "utility/fundamental-types.hpp"
  61. #include "utility/gamma.hpp"
  62. #include "game/astronomy/celestial-time.hpp"
  63. #include <iostream>
  64. void play_state_enter(game_context* ctx)
  65. {
  66. logger* logger = ctx->logger;
  67. logger->push_task("Entering play state");
  68. // Load biome
  69. if (ctx->option_biome.has_value())
  70. {
  71. ctx->biome = ctx->resource_manager->load<biome>(ctx->option_biome.value() + ".bio");
  72. }
  73. else
  74. {
  75. ctx->biome = ctx->resource_manager->load<biome>("grassland.bio");
  76. }
  77. // Apply biome parameters to scene
  78. sky_pass* sky_pass = ctx->overworld_sky_pass;
  79. sky_pass->set_enabled(true);
  80. sky_pass->set_sky_model(ctx->resource_manager->load<model>("sky-dome.mdl"));
  81. sky_pass->set_moon_model(ctx->resource_manager->load<model>("moon.mdl"));
  82. ctx->weather_system->set_location(ctx->biome->location[0], ctx->biome->location[1], ctx->biome->location[2]);
  83. ctx->weather_system->set_time(2000, 1, 1, 12, 0, 0.0, 0.0);
  84. ctx->weather_system->set_time(0.0);
  85. ctx->weather_system->set_sky_palette(ctx->biome->sky_palette);
  86. ctx->weather_system->set_sun_palette(ctx->biome->sun_palette);
  87. ctx->weather_system->set_ambient_palette(ctx->biome->ambient_palette);
  88. ctx->weather_system->set_moon_palette(ctx->biome->moon_palette);
  89. ctx->weather_system->set_shadow_palette(ctx->biome->shadow_palette);
  90. ctx->astronomy_system->set_observer_location(double3{4.26352e-5, ctx->biome->location[0], ctx->biome->location[1]});
  91. ctx->astronomy_system->set_universal_time(0.0);
  92. ctx->astronomy_system->set_obliquity(math::radians(23.4393));
  93. ctx->astronomy_system->set_axial_rotation_at_epoch(math::radians(280.4606));
  94. ctx->astronomy_system->set_axial_rotation_speed(math::radians(360.9856));
  95. resource_manager* resource_manager = ctx->resource_manager;
  96. entt::registry& ecs_registry = *ctx->ecs_registry;
  97. // Create sun
  98. {
  99. ecs::orbit_component sun_orbit;
  100. sun_orbit.a = 1.0;
  101. sun_orbit.ec = 0.016709;
  102. sun_orbit.w = math::radians(282.9404);
  103. sun_orbit.ma = math::radians(356.0470);
  104. sun_orbit.i = 0.0;
  105. sun_orbit.om = 0.0;
  106. sun_orbit.d_a = 0.0;
  107. sun_orbit.d_ec = -1.151e-9;
  108. sun_orbit.d_w = math::radians(4.70935e-5);
  109. sun_orbit.d_ma = math::radians(0.9856002585);
  110. sun_orbit.d_i = 0.0;
  111. sun_orbit.d_om = 0.0;
  112. ecs::transform_component sun_transform;
  113. sun_transform.local = math::identity_transform<float>;
  114. sun_transform.warp = true;
  115. auto sun_entity = ecs_registry.create();
  116. ecs_registry.assign<ecs::transform_component>(sun_entity, sun_transform);
  117. ecs_registry.assign<ecs::orbit_component>(sun_entity, sun_orbit);
  118. }
  119. // Create moon
  120. {
  121. ecs::orbit_component moon_orbit;
  122. moon_orbit.a = 0.00256955529;
  123. moon_orbit.ec = 0.0554;
  124. moon_orbit.w = math::radians(318.15);
  125. moon_orbit.ma = math::radians(135.27);
  126. moon_orbit.i = math::radians(5.16);
  127. moon_orbit.om = math::radians(125.08);
  128. moon_orbit.d_a = 0.0;
  129. moon_orbit.d_ec = 0.0;
  130. moon_orbit.d_w = math::radians(0.1643573223);
  131. moon_orbit.d_ma = math::radians(13.176358);
  132. moon_orbit.d_i = 0.0;
  133. moon_orbit.d_om = math::radians(-0.0529538083);
  134. ecs::transform_component moon_transform;
  135. moon_transform.local = math::identity_transform<float>;
  136. moon_transform.warp = true;
  137. auto moon_entity = ecs_registry.create();
  138. ecs_registry.assign<ecs::transform_component>(moon_entity, moon_transform);
  139. ecs_registry.assign<ecs::orbit_component>(moon_entity, moon_orbit);
  140. }
  141. // Load entity archetypes
  142. ecs::archetype* ant_hill_archetype = resource_manager->load<ecs::archetype>("ant-hill.ent");
  143. ecs::archetype* maple_tree_archetype = resource_manager->load<ecs::archetype>("maple-tree.ent");
  144. ecs::archetype* nest_archetype = resource_manager->load<ecs::archetype>("harvester-nest.ent");
  145. ecs::archetype* samara_archetype = resource_manager->load<ecs::archetype>("samara.ent");
  146. ecs::archetype* forceps_archetype = resource_manager->load<ecs::archetype>("forceps.ent");
  147. ecs::archetype* lens_archetype = resource_manager->load<ecs::archetype>("lens.ent");
  148. ecs::archetype* brush_archetype = resource_manager->load<ecs::archetype>("brush.ent");
  149. ecs::archetype* marker_archetype = resource_manager->load<ecs::archetype>("marker.ent");
  150. ecs::archetype* container_archetype = resource_manager->load<ecs::archetype>("container.ent");
  151. ecs::archetype* twig_archetype = resource_manager->load<ecs::archetype>("twig.ent");
  152. ecs::archetype* larva_archetype = resource_manager->load<ecs::archetype>("larva.ent");
  153. ecs::archetype* pebble_archetype = resource_manager->load<ecs::archetype>("pebble.ent");
  154. ecs::archetype* flashlight_archetype = resource_manager->load<ecs::archetype>("flashlight.ent");
  155. ecs::archetype* flashlight_light_cone_archetype = resource_manager->load<ecs::archetype>("flashlight-light-cone.ent");
  156. ecs::archetype* lens_light_cone_archetype = resource_manager->load<ecs::archetype>("lens-light-cone.ent");
  157. ecs::archetype* ant_head_archetype = resource_manager->load<ecs::archetype>("ant-head.ent");
  158. ecs::archetype* dandelion_plant_archetype = resource_manager->load<ecs::archetype>("dandelion-plant.ent");
  159. ecs::archetype* grassland_road_archetype = resource_manager->load<ecs::archetype>("grassland-road.ent");
  160. // Create tools
  161. forceps_archetype->assign(ecs_registry, ctx->forceps_entity);
  162. lens_archetype->assign(ecs_registry, ctx->lens_entity);
  163. brush_archetype->assign(ecs_registry, ctx->brush_entity);
  164. marker_archetype->assign(ecs_registry, ctx->marker_entity);
  165. container_archetype->assign(ecs_registry, ctx->container_entity);
  166. twig_archetype->assign(ecs_registry, ctx->twig_entity);
  167. // Create flashlight and light cone, set light cone parent to flashlight, and move both to underworld scene
  168. flashlight_archetype->assign(ecs_registry, ctx->flashlight_entity);
  169. auto flashlight_light_cone = flashlight_light_cone_archetype->create(ecs_registry);
  170. ec::parent(ecs_registry, flashlight_light_cone, ctx->flashlight_entity);
  171. ec::assign_render_layers(ecs_registry, ctx->flashlight_entity, 2);
  172. // Make lens tool's model instance unculled, so its shadow is always visible.
  173. model_instance* lens_model_instance = ctx->render_system->get_model_instance(ctx->lens_entity);
  174. if (lens_model_instance)
  175. {
  176. lens_model_instance->set_culling_mask(&ctx->no_cull);
  177. }
  178. // Create lens light cone and set its parent to lens
  179. auto lens_light_cone = lens_light_cone_archetype->create(ecs_registry);
  180. //ec::bind_transform(ecs_registry, lens_light_cone, ctx->lens_entity);
  181. ec::parent(ecs_registry, lens_light_cone, ctx->lens_entity);
  182. // Hide inactive tools
  183. ec::assign_render_layers(ecs_registry, ctx->forceps_entity, 0);
  184. ec::assign_render_layers(ecs_registry, ctx->brush_entity, 0);
  185. ec::assign_render_layers(ecs_registry, ctx->lens_entity, 0);
  186. ec::assign_render_layers(ecs_registry, ctx->marker_entity, 0);
  187. ec::assign_render_layers(ecs_registry, ctx->container_entity, 0);
  188. ec::assign_render_layers(ecs_registry, ctx->twig_entity, 0);
  189. // Activate brush tool
  190. ctx->tool_system->set_active_tool(ctx->brush_entity);
  191. // Create background
  192. for (int i = 0; i < 4; ++i)
  193. {
  194. auto road_entity = grassland_road_archetype->create(ecs_registry);
  195. auto& transform = ecs_registry.get<ecs::transform_component>(road_entity);
  196. math::quaternion<float> rotation = math::angle_axis(math::half_pi<float> * static_cast<float>(i), float3{0, 1, 0});
  197. float3 translation = rotation * float3{0, 0, 1600.0f};
  198. transform.local = math::identity_transform<float>;
  199. transform.local.rotation = rotation;
  200. transform.local.translation = translation;
  201. }
  202. // Create ant-hill
  203. auto ant_hill_entity = ant_hill_archetype->create(ecs_registry);
  204. ec::place(ecs_registry, ant_hill_entity, {0, 0});
  205. // Generate pebbles
  206. float pebble_radius = 300.0f;
  207. int pebble_count = 20;
  208. for (int i = 0; i < pebble_count; ++i)
  209. {
  210. float x = math::random(-pebble_radius, pebble_radius);
  211. float z = math::random(-pebble_radius, pebble_radius);
  212. auto pebble_entity = ant_head_archetype->create(ecs_registry);
  213. auto& transform = ecs_registry.get<ecs::transform_component>(pebble_entity);
  214. transform.local = math::identity_transform<float>;
  215. transform.local.rotation = math::angle_axis(math::random(0.0f, math::two_pi<float>), {0, 1, 0});
  216. transform.local.scale = float3{1, 1, 1} * math::random(0.75f, 1.25f);
  217. ec::place(ecs_registry, pebble_entity, {x, z});
  218. }
  219. // Create maple tree
  220. //auto maple_tree_entity = maple_tree_archetype->create(ecs_registry);
  221. //ec::place(ecs_registry, maple_tree_entity, {300, 200});
  222. // Creat nest
  223. auto nest_entity = nest_archetype->create(ecs_registry);
  224. // Create terrain
  225. int terrain_radius = 6;
  226. for (int x = -terrain_radius; x <= terrain_radius; ++x)
  227. {
  228. for (int z = -terrain_radius; z <= terrain_radius; ++z)
  229. {
  230. ecs::terrain_component terrain_component;
  231. terrain_component.subdivisions = TERRAIN_PATCH_RESOLUTION;
  232. terrain_component.x = x;
  233. terrain_component.z = z;
  234. auto terrain_entity = ecs_registry.create();
  235. ecs_registry.assign<ecs::terrain_component>(terrain_entity, terrain_component);
  236. }
  237. }
  238. // Create samaras
  239. for (int i = 0; i < 15; ++i)
  240. {
  241. auto samara_entity = samara_archetype->create(ecs_registry);
  242. auto& transform = ecs_registry.get<ecs::transform_component>(samara_entity);
  243. float zone = 200.0f;
  244. transform.local = math::identity_transform<float>;
  245. transform.local.translation.x = math::random(-zone, zone);
  246. transform.local.translation.y = math::random(50.0f, 150.0f);
  247. transform.local.translation.z = math::random(-zone, zone);
  248. ecs::samara_component samara_component;
  249. samara_component.angle = math::random(0.0f, math::radians(360.0f));
  250. samara_component.direction = math::normalize(float3{math::random(-1.0f, 1.0f), math::random(-1.0f, -5.0f), math::random(-1.0f, 1.0f)});
  251. samara_component.chirality = (math::random(0.0f, 1.0f) < 0.5f) ? -1.0f : 1.0f;
  252. ecs_registry.assign_or_replace<ecs::samara_component>(samara_entity, samara_component);
  253. }
  254. // Setup camera focal point
  255. ecs::transform_component focal_point_transform;
  256. focal_point_transform.local = math::identity_transform<float>;
  257. focal_point_transform.warp = true;
  258. ecs::camera_follow_component focal_point_follow;
  259. ecs::snap_component focal_point_snap;
  260. focal_point_snap.ray = {float3{0, 10000, 0}, float3{0, -1, 0}};
  261. focal_point_snap.warp = false;
  262. focal_point_snap.relative = true;
  263. focal_point_snap.autoremove = false;
  264. ecs_registry.assign_or_replace<ecs::transform_component>(ctx->focal_point_entity, focal_point_transform);
  265. ecs_registry.assign_or_replace<ecs::camera_follow_component>(ctx->focal_point_entity, focal_point_follow);
  266. ecs_registry.assign_or_replace<ecs::snap_component>(ctx->focal_point_entity, focal_point_snap);
  267. // Setup camera
  268. ctx->overworld_camera->look_at({0, 0, 1}, {0, 0, 0}, {0, 1, 0});
  269. ctx->camera_system->set_camera(ctx->overworld_camera);
  270. auto ant_head = ant_head_archetype->create(ecs_registry);
  271. ec::place(ecs_registry, ant_head, {50, 0});
  272. ctx->overworld_scene->update_tweens();
  273. // Allocate a nest
  274. nest* nest = new ::nest();
  275. // Setup initial nest parameters
  276. float tunnel_radius = 1.15f;
  277. nest->set_tunnel_radius(tunnel_radius);
  278. nest::shaft* central_shaft = nest->get_central_shaft();
  279. central_shaft->chirality = 1.0f;
  280. central_shaft->rotation = math::radians(0.0f);
  281. central_shaft->depth = {0.0f, 200.0f};
  282. central_shaft->radius = {15.0f, 15.0f};
  283. central_shaft->pitch = {40.0f, 40.0f};
  284. central_shaft->translation = {{{0.0f, 0.0f}, {0.0f, 0.0f}}};
  285. central_shaft->current_depth = 0.0f;
  286. for (std::size_t i = 0; i < 4; ++i)
  287. {
  288. nest::chamber chamber;
  289. chamber.shaft = central_shaft;
  290. chamber.depth = (i + 1) * 50.0f;
  291. chamber.rotation = math::radians(0.0f);
  292. chamber.inner_radius = 4.0f;
  293. chamber.outer_radius = 10.0f;
  294. central_shaft->chambers.push_back(chamber);
  295. }
  296. // Dig nest shafts
  297. float shift = 0.1f;
  298. for (int i = 0; i < 800; ++i)
  299. {
  300. ecs::cavity_component cavity;
  301. cavity.position = nest->extend_shaft(*nest->get_central_shaft());
  302. cavity.position += float3{math::random(-shift, shift), math::random(-shift, shift), math::random(-shift, shift)};
  303. cavity.radius = tunnel_radius * math::random(1.0f, 1.1f);
  304. ecs_registry.assign<ecs::cavity_component>(ecs_registry.create(), cavity);
  305. }
  306. // Dig nest chambers
  307. /*
  308. for (int i = 0; i < central_shaft->chambers.size(); ++i)
  309. {
  310. for (int j = 0; j < 150; ++j)
  311. {
  312. ecs::cavity_component cavity;
  313. cavity.position = nest->expand_chamber(central_shaft->chambers[i]);
  314. cavity.position += float3{math::random(-shift, shift), math::random(-shift, shift), math::random(-shift, shift)};
  315. cavity.radius = tunnel_radius * math::random(1.0f, 1.1f);
  316. ecs_registry.assign<ecs::cavity_component>(ecs_registry.create(), cavity);
  317. }
  318. }
  319. */
  320. // Place larva in chamber
  321. {
  322. auto larva = larva_archetype->create(ecs_registry);
  323. ec::assign_render_layers(ecs_registry, larva, 1);
  324. ec::warp_to(ecs_registry, larva, {50, 0.1935f, 10});
  325. //auto& transform = ecs_registry.get<ecs::transform_component>(larva_entity);
  326. //transform.transform = math::identity_transform<float>;
  327. //transform.transform.translation = nest->get_shaft_position(*central_shaft, central_shaft->depth[1]);
  328. //transform.transform.translation.y -= 1.0f;
  329. }
  330. auto dandelion_plant = dandelion_plant_archetype->create(ecs_registry);
  331. ec::place(ecs_registry, dandelion_plant, {55, -30});
  332. control_system* control_system = ctx->control_system;
  333. control_system->update(0.0, 0.0);
  334. control_system->set_nest(nest);
  335. // Start fade in
  336. ctx->fade_transition->transition(1.0f, true, ease<float>::in_quad);
  337. logger->pop_task(EXIT_SUCCESS);
  338. std::string biome_name = (*ctx->strings)[ctx->biome->name];
  339. logger->log("Entered biome \"" + biome_name + "\"");
  340. }
  341. void play_state_exit(game_context* ctx)
  342. {
  343. logger* logger = ctx->logger;
  344. logger->push_task("Exiting play state");
  345. logger->pop_task(EXIT_SUCCESS);
  346. }