-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInputManager.py
64 lines (53 loc) · 2.1 KB
/
InputManager.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
import pygame
from pygame.locals import K_a, K_d, K_s, K_w, K_ESCAPE, K_r, K_t
from GameController import GameController
from AgentInput import AgentInput
from HaxballEngine.Properties import Properties, InternalProperties
class InputManager:
KEY_EXIT = K_ESCAPE
KEY_UP = K_w
KEY_DOWN = K_s
KEY_LEFT = K_a
KEY_RIGHT = K_d
KEY_RESET_BALL = K_r
KEY_TEST_MODE = K_t
@staticmethod
def parseUserInputs(gameController: GameController, userInput: AgentInput) -> bool:
"""
Parses user inputs and returns true if the game should be closed.
"""
shouldClose = False
userInput.reset()
# Parse event inputs
for event in pygame.event.get():
if event.type == pygame.QUIT:
shouldClose = True
elif event.type == pygame.KEYDOWN:
if event.key == InputManager.KEY_TEST_MODE:
Properties.DEBUG_MODE = not Properties.DEBUG_MODE
if event.key == InputManager.KEY_RESET_BALL:
gameController.engine.balls[0].setMovement(
pygame.Vector2(0, 0),
pygame.Vector2(
(
InternalProperties.PITCH_CENTER[0],
InternalProperties.PITCH_CENTER[1],
)
),
)
elif event.type == pygame.MOUSEBUTTONDOWN:
userInput.kick = True
userInput.kickPos = pygame.Vector2(pygame.mouse.get_pos())
# Parse keyboard inputs
pygameKeys = pygame.key.get_pressed()
if pygameKeys[InputManager.KEY_UP]:
userInput.movementDir += (0, -1)
if pygameKeys[InputManager.KEY_DOWN]:
userInput.movementDir += (0, 1)
if pygameKeys[InputManager.KEY_RIGHT]:
userInput.movementDir += (1, 0)
if pygameKeys[InputManager.KEY_LEFT]:
userInput.movementDir += (-1, 0)
if pygameKeys[InputManager.KEY_EXIT]:
shouldClose = True
return shouldClose