summary refs log tree commit diff stats
path: root/src/components/user_movement.cpp
blob: e499feec855089fc5858c5eb0ceb88bec63b5f25 (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
#include "user_movement.h"
#include "renderer.h"

void UserMovementComponent::input(Game& game, Entity& entity, int key, int action)
{
  if (action == GLFW_PRESS)
  {
    if (key == GLFW_KEY_LEFT)
    {
      holdingLeft = true;
      
      if (!frozen)
      {
        entity.send(game, Message::Type::walkLeft);
      }
    } else if (key == GLFW_KEY_RIGHT)
    {
      holdingRight = true;
      
      if (!frozen)
      {
        entity.send(game, Message::Type::walkRight);
      }
    } else if (key == GLFW_KEY_UP)
    {
      if (!frozen)
      {
        entity.send(game, Message::Type::jump);
      }
    } else if (key == GLFW_KEY_DOWN)
    {
      if (!frozen)
      {
        entity.send(game, Message::Type::canDrop);
      }
    }
  } else if (action == GLFW_RELEASE)
  {
    if (key == GLFW_KEY_LEFT)
    {
      holdingLeft = false;
      
      if (!frozen)
      {
        if (holdingRight)
        {
          entity.send(game, Message::Type::walkRight);
        } else {
          entity.send(game, Message::Type::stopWalking);
        }
      }
    } else if (key == GLFW_KEY_RIGHT)
    {
      holdingRight = false;
      
      if (!frozen)
      {
        if (holdingLeft)
        {
          entity.send(game, Message::Type::walkLeft);
        } else {
          entity.send(game, Message::Type::stopWalking);
        }
      }
    } else if (key == GLFW_KEY_DOWN)
    {
      if (!frozen)
      {
        entity.send(game, Message::Type::cantDrop);
      }
    } else if (key == GLFW_KEY_UP)
    {
      if (!frozen)
      {
        entity.send(game, Message::Type::stopJump);
      }
    }
  }
}

void UserMovementComponent::receive(Game& game, Entity& entity, const Message& msg)
{
  if (msg.type == Message::Type::die)
  {
    frozen = true;
    
    entity.send(game, Message::Type::stopWalking);
  } else if (msg.type == Message::Type::stopDying)
  {
    frozen = false;
    
    if (holdingLeft)
    {
      entity.send(game, Message::Type::walkLeft);
    } else if (holdingRight)
    {
      entity.send(game, Message::Type::walkRight);
    }
  }
}