ADD global context for each game state
This commit is contained in:
23
main.py
23
main.py
@@ -2,26 +2,39 @@ import pygame
|
|||||||
from states.game import Game
|
from states.game import Game
|
||||||
from states.start_menu import StartMenu
|
from states.start_menu import StartMenu
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
pygame.init()
|
pygame.init()
|
||||||
screen = pygame.display.set_mode((720, 720))
|
screen = pygame.display.set_mode((720, 720))
|
||||||
clock = pygame.time.Clock()
|
clock = pygame.time.Clock()
|
||||||
|
|
||||||
states = {}
|
states = {}
|
||||||
|
context = {
|
||||||
|
"match": None,
|
||||||
|
"screen": screen,
|
||||||
|
"font": pygame.font.Font(None, 36),
|
||||||
|
"small_font": pygame.font.Font(None, 15),
|
||||||
|
"options": {
|
||||||
|
"show_yaw": True
|
||||||
|
}
|
||||||
|
}
|
||||||
current_state = None
|
current_state = None
|
||||||
|
|
||||||
def switch_state(state_name, data=None):
|
def switch_state(state_name):
|
||||||
nonlocal current_state
|
nonlocal current_state
|
||||||
if state_name == "game":
|
if state_name == "game":
|
||||||
match = data.get("match")
|
# Initialize Game state here
|
||||||
current_state = Game(switch_state, screen, match)
|
try:
|
||||||
|
match = context["match"]
|
||||||
|
except KeyError:
|
||||||
|
raise ValueError("Match object is required to initialize Game state.")
|
||||||
|
|
||||||
|
current_state = Game(switch_state, context)
|
||||||
states[state_name] = current_state
|
states[state_name] = current_state
|
||||||
|
|
||||||
current_state = states[state_name]
|
current_state = states[state_name]
|
||||||
|
|
||||||
# Initialize states
|
# Initialize states
|
||||||
states["start_menu"] = StartMenu(switch_state, screen)
|
states["start_menu"] = StartMenu(switch_state, context)
|
||||||
switch_state("start_menu")
|
switch_state("start_menu")
|
||||||
|
|
||||||
running = True
|
running = True
|
||||||
|
|||||||
@@ -4,11 +4,10 @@ from controllers.player_controller import PlayerController
|
|||||||
import pygame
|
import pygame
|
||||||
|
|
||||||
class Game(GameState):
|
class Game(GameState):
|
||||||
def __init__(self, switch_state_callback, screen, match):
|
def __init__(self, switch_state_callback, context):
|
||||||
super().__init__(switch_state_callback, screen)
|
super().__init__(switch_state_callback, context)
|
||||||
self.match = match
|
|
||||||
|
|
||||||
self.renderer = Renderer(self.match, screen)
|
self.renderer = Renderer(self.match, self.screen)
|
||||||
self.player_controller = PlayerController(self.renderer.player_render, self.match)
|
self.player_controller = PlayerController(self.renderer.player_render, self.match)
|
||||||
|
|
||||||
def handle_events(self, events):
|
def handle_events(self, events):
|
||||||
|
|||||||
@@ -1,7 +1,16 @@
|
|||||||
|
import pygame
|
||||||
|
|
||||||
class GameState:
|
class GameState:
|
||||||
def __init__(self, switch_state_callback, screen):
|
def __init__(self, switch_state_callback, context: dict):
|
||||||
self.switch_state = switch_state_callback
|
self.switch_state = switch_state_callback
|
||||||
self.screen = screen
|
self.context = context
|
||||||
|
self.screen = self.context.get("screen")
|
||||||
|
self.match = self.context.get("match", None)
|
||||||
|
self.font = self.context.get("font", pygame.font.Font(None, 36))
|
||||||
|
self.small_font = self.context.get("small_font", pygame.font.Font(None, 15))
|
||||||
|
self.options = self.context.get("options", {
|
||||||
|
"show_yaw": True
|
||||||
|
})
|
||||||
|
|
||||||
def handle_events(self, events):
|
def handle_events(self, events):
|
||||||
pass
|
pass
|
||||||
|
|||||||
@@ -9,13 +9,12 @@ import pygame
|
|||||||
import pygame_gui
|
import pygame_gui
|
||||||
|
|
||||||
class StartMenu(GameState):
|
class StartMenu(GameState):
|
||||||
def __init__(self, switch_state_callback, screen):
|
def __init__(self, switch_state_callback, context):
|
||||||
super().__init__(switch_state_callback, screen)
|
super().__init__(switch_state_callback, context)
|
||||||
self.screen = screen
|
self.manager = pygame_gui.UIManager((self.screen.get_width(), self.screen.get_height()))
|
||||||
self.manager = pygame_gui.UIManager((screen.get_width(), screen.get_height()))
|
|
||||||
|
|
||||||
self.default_button_start_x = self.screen.get_width() * 0.1
|
self.default_button_start_x = self.screen.get_width() * 0.1
|
||||||
self.default_button_width = screen.get_width() * 0.8
|
self.default_button_width = self.screen.get_width() * 0.8
|
||||||
|
|
||||||
# buttons
|
# buttons
|
||||||
self.button = Button(self.default_button_start_x,
|
self.button = Button(self.default_button_start_x,
|
||||||
@@ -43,7 +42,8 @@ class StartMenu(GameState):
|
|||||||
def _start_game_callback(self):
|
def _start_game_callback(self):
|
||||||
"""Starts the game."""
|
"""Starts the game."""
|
||||||
match = self._setup_game(self.demo_file)
|
match = self._setup_game(self.demo_file)
|
||||||
self.switch_state("game", {"match": match})
|
self.context["match"] = match
|
||||||
|
self.switch_state("game")
|
||||||
|
|
||||||
def _setup_game(self, demo_file: str) -> Match:
|
def _setup_game(self, demo_file: str) -> Match:
|
||||||
demo_parser = demoparser2.DemoParser(demo_file)
|
demo_parser = demoparser2.DemoParser(demo_file)
|
||||||
|
|||||||
Reference in New Issue
Block a user