-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinput_manager.py
100 lines (86 loc) · 4.23 KB
/
input_manager.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
95
96
97
98
99
100
import pyray
class InputManager:
def __init__(self, dead_zone=0.2, mouse_sensitivity=5.0):
self.controller_connected = pyray.is_gamepad_available(0)
self.dead_zone = dead_zone
self.mouse_sensitivity = mouse_sensitivity
self.left_trigger_pressed = False
self.right_trigger_pressed = False
self.left_trigger_state = False
self.right_trigger_state = False
self.left_back_pressed = False
self.right_back_pressed = False
self.left_back_state = False
self.right_back_state = False
def apply_dead_zone(self, value):
if abs(value) < self.dead_zone:
return 0
return value
def get_horizontal_input(self):
if self.controller_connected:
return self.apply_dead_zone(pyray.get_gamepad_axis_movement(0, pyray.GamepadAxis.GAMEPAD_AXIS_LEFT_X))
else:
return int(pyray.is_key_down(pyray.KeyboardKey.KEY_D)) - int(pyray.is_key_down(pyray.KeyboardKey.KEY_A))
def get_vertical_input(self):
if self.controller_connected:
return self.apply_dead_zone(pyray.get_gamepad_axis_movement(0, pyray.GamepadAxis.GAMEPAD_AXIS_LEFT_Y))
else:
return int(pyray.is_key_down(pyray.KeyboardKey.KEY_S)) - int(pyray.is_key_down(pyray.KeyboardKey.KEY_W))
def is_jump_pressed(self):
if self.controller_connected:
return pyray.is_gamepad_button_down(0, pyray.GamepadButton.GAMEPAD_BUTTON_RIGHT_FACE_DOWN)
else:
return pyray.is_key_down(pyray.KeyboardKey.KEY_SPACE)
def get_right_stick_horizontal(self):
if self.controller_connected:
return self.apply_dead_zone(pyray.get_gamepad_axis_movement(0, pyray.GamepadAxis.GAMEPAD_AXIS_RIGHT_X))
return 0
def get_right_stick_vertical(self):
if self.controller_connected:
return self.apply_dead_zone(pyray.get_gamepad_axis_movement(0, pyray.GamepadAxis.GAMEPAD_AXIS_RIGHT_Y))
return 0
def update_mouse_position(self):
if self.controller_connected:
right_stick_x = self.get_right_stick_horizontal()
right_stick_y = self.get_right_stick_vertical()
if right_stick_x != 0 or right_stick_y != 0:
mouse_position = pyray.get_mouse_position()
new_mouse_x = mouse_position.x + right_stick_x * self.mouse_sensitivity
new_mouse_y = mouse_position.y + right_stick_y * self.mouse_sensitivity
pyray.set_mouse_position(int(new_mouse_x), int(new_mouse_y))
def is_left_trigger_pressed(self):
current_state = pyray.is_gamepad_button_down(0, pyray.GamepadButton.GAMEPAD_BUTTON_LEFT_TRIGGER_2)
if current_state and not self.left_trigger_state:
self.left_trigger_state = True
return True
elif not current_state:
self.left_trigger_state = False
return False
def is_right_trigger_pressed(self):
current_state = pyray.is_gamepad_button_down(0, pyray.GamepadButton.GAMEPAD_BUTTON_RIGHT_TRIGGER_2)
if current_state and not self.right_trigger_state:
self.right_trigger_state = True
return True
elif not current_state:
self.right_trigger_state = False
return False
def is_right_trigger_held(self):
return pyray.is_gamepad_button_down(0, pyray.GamepadButton.GAMEPAD_BUTTON_RIGHT_TRIGGER_2)
def is_left_back_pressed(self):
current_state = pyray.is_gamepad_button_down(0, pyray.GamepadButton.GAMEPAD_BUTTON_LEFT_TRIGGER_1)
if current_state and not self.left_back_state:
self.left_back_state = True
return True
elif not current_state:
self.left_back_state = False
return False
def is_right_back_pressed(self):
current_state = pyray.is_gamepad_button_down(0, pyray.GamepadButton.GAMEPAD_BUTTON_RIGHT_TRIGGER_1)
if current_state and not self.right_back_state:
self.right_back_state = True
return True
elif not current_state:
self.right_back_state = False
return False
def is_slide_pressed(self):
return self.controller_connected and pyray.is_gamepad_button_down(0, pyray.GamepadButton.GAMEPAD_BUTTON_RIGHT_FACE_LEFT)