diff --git a/src/game/EventManager.py b/src/game/EventManager.py index 7975494..ef84a55 100644 --- a/src/game/EventManager.py +++ b/src/game/EventManager.py @@ -1,3 +1,5 @@ +from time import sleep + import pygame from src.entities.Interactable import Interactable @@ -14,6 +16,8 @@ class EventManager: self.game = gameObject self.player = player self.keyTimer = pygame.time.Clock() + + self.turnOff = False # Player controls # TODO @@ -23,6 +27,10 @@ class EventManager: def handleEvents(self): pygame.event.pump() + if self.turnOff: + sleep(5) + exit(0) + self.game.screen.ui.updateTime() keys = pygame.key.get_pressed() @@ -32,43 +40,48 @@ class EventManager: self.game.screen.ui.updateBasedOnPygameEvent(event) self.keyTimeout += self.keyTimer.tick() if self.keyTimeout >= TIMEOUT: - self.handlePlayerControls(keys) - self.keyTimeout = 0 + if self.player.alive: + self.handlePlayerControls(keys) + self.keyTimeout = 0 + else: + self.game.screen.ui.updateOnDeath(self.player) + self.turnOff = True - self.game.screen.ui.updateBasedOnPlayerStats(self.player.statistics) + self.game.screen.ui.updateBarsBasedOnPlayerStats(self.player.statistics) def handlePlayerControls(self, keys): # Key names are temporary # TODO: Load key bindings from JSON - if self.player.alive: - # 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.map.removeSpriteFromMap(object) - elif type(object) is Interactable: - object.on_interaction(self.player) + # 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) - # Movement - if keys[pygame.K_w]: - self.player.rotate(Rotations.NORTH) - if not self.game.map.collision(self.player.rect.x, self.player.rect.y - self.player.rect.w): - self.player.move(Rotations.NORTH) - if keys[pygame.K_s]: - self.player.rotate(Rotations.SOUTH) - if not self.game.map.collision(self.player.rect.x, self.player.rect.y + self.player.rect.w): - self.player.move(Rotations.SOUTH) - if keys[pygame.K_d]: - self.player.rotate(Rotations.EAST) - if not self.game.map.collision(self.player.rect.x + self.player.rect.w, self.player.rect.y): - self.player.move(Rotations.EAST) - if keys[pygame.K_a]: - self.player.rotate(Rotations.WEST) - if not self.game.map.collision(self.player.rect.x - self.player.rect.w, self.player.rect.y): - self.player.move(Rotations.WEST) + # Movement + if keys[pygame.K_w]: + self.player.rotate(Rotations.NORTH) + if not self.game.map.collision(self.player.rect.x, self.player.rect.y - self.player.rect.w): + self.player.move(Rotations.NORTH) + if keys[pygame.K_s]: + self.player.rotate(Rotations.SOUTH) + if not self.game.map.collision(self.player.rect.x, self.player.rect.y + self.player.rect.w): + self.player.move(Rotations.SOUTH) + if keys[pygame.K_d]: + self.player.rotate(Rotations.EAST) + if not self.game.map.collision(self.player.rect.x + self.player.rect.w, self.player.rect.y): + self.player.move(Rotations.EAST) + if keys[pygame.K_a]: + self.player.rotate(Rotations.WEST) + if not self.game.map.collision(self.player.rect.x - self.player.rect.w, self.player.rect.y): + self.player.move(Rotations.WEST) diff --git a/src/ui/Ui.py b/src/ui/Ui.py index dcdfd31..4a87745 100644 --- a/src/ui/Ui.py +++ b/src/ui/Ui.py @@ -2,6 +2,7 @@ from enum import Enum import pygame +from src.entities.Player import StatisticNames from src.entities.Statistics import Statistics from src.ui.UiBar import UiBar from src.ui.UiConsole import UiConsole @@ -62,26 +63,16 @@ class Ui(): screenHeight - self.timerTextView.rect.h - self.isDayTextView.rect.h), font=self.font) - def updateBasedOnPlayerStats(self, statistics: Statistics): - consoleLines = [] - if self.healthBar.value != statistics.hp: - self.healthBar.updateFill(statistics.hp) - consoleLines.append(self.timer.getPrettyTime() + " - Health: " + str(statistics.hp)) + def updateConsoleBasedOnPlayerStats(self, statistics: Statistics): + consoleLines = ["Health: " + str(statistics.hp), "Hunger: " + str(statistics.hunger), + "Stamina: " + str(statistics.stamina), "Thirst: " + str(statistics.thirst)] + self.console.addLinesToConsoleAndScrollToDisplayThem(consoleLines) - if self.hungerBar.value != statistics.hunger: - self.hungerBar.updateFill(statistics.hunger) - consoleLines.append(self.timer.getPrettyTime() + " - Hunger: " + str(statistics.hunger)) - - if self.staminaBar.value != statistics.stamina: - self.staminaBar.updateFill(statistics.stamina) - consoleLines.append(self.timer.getPrettyTime() + " - Stamina: " + str(statistics.stamina)) - - if self.thirstBar.value != statistics.thirst: - self.thirstBar.updateFill(statistics.thirst) - consoleLines.append(self.timer.getPrettyTime() + " - Thirst: " + str(statistics.thirst)) - - if len(consoleLines) > 0: - self.console.addLinesToConsoleAndScrollToDisplayThem(consoleLines) + def updateBarsBasedOnPlayerStats(self, statistics: Statistics): + self.healthBar.updateFill(statistics.hp) + self.hungerBar.updateFill(statistics.hunger) + self.staminaBar.updateFill(statistics.stamina) + self.thirstBar.updateFill(statistics.thirst) def updateBasedOnPygameEvent(self, event: pygame.event): if event.type == pygame.MOUSEBUTTONDOWN: @@ -91,6 +82,36 @@ class Ui(): elif event.button == 5: console.writeConsoleLines(console.topWrittenLineInd - 1) + def updateOnPlayerPickup(self, playerStats, pickedObject): + self.console.addLinesToConsoleAndScrollToDisplayThem([self.timer.getPrettyTime() + " - Picked object " + str(pickedObject.id) + ":"]) + self.updateConsoleBasedOnPlayerStats(playerStats) + + def updateOnPlayerInteraction(self, playerStats, interactedObject): + self.console.addLinesToConsoleAndScrollToDisplayThem([self.timer.getPrettyTime() + " - Player interacted with " + str(interactedObject.id) + ":"]) + self.updateConsoleBasedOnPlayerStats(playerStats) + + def updateOnDeath(self, player): + consoleLines = [] + + deathReason: StatisticNames = player.deathReason + + consoleLines.append(self.timer.getPrettyTime() + " - Game Over") + deathReasonString = "" + if deathReason is StatisticNames.HP: + deathReasonString = "Health issues" + elif deathReason is StatisticNames.HUNGER: + deathReasonString = "Hunger" + elif deathReason is StatisticNames.STAMINA: + deathReasonString = "Exhaustion" + elif deathReason is StatisticNames.THIRST: + deathReasonString = "Dehydration" + + consoleLines.append("Death reason: " + deathReasonString) + + consoleLines.append("Time alive: " + str(player.timeAlive / 1000)) + + self.console.addLinesToConsoleAndScrollToDisplayThem(consoleLines) + def updateTime(self): self.timerTextView.changeText(self.timer.getPrettyTime()) if self.timer.isItDay():