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

108 lines
3.3 KiB

  1. /*
  2. * Copyright (C) 2021 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 "game/tools.hpp"
  20. #include "application.hpp"
  21. #include "animation/animator.hpp"
  22. #include "animation/animation.hpp"
  23. #include "entity/components/tool.hpp"
  24. #include "entity/components/celestial-body.hpp"
  25. #include "utility/timestamp.hpp"
  26. #include "render/material.hpp"
  27. #include "game/graphics.hpp"
  28. namespace game {
  29. entity::id build_camera_tool(game::context& ctx)
  30. {
  31. // Create camera tool entity
  32. entity::id tool_eid = ctx.entity_registry->create();
  33. // Create tool component
  34. entity::component::tool tool;
  35. // Setup tool activated callback
  36. tool.activated = [&ctx]()
  37. {
  38. if (!ctx.camera_flash_animation->is_stopped())
  39. return;
  40. game::graphics::save_screenshot(ctx);
  41. render::material_property<float4>* tint = static_cast<render::material_property<float4>*>(ctx.camera_flash_billboard->get_material()->get_property("tint"));
  42. tint->set_value({1.0f, 1.0f, 1.0f, 1.0f});
  43. ctx.camera_flash_billboard->get_material()->update_tweens();
  44. ctx.ui_scene->add_object(ctx.camera_flash_billboard);
  45. ctx.camera_flash_animation->set_end_callback
  46. (
  47. [&ctx]()
  48. {
  49. ctx.ui_scene->remove_object(ctx.camera_flash_billboard);
  50. }
  51. );
  52. ctx.camera_flash_animation->set_frame_callback
  53. (
  54. [&ctx, tint](int channel, const float& opacity)
  55. {
  56. tint->set_value({1.0f, 1.0f, 1.0f, opacity});
  57. }
  58. );
  59. ctx.animator->remove_animation(ctx.camera_flash_animation);
  60. ctx.animator->add_animation(ctx.camera_flash_animation);
  61. ctx.camera_flash_animation->rewind();
  62. ctx.camera_flash_animation->play();
  63. };
  64. // Add tool component to camera tool entity
  65. ctx.entity_registry->assign<entity::component::tool>(tool_eid, tool);
  66. return tool_eid;
  67. }
  68. entity::id build_time_tool(game::context& ctx)
  69. {
  70. // Create time tool entity
  71. entity::id tool_eid = ctx.entity_registry->create();
  72. // Create tool component
  73. entity::component::tool tool;
  74. // Setup tool active calback
  75. tool.active = [&ctx]()
  76. {
  77. auto [mouse_x, mouse_y] = ctx.app->get_mouse()->get_current_position();
  78. auto [window_w, window_h] = ctx.app->get_viewport_dimensions();
  79. entity::id planet_eid = ctx.entities["planet"];
  80. entity::component::celestial_body body = ctx.entity_registry->get<entity::component::celestial_body>(planet_eid);
  81. body.axial_rotation = math::radians(360.0f) * ((float)mouse_x / (float)window_w);
  82. ctx.entity_registry->replace<entity::component::celestial_body>(planet_eid, body);
  83. };
  84. // Add tool component to time tool entity
  85. ctx.entity_registry->assign<entity::component::tool>(tool_eid, tool);
  86. return tool_eid;
  87. }
  88. } // namespace game