-
Notifications
You must be signed in to change notification settings - Fork 0
/
input.cpp
72 lines (63 loc) · 1.75 KB
/
input.cpp
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
#include "input.hpp"
using namespace input;
KeyboardInputHandler::KeyboardInputHandler(
sf::Keyboard::Key up,
sf::Keyboard::Key down,
sf::Keyboard::Key left,
sf::Keyboard::Key right,
sf::Keyboard::Key drop) {
this->action_map = {
{Action::UP, up},
{Action::DOWN, down},
{Action::LEFT, left},
{Action::RIGHT, right},
{Action::DROP, drop}
};
}
float KeyboardInputHandler::get_value(const Action ac) {
return sf::Keyboard::isKeyPressed(this->action_map[ac]);
}
bool KeyboardInputHandler::is_connected() const {
return true;
}
ControllerInputHandler::ControllerInputHandler(int id) :
id{id}
{}
float ControllerInputHandler::get_value(const Action ac) {
auto axis_x = sf::Joystick::getAxisPosition(id, sf::Joystick::X) / 100;
auto axis_y = sf::Joystick::getAxisPosition(id, sf::Joystick::Y) / 100;
switch (ac) {
case Action::UP:
if (axis_y < 0) {
return -axis_y;
}
break;
case Action::DOWN:
if (axis_y > 0) {
return axis_y;
}
break;
case Action::LEFT:
if (axis_x < 0) {
return -axis_x;
}
break;
case Action::RIGHT:
if (axis_x > 0) {
return axis_x;
}
break;
case Action::NONE:
return 0;
break;
case Action::DROP:
if(sf::Joystick::isButtonPressed(this->id, 2)){
return 1;
}
break;
}
return 0;
}
bool ControllerInputHandler::is_connected() const {
return sf::Joystick::isConnected(this->id);
}