summary refs log tree commit diff stats
path: root/src/systems/automating.cpp
diff options
context:
space:
mode:
authorKelly Rauchenberger <fefferburbia@gmail.com>2018-05-03 14:41:01 -0400
committerKelly Rauchenberger <fefferburbia@gmail.com>2018-05-03 14:41:01 -0400
commit83f51a6892629921b4cc482b986656a0a5cc5f6a (patch)
tree50dcd4485a165b29591dafbec907c20dcf9284b1 /src/systems/automating.cpp
parentb2311e1ba43a72a205b51a24376a1f363faa569e (diff)
downloadtherapy-83f51a6892629921b4cc482b986656a0a5cc5f6a.tar.gz
therapy-83f51a6892629921b4cc482b986656a0a5cc5f6a.tar.bz2
therapy-83f51a6892629921b4cc482b986656a0a5cc5f6a.zip
Added simple AI implementation
The new AutomatingSystem and AutomatableComponent are responsible for simple AI tasks. This currently is limited to moving entities at a certain speed for certain periods of time. These tasks are arranged as a set of behaviors, which are picked randomly when automation starts or when a behavior finishes executing. A behavior is a sequence of actions that run one after another.

Currently, if an automated entity is blocked from moving by a collision, it will be coerced out of its intended path. This is because the automation parameters are stored as a speed and a duration, rather than a starting location and an ending location. This may end up being changed, or made configurable, as this is an early implementation of this feature and will need to be more complex later.

Added an RNG object to the Game class, so that the AutomatingSystem can pick behaviors at random.
Diffstat (limited to 'src/systems/automating.cpp')
-rw-r--r--src/systems/automating.cpp71
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
7void 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
64void 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}