summary refs log tree commit diff stats
path: root/src/systems/automating.cpp
blob: 61b97d92845d7a602b100e28b67a04c53a87f9d3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include "automating.h"
#include "game.h"
#include "components/automatable.h"
#include "components/ponderable.h"
#include "systems/pondering.h"

void AutomatingSystem::tick(double dt)
{
  auto entities = game_.getEntityManager().getEntitiesWithComponents<
    AutomatableComponent,
    PonderableComponent>();

  for (id_type entity : entities)
  {
    auto& automatable = game_.getEntityManager().
      getComponent<AutomatableComponent>(entity);

    if (!automatable.active)
    {
      continue;
    }

    if (automatable.behaviorRunning &&
        (automatable.remaining <= 0.0))
    {
      automatable.currentAction++;
      automatable.actionRunning = false;

      if (automatable.currentAction ==
            automatable.behaviors[automatable.currentBehavior].size())
      {
        automatable.behaviorRunning = false;
      }
    }

    if (!automatable.behaviorRunning)
    {
      automatable.currentBehavior = automatable.behaviorDist(game_.getRng());
      automatable.currentAction = 0;
      automatable.behaviorRunning = true;
    }

    AutomatableComponent::Action& curAction =
      automatable.behaviors
        [automatable.currentBehavior]
          [automatable.currentAction];

    if (!automatable.actionRunning)
    {
      automatable.remaining = curAction.dur;
      automatable.actionRunning = true;
    }

    auto& ponderable = game_.getEntityManager().
      getComponent<PonderableComponent>(entity);

    ponderable.vel = curAction.speed;

    automatable.remaining -= dt;
  }
}

void AutomatingSystem::initPrototype(id_type prototype)
{
  auto& automatable = game_.getEntityManager().
    getComponent<AutomatableComponent>(prototype);

  automatable.behaviorRunning = false;
  automatable.actionRunning = false;
}