diff options
Diffstat (limited to 'src/systems/scheduling.cpp')
-rw-r--r-- | src/systems/scheduling.cpp | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/src/systems/scheduling.cpp b/src/systems/scheduling.cpp new file mode 100644 index 0000000..220efae --- /dev/null +++ b/src/systems/scheduling.cpp | |||
@@ -0,0 +1,54 @@ | |||
1 | #include "scheduling.h" | ||
2 | #include "game.h" | ||
3 | #include "components/schedulable.h" | ||
4 | #include "util.h" | ||
5 | |||
6 | void SchedulingSystem::tick(double dt) | ||
7 | { | ||
8 | auto entities = game_.getEntityManager().getEntitiesWithComponents< | ||
9 | SchedulableComponent>(); | ||
10 | |||
11 | for (id_type entity : entities) | ||
12 | { | ||
13 | auto& schedulable = game_.getEntityManager(). | ||
14 | getComponent<SchedulableComponent>(entity); | ||
15 | |||
16 | for (auto& action : schedulable.actions) | ||
17 | { | ||
18 | std::get<0>(action) -= dt; | ||
19 | |||
20 | if (std::get<0>(action) < 0) | ||
21 | { | ||
22 | std::get<1>(action)(entity); | ||
23 | } | ||
24 | } | ||
25 | |||
26 | erase_if(schedulable.actions, | ||
27 | [] (const SchedulableComponent::Action& action) { | ||
28 | return (std::get<0>(action) < 0); | ||
29 | }); | ||
30 | |||
31 | if (schedulable.actions.empty()) | ||
32 | { | ||
33 | game_.getEntityManager().removeComponent<SchedulableComponent>(entity); | ||
34 | } | ||
35 | } | ||
36 | } | ||
37 | |||
38 | void SchedulingSystem::schedule( | ||
39 | id_type entity, | ||
40 | double length, | ||
41 | std::function<void(id_type)> action) | ||
42 | { | ||
43 | if (!game_.getEntityManager().hasComponent<SchedulableComponent>(entity)) | ||
44 | { | ||
45 | game_.getEntityManager().emplaceComponent<SchedulableComponent>(entity); | ||
46 | } | ||
47 | |||
48 | auto& schedulable = game_.getEntityManager(). | ||
49 | getComponent<SchedulableComponent>(entity); | ||
50 | |||
51 | schedulable.actions.emplace_back( | ||
52 | length, | ||
53 | std::move(action)); | ||
54 | } | ||