2020-04-26 03:59:09 +02:00
|
|
|
from random import Random
|
2020-04-06 13:42:56 +02:00
|
|
|
from time import sleep
|
|
|
|
|
2020-04-02 17:30:25 +02:00
|
|
|
import pygame
|
|
|
|
|
2020-04-06 10:38:12 +02:00
|
|
|
from src.entities.Interactable import Interactable
|
2020-04-06 00:40:16 +02:00
|
|
|
from src.entities.Pickupable import Pickupable
|
2020-04-25 22:02:21 +02:00
|
|
|
from src.entities.Player import Movement
|
2020-04-04 21:33:39 +02:00
|
|
|
|
2020-04-05 14:49:37 +02:00
|
|
|
|
2020-05-10 12:53:08 +02:00
|
|
|
class EventManager:
|
2020-04-04 21:33:39 +02:00
|
|
|
def __init__(self, gameObject, player):
|
2020-05-10 12:53:08 +02:00
|
|
|
# TODO: Is this really necessary?
|
2020-04-02 17:30:25 +02:00
|
|
|
self.game = gameObject
|
2020-05-09 22:30:21 +02:00
|
|
|
|
2020-04-04 21:33:39 +02:00
|
|
|
self.player = player
|
2020-04-06 13:48:36 +02:00
|
|
|
|
2020-05-09 22:30:21 +02:00
|
|
|
# TODO: Make this not retarded
|
2020-04-06 13:48:36 +02:00
|
|
|
self.turnOff = False
|
2020-04-04 21:33:39 +02:00
|
|
|
|
|
|
|
# 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()
|
|
|
|
|
2020-04-06 13:48:36 +02:00
|
|
|
if self.turnOff:
|
|
|
|
sleep(5)
|
|
|
|
exit(0)
|
|
|
|
|
2020-05-09 22:30:21 +02:00
|
|
|
# TODO: Move to ui.update()
|
2020-04-06 01:00:02 +02:00
|
|
|
self.game.screen.ui.updateTime()
|
2020-04-05 23:57:53 +02:00
|
|
|
|
2020-04-04 21:33:39 +02:00
|
|
|
keys = pygame.key.get_pressed()
|
2020-05-10 12:53:08 +02:00
|
|
|
|
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-26 17:58:22 +02:00
|
|
|
if event.type == pygame.MOUSEBUTTONUP:
|
|
|
|
pos = pygame.mouse.get_pos()
|
2020-04-26 18:45:07 +02:00
|
|
|
self.handleClickingOnCollidablesAndTerrains(pos)
|
2020-05-09 22:30:21 +02:00
|
|
|
# TODO: Move to ui.update()
|
2020-04-05 23:45:19 +02:00
|
|
|
self.game.screen.ui.updateBasedOnPygameEvent(event)
|
2020-05-10 12:53:08 +02:00
|
|
|
if self.player.alive:
|
|
|
|
# TODO: Add A* here?
|
|
|
|
self.handlePlayerControls(keys)
|
|
|
|
else:
|
|
|
|
self.game.screen.ui.updateOnDeath(self.player)
|
|
|
|
self.turnOff = True
|
2020-04-04 21:33:39 +02:00
|
|
|
|
2020-05-10 12:53:08 +02:00
|
|
|
# TODO: Move to ui.update()
|
2020-04-06 13:20:28 +02:00
|
|
|
self.game.screen.ui.updateBarsBasedOnPlayerStats(self.player.statistics)
|
|
|
|
|
2020-04-26 18:45:07 +02:00
|
|
|
def handleClickingOnCollidablesAndTerrains(self, pos):
|
|
|
|
# get a list of all collidables that are under the mouse cursor
|
|
|
|
clicked_collidables = [s for s in self.game.map.collidables if s.rect.collidepoint(pos)]
|
|
|
|
|
|
|
|
if len(clicked_collidables) > 0:
|
|
|
|
self.game.movement.gotoToTarget(Random().choice(clicked_collidables))
|
|
|
|
else:
|
|
|
|
# get a list of all terrains that are under the mouse cursor
|
|
|
|
clicked_terrains = [tile for tile in self.game.map.terrainTilesList if tile.rect.collidepoint(pos)]
|
|
|
|
if len(clicked_terrains) > 0:
|
|
|
|
print("Terrains under clik:")
|
|
|
|
for terrain in clicked_terrains:
|
|
|
|
print(terrain)
|
|
|
|
else:
|
|
|
|
print("NO TERRAIN FOUND UNDER CLICK")
|
|
|
|
|
2020-04-04 21:33:39 +02:00
|
|
|
def handlePlayerControls(self, keys):
|
|
|
|
# Key names are temporary
|
|
|
|
# TODO: Load key bindings from JSON
|
2020-04-06 10:38:12 +02:00
|
|
|
|
2020-04-06 13:42:56 +02:00
|
|
|
# Picking up items
|
|
|
|
if keys[pygame.K_SPACE]:
|
|
|
|
object = self.game.map.getEntityOnCoord(self.player.getFacingCoord())
|
|
|
|
# Picked up item gets removed from the map
|
|
|
|
if type(object) is Pickupable:
|
|
|
|
object.on_interaction(self.player)
|
|
|
|
self.game.screen.ui.updateOnPlayerPickup(self.player.statistics, object)
|
|
|
|
self.game.map.removeSpriteFromMap(object)
|
|
|
|
elif type(object) is Interactable:
|
|
|
|
object.on_interaction(self.player)
|
|
|
|
self.game.screen.ui.updateOnPlayerInteraction(self.player.statistics, object)
|
2020-04-06 10:38:12 +02:00
|
|
|
|
2020-04-06 13:42:56 +02:00
|
|
|
if keys[pygame.K_w]:
|
2020-05-09 22:30:21 +02:00
|
|
|
if not self.game.map.collision(self.player.getFacingCoord()[0], self.player.getFacingCoord()[1]):
|
|
|
|
self.player.move(Movement.FORWARD)
|
2020-04-06 13:42:56 +02:00
|
|
|
if keys[pygame.K_a]:
|
2020-04-25 22:02:21 +02:00
|
|
|
self.player.move(Movement.ROTATE_L)
|
|
|
|
if keys[pygame.K_d]:
|
|
|
|
self.player.move(Movement.ROTATE_R)
|
2020-04-04 21:33:39 +02:00
|
|
|
|
2020-04-26 14:13:27 +02:00
|
|
|
# Pick random target for A* algorithm
|
2020-04-26 03:59:09 +02:00
|
|
|
if keys[pygame.K_u]:
|
2020-04-26 14:13:27 +02:00
|
|
|
while True:
|
|
|
|
try:
|
2020-04-26 17:58:22 +02:00
|
|
|
self.game.movement.gotoToTarget(Random().choice(self.game.map.entities))
|
2020-04-26 14:13:27 +02:00
|
|
|
break
|
2020-04-26 16:33:39 +02:00
|
|
|
except IndexError:
|
2020-04-26 14:13:27 +02:00
|
|
|
pass
|
2020-04-05 20:20:37 +02:00
|
|
|
|
2020-04-05 19:08:08 +02:00
|
|
|
|