DSZI_Survival/src/game/EventManager.py

48 lines
1.6 KiB
Python
Raw Normal View History

2020-04-02 17:30:25 +02:00
import pygame
2020-04-04 21:33:39 +02:00
from src.entities.Player import Rotations
2020-04-05 14:49:37 +02:00
# Player can move every given milliseconds
TIMEOUT = 100
2020-03-31 21:47:09 +02:00
class EventManager:
2020-04-05 14:49:37 +02:00
keyTimeout = 0
#self.game.map
2020-04-04 21:33:39 +02:00
def __init__(self, gameObject, player):
2020-04-02 17:30:25 +02:00
self.game = gameObject
2020-04-04 21:33:39 +02:00
self.player = player
2020-04-05 14:49:37 +02:00
self.keyTimer = pygame.time.Clock()
2020-04-04 21:33:39 +02:00
# Player controls
# TODO
def loadKeyboardSettings(self):
pass
2020-04-02 17:30:25 +02:00
def handleEvents(self):
2020-04-04 21:33:39 +02:00
pygame.event.pump()
keys = pygame.key.get_pressed()
2020-04-02 17:30:25 +02:00
for event in pygame.event.get():
if event.type == pygame.QUIT:
self.game.running = False
2020-04-05 14:49:37 +02:00
self.keyTimeout += self.keyTimer.tick()
if self.keyTimeout >= TIMEOUT:
self.handlePlayerControls(keys)
self.keyTimeout = 0
2020-04-04 21:33:39 +02:00
def handlePlayerControls(self, keys):
# Key names are temporary
# TODO: Load key bindings from JSON
2020-04-05 14:49:37 +02:00
2020-04-05 20:20:37 +02:00
if keys[pygame.K_w] and not self.game.map.collision(self.player.rect.x, self.player.rect.y - self.player.rect.w):
2020-04-04 21:33:39 +02:00
self.player.move(Rotations.NORTH)
2020-04-05 20:20:37 +02:00
if keys[pygame.K_s] and not self.game.map.collision(self.player.rect.x, self.player.rect.y + self.player.rect.w):
2020-04-04 21:33:39 +02:00
self.player.move(Rotations.SOUTH)
2020-04-05 20:20:37 +02:00
if keys[pygame.K_d] and not self.game.map.collision(self.player.rect.x + self.player.rect.w, self.player.rect.y):
2020-04-04 21:33:39 +02:00
self.player.move(Rotations.EAST)
2020-04-05 20:20:37 +02:00
if keys[pygame.K_a] and not self.game.map.collision(self.player.rect.x - self.player.rect.w, self.player.rect.y):
2020-04-04 21:33:39 +02:00
self.player.move(Rotations.WEST)
2020-04-05 20:20:37 +02:00