|
|
|
|
@ -2,9 +2,11 @@ import tomllib
|
|
|
|
|
import time
|
|
|
|
|
|
|
|
|
|
import NoPELib
|
|
|
|
|
import pygame
|
|
|
|
|
|
|
|
|
|
from .events import Timeout
|
|
|
|
|
from .anim import AnimatedObject
|
|
|
|
|
from .utils import centre
|
|
|
|
|
|
|
|
|
|
class Game:
|
|
|
|
|
def __init__(self, surface):
|
|
|
|
|
@ -18,8 +20,21 @@ class Game:
|
|
|
|
|
with open('./game.toml', 'r') as f:
|
|
|
|
|
self.cfg = tomllib.loads(f.read())
|
|
|
|
|
|
|
|
|
|
self.surf = surface
|
|
|
|
|
# The surface that gets drawn by the launcher.
|
|
|
|
|
# Contains the game surface, and overlays.
|
|
|
|
|
self._surf = surface
|
|
|
|
|
# The games serface
|
|
|
|
|
self.surf = pygame.Surface(self._surf.size)
|
|
|
|
|
self.size = self.surf.size
|
|
|
|
|
# Used for time delta, stores when the last frame was drawn
|
|
|
|
|
self._lastFrame = time.perf_counter()
|
|
|
|
|
# How many seconds it's been between frames
|
|
|
|
|
self.timeDelta = 0
|
|
|
|
|
# Pre-render semi-transparent black surface
|
|
|
|
|
self._dimSurf = pygame.Surface(self._surf.size)
|
|
|
|
|
self._dimSurf.set_alpha(128)
|
|
|
|
|
pygame.draw.rect(self._dimSurf, (50, 50, 50), (0, 0, self.size[0], self.size[1]))
|
|
|
|
|
|
|
|
|
|
self.pm = NoPELib.PlayersManager(self.cfg['name'], playersPath='../players.json')
|
|
|
|
|
# Holds all the timeouts that haven't been fired yet
|
|
|
|
|
self._timeouts = []
|
|
|
|
|
@ -50,7 +65,17 @@ class Game:
|
|
|
|
|
if not self._haltUpdate:
|
|
|
|
|
self.update()
|
|
|
|
|
|
|
|
|
|
# TODO: Draw things like debug menu, etc.
|
|
|
|
|
# Draw the game surface onto the main surface
|
|
|
|
|
self._surf.blit(self.surf, (0, 0))
|
|
|
|
|
|
|
|
|
|
# If there's a popup
|
|
|
|
|
if self._popupMenu is not None:
|
|
|
|
|
# Dim the screen
|
|
|
|
|
self._surf.blit(self._dimSurf, (0, 0))
|
|
|
|
|
self._surf.blit(centre(self._popupMenu, self.size), (0, 0))
|
|
|
|
|
|
|
|
|
|
self.timeDelta = time.perf_counter() - self._lastFrame
|
|
|
|
|
self._lastFrame = time.perf_counter()
|
|
|
|
|
|
|
|
|
|
def onEvent(self, event):
|
|
|
|
|
"""
|
|
|
|
|
@ -65,6 +90,12 @@ class Game:
|
|
|
|
|
|
|
|
|
|
if not self._haltEvents:
|
|
|
|
|
self.onEvent(event)
|
|
|
|
|
|
|
|
|
|
if event.type == pygame.KEYDOWN and self._popupMenu is not None:
|
|
|
|
|
if event.key == 13 and event.mod & 0b10000000: # R-CTRL + ENTER
|
|
|
|
|
self._popupMenu = None
|
|
|
|
|
self._haltUpdate = False
|
|
|
|
|
self._haltEvents = False
|
|
|
|
|
|
|
|
|
|
def close(self):
|
|
|
|
|
"""
|
|
|
|
|
@ -110,23 +141,37 @@ class Game:
|
|
|
|
|
|
|
|
|
|
return any(timeout.timeoutID == timeoutID for timeout in self._timeouts)
|
|
|
|
|
|
|
|
|
|
def popup(self, title: str, text: str, haltUpdate: bool=False, haltEvents: bool=True):
|
|
|
|
|
def popup(self, title: str, message: str, haltUpdate: bool=False, haltEvents: bool=True):
|
|
|
|
|
"""
|
|
|
|
|
Pops up a dialogue box.
|
|
|
|
|
|
|
|
|
|
Args:
|
|
|
|
|
title (str): The big "title" text to be displayed.
|
|
|
|
|
text (str): The main body text to be displayed.
|
|
|
|
|
message (str): The main body text to be displayed.
|
|
|
|
|
haltUpdate (bool): If True, the games update() method will not be called until
|
|
|
|
|
the popup is dismissed. Default: False.
|
|
|
|
|
haltEvents (bool): If True, the games onEvent() method will not be called until
|
|
|
|
|
the popup is dismissed. Default: True.
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
POPUP_MARGINS = 10
|
|
|
|
|
FONT_BIG = pygame.font.SysFont('', 50)
|
|
|
|
|
FONT_NORM = pygame.font.SysFont('', 32)
|
|
|
|
|
|
|
|
|
|
titleSurf = FONT_BIG.render(title, True, (128, 128, 128))
|
|
|
|
|
messageSurf = FONT_NORM.render(message, True, (128, 128, 128))
|
|
|
|
|
|
|
|
|
|
w = max(titleSurf.get_width(), messageSurf.get_width())
|
|
|
|
|
w += POPUP_MARGINS*2
|
|
|
|
|
h = titleSurf.get_height() + messageSurf.get_height()
|
|
|
|
|
h += POPUP_MARGINS*3
|
|
|
|
|
|
|
|
|
|
self._haltUpdate = haltUpdate
|
|
|
|
|
self._haltEvents = haltEvents
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
self._popupMenu = pygame.Surface((w, h))
|
|
|
|
|
self._popupMenu.fill((0, 0, 0))
|
|
|
|
|
self._popupMenu.blit(centre(titleSurf, (w, titleSurf.get_height())), (0, POPUP_MARGINS))
|
|
|
|
|
self._popupMenu.blit(centre(messageSurf, (w, messageSurf.get_height())), (0, titleSurf.get_height()+POPUP_MARGINS*2))
|
|
|
|
|
|
|
|
|
|
def punish(self, player, intensity: float):
|
|
|
|
|
"""
|
|
|
|
|
|