summary refs log tree commit diff stats
path: root/src/systems/scheduling.cpp
diff options
context:
space:
mode:
authorKelly Rauchenberger <fefferburbia@gmail.com>2018-05-17 15:55:37 -0400
committerGitHub <noreply@github.com>2018-05-17 15:55:37 -0400
commit90aadf3844386824140a20d7fbb847bc16009a94 (patch)
tree6f83fce90e71abb22b1a8f3e09c79963b2a34d5d /src/systems/scheduling.cpp
parentbc63fa57ced1c7329f7fdcfd168eaf7e290158bc (diff)
parent86f0106d0523825549f1e74b835688c78a10cf6c (diff)
downloadtherapy-90aadf3844386824140a20d7fbb847bc16009a94.tar.gz
therapy-90aadf3844386824140a20d7fbb847bc16009a94.tar.bz2
therapy-90aadf3844386824140a20d7fbb847bc16009a94.zip
Merge pull request #7 from hatkirby/es-rewrite
The ECS rewrite exceeds the original branch in functionality, so it is time to merge it in.
Diffstat (limited to 'src/systems/scheduling.cpp')
-rw-r--r--src/systems/scheduling.cpp54
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
6void 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
38void 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}