Unravel Engine C++ Reference
Loading...
Searching...
No Matches
play_mode.cpp
Go to the documentation of this file.
1#include "play_mode.h"
2
3#include "events.h"
4#include "splash_scene.h"
5
6#include <engine/ecs/ecs.h>
8#include <logging/logging.h>
9#include <seq/seq.h>
11
12namespace unravel
13{
14
16{
17 APPLOG_TRACE("{}::{}", hpp::type_name_str(*this), __func__);
18 auto& ev = ctx.get_cached<events>();
19 ev.on_frame_update.connect(sentinel_, 10000, this, &play_mode::on_frame_update);
20 return true;
21}
22
24{
25 (void)ctx;
26 if(is_active())
27 {
28 end_play(ctx);
29 }
30 return true;
31}
32
33void play_mode::toggle(rtti::context& ctx, bool allow_splash)
34{
35 auto action = seq::delay(0ms);
36 action.on_end.connect([this, &ctx, allow_splash]()
37 {
38 set_active(ctx, !is_active(), allow_splash);
39 });
40 seq::queue(action, "play_mode");
41}
42
43void play_mode::set_active(rtti::context& ctx, bool active, bool allow_splash)
44{
45 if(is_active() == active)
46 {
47 return;
48 }
49 if(active)
50 {
51 begin_play(ctx, allow_splash);
52 }
53 else
54 {
55 end_play(ctx);
56 }
57}
58
60{
61 if(paused && !is_active())
62 {
63 return;
64 }
65 if(is_paused_ == paused)
66 {
67 return;
68 }
69 is_paused_ = paused;
70 auto& ev = ctx.get_cached<events>();
71 is_paused_ ? ev.on_pause(ctx) : ev.on_resume(ctx);
72}
73
75{
76 auto action = seq::delay(0ms);
77 action.on_begin.connect([this, &ctx]()
78 {
79 set_paused(ctx, !is_paused_);
80 });
81 seq::queue(action, "play_mode");
82}
83
85{
86 if(!is_active())
87 {
88 return;
89 }
90 if(!is_paused_)
91 {
92 return;
93 }
94 ctx.get_cached<events>().on_skip_next_frame(ctx);
95}
96
98{
99 (void)dt;
100 if(current_phase_ != phase::splash)
101 {
102 return;
103 }
104 splash_scene::update(ctx, splash_state_);
105 if(splash_scene::is_finished(splash_state_))
106 {
107 enter_running(ctx);
108 }
109}
110
111void play_mode::begin_play(rtti::context& ctx, bool allow_splash)
112{
113 auto& ev = ctx.get_cached<events>();
114 ev.on_play_before_begin(ctx);
115
116 const bool show_splash = allow_splash && should_show_splash(ctx);
117 if(show_splash)
118 {
119 current_phase_ = phase::splash;
120 is_paused_ = false;
121 frames_running_ = 0;
122 auto& ec = ctx.get_cached<ecs>();
123 splash_scene::setup(ctx, ec.get_scene(), splash_state_);
124 }
125 else
126 {
127 is_paused_ = false;
128 frames_running_ = 0;
129 enter_running(ctx);
130 }
131}
132
133void play_mode::enter_running(rtti::context& ctx)
134{
135 current_phase_ = phase::running;
136 frames_running_ = 0;
137 // Domain reload / scene restore often ran just before this; discard that
138 // hitch from the sim clock so the next measured frame is clean.
139 ctx.get_cached<simulation>().reset_delta_clock();
140 splash_scene::teardown(splash_state_);
141 ctx.get_cached<events>().on_play_begin(ctx);
142}
143
144void play_mode::end_play(rtti::context& ctx)
145{
146 auto& ev = ctx.get_cached<events>();
147 if(is_paused_)
148 {
149 set_paused(ctx, false);
150 }
151 ev.on_play_end(ctx);
152 current_phase_ = phase::inactive;
153 is_paused_ = false;
154 frames_running_ = 0;
155 splash_scene::teardown(splash_state_);
156 ev.on_play_after_end(ctx);
157}
158
159auto play_mode::should_show_splash(rtti::context& ctx) const -> bool
160{
161 if(!ctx.has<settings>())
162 {
163 return splash_scene::has_content(ctx);
164 }
165 const auto& splash = ctx.get<settings>().splash;
166 return splash.enabled && splash_scene::has_content(ctx);
167}
168
170{
171 ++frames_running_;
172}
173
174} // namespace unravel
std::chrono::duration< float > delta_t
#define APPLOG_TRACE(...)
Definition logging.h:17
auto queue(seq_action action, const seq_scope_policy &scope_policy, hpp::source_location location) -> seq_id_t
Queues a new action.
Definition seq.cpp:14
auto delay(const duration_t &duration, const sentinel_t &sentinel) -> seq_action
Creates a delay action.
Definition seq_core.cpp:182
void teardown(splash_scene_state &state)
auto is_finished(const splash_scene_state &state) -> bool
void update(rtti::context &ctx, splash_scene_state &state)
auto has_content(rtti::context &ctx) -> bool
void setup(rtti::context &ctx, scene &scn, splash_scene_state &state)
auto get_cached() -> T &
Definition context.hpp:49
auto has() const -> bool
Definition context.hpp:28
auto get() -> T &
Definition context.hpp:35
Manages the entity-component-system (ECS) operations for the ACE framework.
Definition ecs.h:12
hpp::event< void(rtti::context &, delta_t)> on_frame_update
Definition events.h:16
hpp::event< void(rtti::context &)> on_play_before_begin
engine play events
Definition events.h:23
auto deinit(rtti::context &ctx) -> bool
Definition play_mode.cpp:23
void toggle(rtti::context &ctx, bool allow_splash=true)
Definition play_mode.cpp:33
void set_paused(rtti::context &ctx, bool paused)
Definition play_mode.cpp:59
void set_active(rtti::context &ctx, bool active, bool allow_splash=true)
Definition play_mode.cpp:43
auto init(rtti::context &ctx) -> bool
Definition play_mode.cpp:15
void skip_next_frame(rtti::context &ctx)
Definition play_mode.cpp:84
void toggle_pause(rtti::context &ctx)
Definition play_mode.cpp:74
void on_frame_update(rtti::context &ctx, delta_t dt)
Definition play_mode.cpp:97
auto is_active() const -> bool
Definition play_mode.h:36