Antkeeper  0.0.1
credits-state.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2023 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 
22 #include "game/game.hpp"
26 #include <engine/scene/text.hpp>
27 #include <engine/debug/log.hpp>
28 #include "game/strings.hpp"
30 #include <engine/math/vector.hpp>
31 
33  game_state(ctx)
34 {
35  debug::log_trace("Entering credits state...");
36 
37  const math::fvec2 viewport_size = math::fvec2(ctx.window->get_viewport_size());
38  const math::fvec2 viewport_center = viewport_size * 0.5f;
39 
40  // Construct credits text
41  credits_text.set_material(ctx.menu_font_material);
42  credits_text.set_font(&ctx.menu_font);
43  credits_text.set_color({1.0f, 1.0f, 1.0f, 0.0f});
44  credits_text.set_content(get_string(ctx, "credits"));
45 
46  // Align credits text
47  const auto& credits_aabb = credits_text.get_bounds();
48  float credits_w = credits_aabb.max.x() - credits_aabb.min.x();
49  float credits_h = credits_aabb.max.y() - credits_aabb.min.y();
50  credits_text.set_translation({std::round(viewport_center.x() - credits_w * 0.5f), std::round(viewport_center.y() - credits_h * 0.5f), 0.0f});
51 
52  // Set up animation timing configuration
53  const float credits_fade_in_duration = 0.5;
54  const float credits_scroll_duration = 5.0;
55 
56  auto set_credits_opacity = [this](int channel, const float& opacity)
57  {
58  this->credits_text.set_color({1.0f, 1.0f, 1.0f, opacity});
59  };
60 
61  // Build credits fade in animation
62  credits_fade_in_animation.set_interpolator(ease<float>::in_quad);
63  animation_channel<float>* credits_fade_in_opacity_channel = credits_fade_in_animation.add_channel(0);
64  credits_fade_in_opacity_channel->insert_keyframe({0.0f, 0.0f});
65  credits_fade_in_opacity_channel->insert_keyframe({credits_fade_in_duration, 1.0f});
66  credits_fade_in_animation.set_frame_callback(set_credits_opacity);
67 
68  // Add credits animations to animator
69  ctx.animator->add_animation(&credits_fade_in_animation);
70 
71  // Start credits fade in animation
72  credits_fade_in_animation.play();
73 
74  // Setup window resized callback
75  window_resized_subscription = ctx.window->get_resized_channel().subscribe
76  (
77  [&](const auto& event)
78  {
79  const math::fvec2 viewport_size = math::fvec2(event.window->get_viewport_size());
80  const math::fvec2 viewport_center = viewport_size * 0.5f;
81  const auto& credits_aabb = credits_text.get_bounds();
82  float credits_w = credits_aabb.max.x() - credits_aabb.min.x();
83  float credits_h = credits_aabb.max.y() - credits_aabb.min.y();
84  credits_text.set_translation({std::round(viewport_center.x() - credits_w * 0.5f), std::round(viewport_center.y() - credits_h * 0.5f), 0.0f});
85  }
86  );
87 
88  // Construct credits skip function
89  auto skip = [&](const auto& event)
90  {
91  ctx.function_queue.emplace
92  (
93  [&]()
94  {
95  // Change to extras menu state
96  ctx.state_machine.pop();
97  ctx.state_machine.emplace(std::make_unique<extras_menu_state>(ctx));
98  }
99  );
100  };
101 
102  // Set up credits skippers
103  input_mapped_subscriptions.emplace_back
104  (
106  (
107  skip
108  )
109  );
110  input_mapped_subscriptions.emplace_back
111  (
113  (
114  skip
115  )
116  );
117  input_mapped_subscriptions.emplace_back
118  (
120  (
121  skip
122  )
123  );
124 
125  // Enable credits skippers next frame
126  ctx.function_queue.push
127  (
128  [&]()
129  {
130  ctx.input_mapper.connect(ctx.input_manager->get_event_dispatcher());
131  }
132  );
133 
134  ctx.ui_scene->add_object(credits_text);
135 
136  debug::log_trace("Entered credits state");
137 }
138 
140 {
141  debug::log_trace("Exiting credits state...");
142 
143  // Disable credits skippers
145  input_mapped_subscriptions.clear();
146 
147  // Destruct credits text
148  ctx.ui_scene->remove_object(credits_text);
149 
150  // Destruct credits animations
151  ctx.animator->remove_animation(&credits_fade_in_animation);
152 
153  debug::log_trace("Exited credits state");
154 }
void play()
Plays the animation.
Definition: animation.cpp:54
Single channel in a keyframe animation.
void insert_keyframe(const keyframe &k)
Adds a keyframe to the animation.
void set_interpolator(interpolator_type interpolator)
Sets the frame interpolator function object.
Definition: animation.hpp:339
channel * add_channel(int id)
Adds a channel to the animation.
Definition: animation.hpp:317
void set_frame_callback(std::function< void(int, const T &)> callback)
Sets the callback that's executed on each frame of animation.
Definition: animation.hpp:345
virtual ~credits_state()
credits_state(::game &ctx)
Abstract base class for game states.
Definition: game-state.hpp:29
::game & ctx
Definition: game-state.hpp:44
Definition: game.hpp:121
std::unique_ptr< scene::collection > ui_scene
Definition: game.hpp:333
hsm::state_machine< game_state > state_machine
Definition: game.hpp:299
std::unique_ptr< animator > animator
Definition: game.hpp:368
type::bitmap_font menu_font
Definition: game.hpp:188
std::shared_ptr< app::window > window
Definition: game.hpp:166
std::queue< std::function< void()> > function_queue
Definition: game.hpp:303
std::shared_ptr< render::material > menu_font_material
Definition: game.hpp:191
input::mapper input_mapper
Definition: game.hpp:214
std::unique_ptr< app::input_manager > input_manager
Definition: game.hpp:172
::event::channel< key_mapped_event > & get_key_mapped_channel() noexcept
Returns the channel through which key mapped events are published.
Definition: mapper.hpp:66
void disconnect()
Disconnects all input event signals from the mapper.
Definition: mapper.cpp:35
::event::channel< mouse_button_mapped_event > & get_mouse_button_mapped_channel() noexcept
Returns the channel through which mouse button mapped events are published.
Definition: mapper.hpp:72
::event::channel< gamepad_button_mapped_event > & get_gamepad_button_mapped_channel() noexcept
Returns the channel through which gamepad button mapped events are published.
Definition: mapper.hpp:60
void connect(::event::dispatcher &dispatcher)
Connects the input event signals of an event dispatcher to the mapper.
Definition: mapper.cpp:25
void set_translation(const vector_type &translation)
Sets the translation of the object.
Definition: object.hpp:85
void set_content(const std::string &content)
Sets the text content.
Definition: text.cpp:116
void set_color(const math::fvec4 &color)
Sets the text color.
Definition: text.cpp:126
const aabb_type & get_bounds() const noexcept override
Returns the bounds of the object.
Definition: text.hpp:116
void set_material(std::shared_ptr< render::material > material)
Sets the text material.
Definition: text.cpp:93
void set_font(const type::bitmap_font *font)
Sets the text font.
Definition: text.cpp:98
log_message< log_message_severity::trace, Args... > log_trace
Formats and logs a trace message.
Definition: log.hpp:88
Publish-subscribe messaging.
Definition: channel.hpp:32
constexpr vector< T, N > round(const vector< T, N > &x)
Performs a element-wise round operation.
Definition: vector.hpp:1489
std::string get_string(const ::game &ctx, hash::fnv1a32_t key)
Returns a localized string.
Definition: strings.cpp:23
Container for templated easing functions.
Definition: ease.hpp:71
vector_type max
Maximum extent of the hyperrectangle.
n-dimensional vector.
Definition: vector.hpp:44
constexpr element_type & x() noexcept
Returns a reference to the first element.
Definition: vector.hpp:164
constexpr element_type & y() noexcept
Returns a reference to the second element.
Definition: vector.hpp:180