49 lines
1.7 KiB
Python
49 lines
1.7 KiB
Python
import pygame
|
|
|
|
from Classes.Game.BoardField import BoardField
|
|
|
|
class InputHandler:
|
|
# returns pressed key
|
|
@staticmethod
|
|
def getPressed():
|
|
return pygame.key.get_pressed()
|
|
|
|
# takes in movement inputs and maps them to x and y axis
|
|
@staticmethod
|
|
def getInputAxis() -> tuple:
|
|
xvel = 0
|
|
yvel = 0
|
|
|
|
# construct x and y velocity input axis
|
|
if InputHandler.getPressed()[pygame.K_a] or InputHandler.getPressed()[pygame.K_LEFT]:
|
|
xvel = -1
|
|
if InputHandler.getPressed()[pygame.K_d] or InputHandler.getPressed()[pygame.K_RIGHT]:
|
|
xvel = 1
|
|
if InputHandler.getPressed()[pygame.K_w] or InputHandler.getPressed()[pygame.K_UP]:
|
|
yvel = -1
|
|
if InputHandler.getPressed()[pygame.K_s] or InputHandler.getPressed()[pygame.K_DOWN]:
|
|
yvel = 1
|
|
|
|
return tuple((xvel, yvel))
|
|
|
|
@staticmethod
|
|
def getMousePos() -> pygame.Vector2:
|
|
return pygame.Vector2(pygame.mouse.get_pos())
|
|
|
|
# get field under mousbutton
|
|
@staticmethod
|
|
def getMouseHover(mouse_pos: pygame.Vector2, world_card_width: int, world_card_height: int, board_fields: list) -> BoardField:
|
|
x_pos = mouse_pos.x / world_card_width
|
|
y_pos = mouse_pos.y / world_card_height
|
|
|
|
for field in board_fields:
|
|
field_x = field.getPos().x
|
|
field_y = field.getPos().y
|
|
field_width = world_card_width # Annahme: Jedes Feld hat eine Breite von 1 Einheit
|
|
field_height = world_card_height # Annahme: Jedes Feld hat eine Höhe von 1 Einheit
|
|
|
|
if field_x <= x_pos < field_x + field_width and field_y <= y_pos < field_y + field_height:
|
|
return field
|
|
|
|
return None
|