summary refs log tree commit diff stats
path: root/src/systems/controlling.cpp
blob: e1609bdd37c2e9a8d530a878e0b365ae1a004781 (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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#include "controlling.h"
#include "game.h"
#include "components/controllable.h"
#include "components/orientable.h"
#include "systems/orienting.h"

void ControllingSystem::tick(double)
{
  while (!actions_.empty())
  {
    int key = actions_.front().first;
    int action = actions_.front().second;

    auto entities = game_.getEntityManager().getEntitiesWithComponents<
      ControllableComponent,
      OrientableComponent>();

    for (auto entity : entities)
    {
      auto& controllable = game_.getEntityManager().
        getComponent<ControllableComponent>(entity);

      auto& orienting = game_.getSystemManager().getSystem<OrientingSystem>();

      if (action == GLFW_PRESS)
      {
        if (key == controllable.getLeftKey())
        {
          controllable.setHoldingLeft(true);

          if (!controllable.isFrozen())
          {
            orienting.moveLeft(entity);
          }
        } else if (key == controllable.getRightKey())
        {
          controllable.setHoldingRight(true);

          if (!controllable.isFrozen())
          {
            orienting.moveRight(entity);
          }
        } else if (key == controllable.getJumpKey())
        {
          if (!controllable.isFrozen())
          {
            orienting.jump(entity);
          }
        } else if (key == controllable.getDropKey())
        {
          if (!controllable.isFrozen())
          {
            orienting.drop(entity);
          }
        }
      } else if (action == GLFW_RELEASE)
      {
        if (key == controllable.getLeftKey())
        {
          controllable.setHoldingLeft(false);

          if (!controllable.isFrozen())
          {
            if (controllable.isHoldingRight())
            {
              orienting.moveRight(entity);
            } else {
              orienting.stopWalking(entity);
            }
          }
        } else if (key == controllable.getRightKey())
        {
          controllable.setHoldingRight(false);

          if (!controllable.isFrozen())
          {
            if (controllable.isHoldingLeft())
            {
              orienting.moveLeft(entity);
            } else {
              orienting.stopWalking(entity);
            }
          }
        } else if (key == controllable.getDropKey())
        {
          if (!controllable.isFrozen())
          {
            orienting.stopDropping(entity);
          }
        } else if (key == controllable.getJumpKey())
        {
          if (!controllable.isFrozen())
          {
            orienting.stopJumping(entity);
          }
        }
      }
    }

    actions_.pop();
  }
}

void ControllingSystem::input(int key, int action)
{
  actions_.push(std::make_pair(key, action));
}