diff options
Diffstat (limited to 'src/systems/automating.cpp')
-rw-r--r-- | src/systems/automating.cpp | 71 |
1 files changed, 71 insertions, 0 deletions
diff --git a/src/systems/automating.cpp b/src/systems/automating.cpp new file mode 100644 index 0000000..0d85957 --- /dev/null +++ b/src/systems/automating.cpp | |||
@@ -0,0 +1,71 @@ | |||
1 | #include "automating.h" | ||
2 | #include "game.h" | ||
3 | #include "components/automatable.h" | ||
4 | #include "components/ponderable.h" | ||
5 | #include "systems/pondering.h" | ||
6 | |||
7 | void AutomatingSystem::tick(double dt) | ||
8 | { | ||
9 | auto entities = game_.getEntityManager().getEntitiesWithComponents< | ||
10 | AutomatableComponent, | ||
11 | PonderableComponent>(); | ||
12 | |||
13 | for (id_type entity : entities) | ||
14 | { | ||
15 | auto& automatable = game_.getEntityManager(). | ||
16 | getComponent<AutomatableComponent>(entity); | ||
17 | |||
18 | if (!automatable.active) | ||
19 | { | ||
20 | continue; | ||
21 | } | ||
22 | |||
23 | if (automatable.behaviorRunning && | ||
24 | (automatable.remaining <= 0.0)) | ||
25 | { | ||
26 | automatable.currentAction++; | ||
27 | automatable.actionRunning = false; | ||
28 | |||
29 | if (automatable.currentAction == | ||
30 | automatable.behaviors[automatable.currentBehavior].size()) | ||
31 | { | ||
32 | automatable.behaviorRunning = false; | ||
33 | } | ||
34 | } | ||
35 | |||
36 | if (!automatable.behaviorRunning) | ||
37 | { | ||
38 | automatable.currentBehavior = automatable.behaviorDist(game_.getRng()); | ||
39 | automatable.currentAction = 0; | ||
40 | automatable.behaviorRunning = true; | ||
41 | } | ||
42 | |||
43 | AutomatableComponent::Action& curAction = | ||
44 | automatable.behaviors | ||
45 | [automatable.currentBehavior] | ||
46 | [automatable.currentAction]; | ||
47 | |||
48 | if (!automatable.actionRunning) | ||
49 | { | ||
50 | automatable.remaining = curAction.dur; | ||
51 | automatable.actionRunning = true; | ||
52 | } | ||
53 | |||
54 | auto& ponderable = game_.getEntityManager(). | ||
55 | getComponent<PonderableComponent>(entity); | ||
56 | |||
57 | ponderable.velX = curAction.speedX; | ||
58 | ponderable.velY = curAction.speedY; | ||
59 | |||
60 | automatable.remaining -= dt; | ||
61 | } | ||
62 | } | ||
63 | |||
64 | void AutomatingSystem::initPrototype(id_type prototype) | ||
65 | { | ||
66 | auto& automatable = game_.getEntityManager(). | ||
67 | getComponent<AutomatableComponent>(prototype); | ||
68 | |||
69 | automatable.behaviorRunning = false; | ||
70 | automatable.actionRunning = false; | ||
71 | } | ||