-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInputMethods.cpp
More file actions
52 lines (48 loc) · 1021 Bytes
/
Copy pathInputMethods.cpp
File metadata and controls
52 lines (48 loc) · 1021 Bytes
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
#include "InputMethods.h"
#include "Player.h"
#ifdef ARDUINO_AVR_UNO
#include <Arduino.h>
#define THRESHOLD 256
void JoystickInput::handle(Player* player) {
long xIn = (512 - analogRead(pinX)) * xMult;
long yIn = (512 - analogRead(pinY)) * yMult;
if ((xIn * xIn + yIn * yIn) > ((long)THRESHOLD * THRESHOLD)) {
if (abs(xIn) > abs(yIn)) {
player->move(xIn < 0 ? LEFT : RIGHT);
} else {
player->move(yIn < 0 ? DOWN : UP);
}
}
if (!digitalRead(pinButton)) {
player->activate();
}
}
#else
#include <iostream>
void ConsoleInput::handle(Player* player) {
char input;
std::cin >> input;
if (input == 'u')
{
player->move(Direction::UP);
}
else if (input == 'd')
{
player->move(Direction::DOWN);
}
else if (input == 'r')
{
player->move(Direction::RIGHT);
}
else if (input == 'l')
{
player->move(Direction::LEFT);
}
else if (input == 'x') {
player->activate();
}
else {
std::cout << "invalid input" << std::endl;
}
}
#endif