-
Notifications
You must be signed in to change notification settings - Fork 2
/
controller.py
95 lines (65 loc) · 1.67 KB
/
controller.py
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
"""
Represent various videogame controllers
TODO: Various play schemes/configs
XXX: UNTESTED
"""
import re
def setup_controls(joystick):
"""
Joystick wrapper.
"""
if re.search('playstation', joystick.get_name(), re.I):
return Ps3Controller(joystick)
elif re.search('playstation', joystick.get_name(), re.I):
return XboxController(joystick)
return Controller(joystick)
class Controller(object):
def __init__(self, joystick):
"""Pass a PyGame joystick instance."""
self.js = joystick
def getLeftHori(self):
return 0.0
def getLeftVert(self):
return 0.0
def getRightHori(self):
return 0.0
def getRightVert(self):
return 0.0
def getLeftTrigger(self):
return 0.0
def getRightTrigger(self):
return 0.0
class XboxController(Controller):
def __init__(self, joystick):
super(XboxController, self).__init__(joystick)
def getLeftHori(self):
return self.js.get_axis(0)
def getLeftVert(self):
return self.js.get_axis(1)
def getRightHori(self):
return self.js.get_axis(3)
def getRightVert(self):
return self.js.get_axis(4)
def getLeftTrigger(self):
# TODO: Verify
return self.js.get_axis(2)
def getRightTrigger(self):
# TODO: Verify
return self.js.get_axis(5)
class Ps3Controller(Controller):
def __init__(self, joystick):
super(Ps3Controller, self).__init__(joystick)
def getLeftHori(self):
return self.js.get_axis(0)
def getLeftVert(self):
return self.js.get_axis(1)
def getRightHori(self):
return self.js.get_axis(2)
def getRightVert(self):
return self.js.get_axis(3)
def getLeftTrigger(self):
# TODO: Verify
return self.js.get_axis(12)
def getRightTrigger(self):
# TODO: Verify
return self.js.get_axis(13)