Unravel Engine C++ Reference
Loading...
Searching...
No Matches
seq_core.cpp
Go to the documentation of this file.
1#include "seq_core.h"
2#include "seq_inspector.h"
3
4namespace seq
5{
6
7auto sequence_impl(const std::vector<seq_action>& actions, const sentinel_t& sentinel, bool precise) -> seq_action
8{
9 if(actions.empty())
10 {
11 return {};
12 }
13
14 duration_t duration = 0ms;
15 for(auto& action : actions)
16 {
17 duration += seq_private::get_duration(action);
18 }
19
20 auto updater = [actions = actions,
21 sentinel = sentinel,
22 current_action_idx = size_t(0),
23 prev_overflow = duration_t::zero(),
24 prev_elapsed = duration_t::zero(),
25 start_required = true,
26 finished = false,
27 precise](duration_t delta, seq_action& self) mutable
28 {
29 if(finished)
30 {
31 return state_t::finished;
32 }
33
34 if(sentinel.expired())
35 {
36 finished = true;
37 return state_t::finished;
38 }
39
40 if(start_required)
41 {
42 seq_private::start(actions.at(current_action_idx));
43 prev_elapsed = duration_t::zero();
44 start_required = false;
45 }
46
48 {
49 for(auto& action : actions)
50 {
52 }
53 }
54
55 auto& current_action = actions.at(current_action_idx);
56
57 if(precise)
58 {
59 delta += prev_overflow;
60 }
61
62 auto state = seq_private::update(current_action, delta);
63
64 auto elapsed = seq_private::get_elapsed(current_action);
65 auto elapsed_diff = elapsed - prev_elapsed;
66 prev_elapsed = elapsed;
67
68 seq_private::update_elapsed(self, elapsed_diff);
69 inspector::update_action_status(self, current_action_idx);
70 self.on_step.emit();
71
72 prev_overflow = duration_t::zero();
73 if(state == state_t::finished)
74 {
75 prev_overflow = seq_private::get_overflow(current_action);
76 current_action_idx++;
77 if(current_action_idx == actions.size())
78 {
79 seq_private::update_elapsed(self, prev_overflow);
80
81 finished = true;
82 return state_t::finished;
83 }
84 start_required = true;
85 }
86 return state_t::running;
87 };
88
89 auto creator = [updater = std::move(updater)]()
90 {
91 return updater;
92 };
93
94 auto action = seq_action(std::move(creator), duration, sentinel);
95 inspector::add_sequence_info(action, actions);
96 return action;
97}
98
99auto sequence(const std::vector<seq_action>& actions, const sentinel_t& sentinel) -> seq_action
100{
101 return sequence_impl(actions, sentinel, false);
102}
103
104auto sequence_precise(const std::vector<seq_action>& actions, const sentinel_t& sentinel) -> seq_action
105{
106 return sequence_impl(actions, sentinel, true);
107}
108
109auto together(const std::vector<seq_action>& actions, const sentinel_t& sentinel) -> seq_action
110{
111 if(actions.empty())
112 {
113 return {};
114 }
115
116 duration_t duration = 0ms;
117 for(const auto& action : actions)
118 {
119 duration = std::max(seq_private::get_duration(action), duration);
120 }
121
122 auto updater =
123 [actions = actions, sentinel = sentinel, start_required = true, finished = false](duration_t delta,
124 seq_action& self) mutable
125 {
126 if(finished)
127 {
128 return state_t::finished;
129 }
130
131 if(sentinel.expired())
132 {
133 finished = true;
134 return state_t::finished;
135 }
136
137 if(start_required)
138 {
139 for(auto& action : actions)
140 {
141 seq_private::start(action);
142 }
143 start_required = false;
144 }
145
147 {
148 for(auto& action : actions)
149 {
151 }
152 }
153
154 finished = true;
155 for(auto& action : actions)
156 {
157 const auto state = seq_private::update(action, delta);
158 finished &= (state == state_t::finished);
159 }
160
161 seq_private::update_elapsed(self, delta);
163 self.on_step.emit();
164
165 if(finished)
166 {
167 return state_t::finished;
168 }
169 return state_t::running;
170 };
171
172 auto creator = [updater = std::move(updater)]()
173 {
174 return updater;
175 };
176
177 auto action = seq_action(std::move(creator), duration, sentinel);
178 inspector::add_together_info(action, actions);
179 return action;
180}
181
182auto delay(const duration_t& duration, const sentinel_t& sentinel) -> seq_action
183{
184 auto updater = [sentinel, finished = false](duration_t delta, seq_action& self) mutable
185 {
186 if(finished)
187 {
188 return state_t::finished;
189 }
190
191 if(sentinel.expired())
192 {
193 finished = true;
194 return state_t::finished;
195 }
196
197 seq_private::update_elapsed(self, delta);
199
200 self.on_step.emit();
201
203 {
204 finished = true;
205 return state_t::finished;
206 }
207
208 return state_t::running;
209 };
210
211 auto creator = [updater = std::move(updater)]()
212 {
213 return updater;
214 };
215
216 auto action = seq_action(std::move(creator), duration, sentinel);
218 return action;
219}
220
221auto repeat_impl(const seq_action& action,
222 size_t times,
223 bool precise,
224 const sentinel_t& sentinel = hpp::eternal_sentinel()) -> seq_action
225{
226 auto updater = [action = action,
227 times = times,
228 sentinel = sentinel,
229 elapsed = size_t(0),
230 finished = false,
231 prev_overflow = duration_t::zero(),
232 precise](duration_t delta, seq_action& self) mutable
233 {
234 if(finished)
235 {
236 return state_t::finished;
237 }
238
239 if(sentinel.expired())
240 {
241 finished = true;
242 return state_t::finished;
243 }
244
245 if(elapsed > 0)
246 {
248 {
250 }
251 }
252
253 if(precise)
254 {
255 delta += prev_overflow;
256 }
257
258 auto state = seq_private::update(action, delta);
259 if(times > 0)
260 {
262 }
263
264 self.on_step.emit();
265
266 prev_overflow = duration_t::zero();
267 if(state == state_t::finished)
268 {
269 prev_overflow = seq_private::get_overflow(action);
270 if(elapsed > 0)
271 {
273 {
274 finished = true;
275 return state_t::finished;
276 }
277 }
278
279 if(times == 0)
280 {
281 seq_private::start(action);
282 elapsed++;
283 }
284 else
285 {
286 if(elapsed >= times)
287 {
288 finished = true;
289 return state_t::finished;
290 }
291
292 seq_private::start(action);
293
294 elapsed++;
295 }
296 }
297 return state_t::running;
298 };
299
300 auto creator = [updater = std::move(updater)]() mutable
301 {
302 return updater;
303 };
304
305 duration_t duration = 0ms;
306 if(times > 0)
307 {
308 duration = times * seq_private::get_duration(action);
309 }
310
311 auto new_action = seq_action(std::move(creator), duration, sentinel);
312 inspector::add_repeat_info(new_action, action, times);
313 return new_action;
314}
315
316auto repeat(const seq_action& action, size_t times) -> seq_action
317{
318 return repeat_impl(action, times, false);
319}
320
321auto repeat(const seq_action& action, const sentinel_t& sentinel, size_t times) -> seq_action
322{
323 return repeat_impl(action, times, false, sentinel);
324}
325
326auto repeat_precise(const seq_action& action, size_t times) -> seq_action
327{
328 return repeat_impl(action, times, true);
329}
330
331auto repeat_precise(const seq_action& action, const sentinel_t& sentinel, size_t times) -> seq_action
332{
333 return repeat_impl(action, times, true, sentinel);
334}
335
336} // namespace seq
float elapsed
void add_repeat_info(seq_action &repeat_action, const seq_action &action, size_t times)
void add_together_info(seq_action &action, const std::vector< seq_action > &actions)
void add_sequence_info(seq_action &action, const std::vector< seq_action > &actions)
void update_action_status(seq_action &action)
void add_delay_info(seq_action &action)
Provides a sequence-based action management system for controlling and scheduling actions.
hpp::sentinel sentinel_t
Alias for a sentinel object used for lifecycle management.
Definition seq_common.h:47
@ running
The action is running.
@ finished
The action has finished.
auto sequence_precise(const std::vector< seq_action > &actions, const sentinel_t &sentinel) -> seq_action
Creates a precise sequential action that executes a list of actions with exact timing.
Definition seq_core.cpp:104
seq::seq_action creator(const std::string &type, values_t< T > &values, T &begin, const T &end, seq::duration_t duration)
Definition tests.cpp:195
auto repeat(const seq_action &action, size_t times) -> seq_action
Repeats an action a specified number of times.
Definition seq_core.cpp:316
auto repeat_impl(const seq_action &action, size_t times, bool precise, const sentinel_t &sentinel=hpp::eternal_sentinel()) -> seq_action
Definition seq_core.cpp:221
auto repeat_precise(const seq_action &action, size_t times) -> seq_action
Precisely repeats an action a specified number of times.
Definition seq_core.cpp:326
auto delay(const duration_t &duration, const sentinel_t &sentinel) -> seq_action
Creates a delay action.
Definition seq_core.cpp:182
auto sequence(const std::vector< seq_action > &actions, const sentinel_t &sentinel) -> seq_action
Creates a sequential action that executes a list of actions one after another.
Definition seq_core.cpp:99
std::chrono::nanoseconds duration_t
Represents a duration in nanoseconds.
Definition seq_common.h:41
auto together(const std::vector< seq_action > &actions, const sentinel_t &sentinel) -> seq_action
Creates a simultaneous action that executes a list of actions together.
Definition seq_core.cpp:109
auto sequence_impl(const std::vector< seq_action > &actions, const sentinel_t &sentinel, bool precise) -> seq_action
Definition seq_core.cpp:7
Represents an action within the sequence management system. Contains lifecycle events and management ...
Definition seq_action.h:18
static auto get_duration(const seq_action &self) -> duration_t
Gets the total duration of a given action.
static void start(seq_action &action)
Starts a given action.
Definition seq_private.h:22
static auto update(seq_action &action, duration_t delta) -> state_t
Updates a given action with a time delta.
static void update_elapsed(seq_action &self, duration_t update_time)
Updates the elapsed time of a given action.
static void stop_when_finished(seq_action &action)
Marks a given action to stop when finished.
Definition seq_private.h:49
static auto get_overflow(const seq_action &self) -> duration_t
Computes the overflow duration of a given action if it exceeds its total duration.
static auto is_stop_when_finished_requested(const seq_action &action) -> bool
Checks if the action is requested to stop when finished.
static auto get_elapsed(const seq_action &self) -> duration_t
Gets the elapsed time of a given action.