diff options
| author | Kelly Rauchenberger <fefferburbia@gmail.com> | 2018-02-18 15:25:52 -0500 |
|---|---|---|
| committer | Kelly Rauchenberger <fefferburbia@gmail.com> | 2018-02-18 15:25:52 -0500 |
| commit | e4e2f2d2a7b6a282b9618aa0004d9453936f43c6 (patch) | |
| tree | de1653dc8b5992420147f28d9fb4de052ea1845f /src/systems/scheduling.cpp | |
| parent | e16fb5be90c889c371cbb0ca2444735c2e12073c (diff) | |
| download | therapy-e4e2f2d2a7b6a282b9618aa0004d9453936f43c6.tar.gz therapy-e4e2f2d2a7b6a282b9618aa0004d9453936f43c6.tar.bz2 therapy-e4e2f2d2a7b6a282b9618aa0004d9453936f43c6.zip | |
Added player death and event scheduling
Also added ability to make sprites flicker, to freeze physics for an entity, and to freeze progression of a sprite's animation loop.
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 | } | ||
